Two Interface and one concrete class in WCF

只愿长相守 提交于 2019-12-05 07:25:51

Make sure 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 config you have the endpoint contract name qualified by a namespace (GServices.itest), but the service is not (GQS1). If you don't have any service confugration for a specific service, WCF will add one default endpoint which wil expose the problem you have. For example, in the code below, where the line which adds one endpoint is commented out, the WSDL on the service shows both operations. But if you uncomment the line (which will make the service 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();
    }
}

If you don't need the ITest2 interface exposed as a service, simply remove the ServiceContract attribute from it.

If you need ITest2 in a different service, you can use interface inheritance to solve this issue:

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

[ServiceContract]
interface ITest2Service : ITest2 { }

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!