WCF service returns another service (factory service?)

We use WCF for communication between the client and the server application. The client application has many functions that require communication with the server - and we decided to implement this in several classes (division of responsibility)

Over time, we create new WCF endpoints and service contracts for each facility — billing, accounting, content management, etc. This causes a lot of endpoint configurations both on the client and on the server (with potential problems with incorrect configuration when moving to test and production platforms).

I would like to know if I can define one WCF endpoint that can run multiple service contact implementations. Our configuration files will then contain one endpoint (to the factory service), and I can request different services by indicating the interface of the service that interests me.

eg.

using (IServiceClientFactory serviceClientFactory = new RealProxyServiceClientFactory()) { // This is normal WCF proxy object creation. IServiceFactory serviceFactory = serviceClientFactory.CreateInstance<IServiceFactory>(""); // This is what we would like to do IInvoiceService invoiceService = serviceFactory.getService(typeof(IInvoiceService)); invoiceService.executeOperation(data); } 

The key is a single endpoint configuration for a client / server pair, and not an endpoint configuration for a single service contact that I would like to make available.

Is it possible?

+6
c # wcf endpoints
source share
3 answers

I do not 100% understand what you are trying to do, but if you just want to have different contracts at the same address with the implementation inside the same service class, this is quite possible. To share the endpoint address, you must ensure that you use the same binding instance for each service endpoint.

Here is a complete sample that defines 3 contracts, 1 class of service that implements all of them, and ServiceHost with three contract endpoints at the same address:

 using System; using System.ServiceModel; [ServiceContract] interface IContractA { [OperationContract] void A(); } [ServiceContract] interface IContractB { [OperationContract] void B(); } [ServiceContract] interface IContractC { [OperationContract] void C(); } [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] class Service : IContractA, IContractB, IContractC { public Service() { } public void A() { Console.WriteLine("A"); } public void B() { Console.WriteLine("B"); } public void C() { Console.WriteLine("C"); } } class Program { public static void Main(string[] args) { Uri address = new Uri("net.pipe://localhost/Service/"); ServiceHost host = new ServiceHost(new Service(), address); NetNamedPipeBinding binding = new NetNamedPipeBinding(); host.AddServiceEndpoint(typeof(IContractA), binding, string.Empty); host.AddServiceEndpoint(typeof(IContractB), binding, string.Empty); host.AddServiceEndpoint(typeof(IContractC), binding, string.Empty); host.Open(); IContractA proxyA = ChannelFactory<IContractA>.CreateChannel(new NetNamedPipeBinding(), new EndpointAddress(address)); proxyA.A(); ((IClientChannel)proxyA).Close(); IContractB proxyB = ChannelFactory<IContractB>.CreateChannel(new NetNamedPipeBinding(), new EndpointAddress(address)); proxyB.B(); ((IClientChannel)proxyB).Close(); IContractC proxyC = ChannelFactory<IContractC>.CreateChannel(new NetNamedPipeBinding(), new EndpointAddress(address)); proxyC.C(); ((IClientChannel)proxyC).Close(); host.Close(); } } 
+3
source share

I doubt it will work. Xml serialization may be the biggest issue here.

And I don’t think you really need it. If I were in your place, I would try to distract my communication with the service. Basically, you always send a “message” to a service whose “Target” is one of the classes that you wanted to access. The service will always reply “Reply”, the contents of which will be filled by the class to which the message “Message” has been sent.

Another approach would be to route all of these messages through a service, which will be an echo request to the corresponding service. This way you maintain scalability, but it still has a large configuration.

NTN.

+1
source share

It looks like you want to keep your individual services, but you have some kind of bus that goes through the route. MSMQ, maybe then you can have one service that receives each message, falling into a specific queue, and then a dedicated service can read it from this particular queue.

This is actually not a WCF-based solution, though admittedly.

The concept of a single interface (read as a ServiceContract) implemented by several classes will not work. Thus, you will need one "monster" that implements everything and directs to the correct service. Samples of the facades resemble.

0
source share

All Articles