How to create a WCF service that provides your business layer?

WCF promotes good design using interfaces and contracts, etc. Which puzzles me, for example, in my case, if I have two sets of business functions, such as ICustomerMgmtBIZ and IProductMgmtBiz. If these two are ServiceContracts and I have an interface like

IBusinessService: IProductMgmtBIZ, ICustomerMgmtBIZ

and the BusinessService implementation class. I see that the BusinessService class will have too much implementation. The workaround I've used so far is to implement partial classes.

So roughly speaking, can a WCF service have only 1 implementation and 1 service contract?

+4
source share
2 answers

No, it is possible to implement more than one service contract by the type of WCF service (the class that is assigned to the ServiceBehavior attribute), because it is just a matter of having the class implement multiple interfaces. If you use any Visual Studio templates or other code generators, this may not be immediately clear. However, although you can implement more than one Service Contract interface in a service type, it is not very useful if you need a service, presumably a singleton in this case (?), To behave as a single service. IBusinessService implies that you need all of the service functions to be called from the same client proxy, so that all operations can work in the same logical session (similar to the ASPX web session). If this is not the case, then you can define separate proxies for each interface of the contract, but it will also require support for one endpoint for each contract.

Is this an absolute requirement that you can only use for an instance of WCF ServiceHost for your implementation? What factors influence your decision?

By the way, partial classes no longer bother me. The idea of โ€‹โ€‹splitting code into multiple files now seems pretty natural. For example, storing partial classes in files such as ServiceType_IProductMgmtBiz.cs and ServiceType_ICustomerMgmtBIZ.cs seems quite natural, in addition to storing the main logic in ServiceType.cs.

Finally, the following question may be useful ... WCF and interface inheritance - Is this a terrible thing?

+3
source

Overly set, no - sort of - yes, but. Any workaround is not optimal and involves the use of IBlank as the main WCF interface (where your interfaces are derived from IBlank) and two endpoints, one of which implements IProductMgmtBIZ, and the other implements ICustomerMgmtBIZ. I don't have my dev machine in front of me, this may include some other overrides. So, at the WCF level you are screwed up if you don't want to have two WCF ServiceHosts (which is quite reasonable).

In short, the workaround is inelegant. It is easier to have two WCF endpoints on the same port with a different extension.

+1
source

All Articles