Two interfaces and one specific class in WCF

please check the example below

namespace GServices { [ServiceKnownType(typeof(SearchType))] [ServiceContract(SessionMode = SessionMode.Allowed)] public interface ITest { [OperationContract] int subtract(int x, int y); } [ServiceKnownType(typeof(SearchType))] [ServiceContract(SessionMode = SessionMode.Allowed)] public interface ITest2 { [OperationContract] int add(int x, int y); } public class G : ITest2, ITest { public int add(int x, int y) { return x + y; } public int subtract(int x, int y) { return x + y; } } } 

The ITest method has a subtract () method, and the Itest2 method has an add () method.

Both are implemented by one specific class G.

If I just want to expose ITest through WCF, I have the following endpoint configuration

  <service name="GQS1" behaviorConfiguration="GQwcfBehaviour"> <endpoint address="DP2Svcs" binding="wsHttpContextBinding" bindingConfiguration="wsHttpEndpointBindingConfig" contract="GServices.itest"> <identity> <dns value="localhost" /> </identity> </endpoint> </service> 

when I start this service and check wsdl, I see that the methods that are in itest2 also appeared in wsdl. in this case, the subtract () method should be open. But the add () method is also displayed.

My requirement is that the methods in the ITest interface should only be displayed. in this case, I want to set only the subtract () method, which is declared in ITest. But both of their implementations are in only one specific class "G". What am I missing here?

Edit: I provided my Service.svc content file:

 <%@ ServiceHost Language="C#" Debug="true" Service="GServices.G" %> 

enter image description here

+6
source share
2 answers

Verify that the value of the name attribute in the <service> element in the configuration is the fully qualified name of the service class. In your configuration, you have the final contract name specified in the namespace (GServices.itest), but the service is not (GQS1). If you do not have any service for a specific service, WCF will add one default endpoint that will reveal your problem. For example, in the code below, where a line that adds one endpoint is commented out, the WSDL on the service shows both operations. But if you uncomment the line (which forces the service to have only one endpoint of type ITest), only the subtract operation will be shown.

 public class StackOverflow_11339853 { [ServiceContract(SessionMode = SessionMode.Allowed)] public interface ITest { [OperationContract] int subtract(int x, int y); } [ServiceContract(SessionMode = SessionMode.Allowed)] public interface ITest2 { [OperationContract] int add(int x, int y); } public class G : ITest2, ITest { public int add(int x, int y) { return x + y; } public int subtract(int x, int y) { return x + y; } } public static void Test() { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(G), new Uri(baseAddress)); // host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true }); host.Open(); Console.WriteLine("Host opened"); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); } } 
+4
source

If you do not need the ITest2 interface open as a service, simply remove the ServiceContract attribute from it.

If you need ITest2 in another service, you can use interface inheritance to solve this problem:

 interface ITest2 { [OperationContract] int Add(int x, int y); } [ServiceContract] interface ITest2Service : ITest2 { } 

Use ITest2 in the first service (which also implements ITest ) and ITest2Service in the second service.

+1
source

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


All Articles