Multiple WCF Services Implementing the Same Service Contract Interface

Is it possible for multiple wcf services to implement the same service contract interface?

I want to make sure that the test service can be interchangeable for the real service and indicate which service will be used in the configuration file.

For instance:

[ServiceContract] public interface IUselessService { [OperationContract] string GetData(int value); } 

Test execution

 public class TestService : IUselessService { public string GetData(int value) { return "This is a test"; } } 

Valid class

 public class RealService : IUselessService { public string GetData(int value) { return string.Format("You entered: {0}", value); } } 
+4
source share
3 answers

Thank you guys for your answers. I now have a solution that works for me without putting the interface in a separate assembly and in the GAC. I do not look at using an interface for other projects, just using the same interface for several services in one project.

What I was trying to do was to make changes between RealService and TestService in the WCF service configuration file so that the client does not know the difference (the client would not have to change its configuration to point to another. Svc file). I'm not sure if this is possible, or the least, if so, it is definitely not straightforward.

What I'm doing now is just to specify both services in the WCF service configuration file, and then I tell the client one option or another based on which I want. Since this WCF service is for internal use only, and we control both the client and the service, this is not a bad compromise. In any case, this decision is probably more explicit in its intentions.

Here is a snippet of the configuration file:

 <services> <service behaviorConfiguration="WcfService1.Service1Behavior" name="WcfService1.TestService"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="testBasicHttpBinding" contract="WcfService1.IUselessService"> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service behaviorConfiguration="WcfService1.Service1Behavior" name="WcfService1.RealService"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="testBasicHttpBinding" contract="WcfService1.IUselessService"> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> 
+4
source

Yes, this is not only possible, it is clearly related to the design intentions of the service contract interfaces.

+4
source

If you define an IUselessService interface inside a separate assembly and put it in the GAC. This assembly should not implement anything, just define the IUselessService interface and some other types that use needs as parameters of the IUselessService.

Both TestService and RealService must implement the same IUselessService interface. I mean, you have to create two additional projects for each service, then TestService and RealService will not have type conflicts.

+2
source

Source: https://habr.com/ru/post/1308088/


All Articles