简体   繁体   English

WCF中的两个接口和一个具体类

[英]Two Interface and one concrete class in WCF

please check the below example 请查看以下示例

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;
        }
    }
}

ITest has subtract() method and Itest2 has add() method. ITest有subtract()方法,Itest2有add()方法。

Both are implemented by one concrete class called G. 两者都由一个名为G的具体类实现。

If i just want to expose the ITest through WCF, I have following endpoint config 如果我只想通过WCF公开ITest,我有以下端点配置

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

when i run this service and check the wsdl, I can see that the methods which are in itest2 also appeared in wsdl. 当我运行这个服务并检查wsdl时,我可以看到itest2中的方法也出现在wsdl中。 in this example case , subtract() method should only be exposed. 在此示例中,应仅公开subtract()方法。 But add() method is also exposed. 但是add()方法也暴露了。

My requirement is to have methods in ITest Interface should only exposed. 我的要求是只暴露ITest接口中的方法。 in this case , i want to expose only subtract() method which is declared in ITest. 在这种情况下,我想只公开在ITest中声明的subtract()方法。 But both of their implementation resides in Only one concrete class "G". 但它们的实现都只存在于一个具体的类“G”中。 What am I missing here? 我在这里错过了什么?

Edit : I have given my Service.svc file content : 编辑:我已经提供了Service.svc文件内容:

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

在此输入图像描述

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. 确保配置中<service>元素中name属性的值是服务类的完全限定名称 In your config you have the endpoint contract name qualified by a namespace (GServices.itest), but the service is not (GQS1). 在您的配置中,您具有由命名空间(GServices.itest)限定的端点协定名称,但该服务不是(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. 如果您没有针对特定服务的任何服务配置,WCF将添加一个默认端点,它将揭露您遇到的问题。 For example, in the code below, where the line which adds one endpoint is commented out, the WSDL on the service shows both operations. 例如,在下面的代码中,添加一个端点的行被注释掉,服务上的WSDL显示两个操作。 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. 但是,如果取消注释该行(这将使服务只有一个类型为ITest的端点),则只显示“减去”操作。

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. 如果您不需要将ITest2接口公开为服务,只需从中删除ServiceContract属性即可。

If you need ITest2 in a different service, you can use interface inheritance to solve this issue: 如果您在其他服务中需要ITest2 ,则可以使用接口继承来解决此问题:

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. 使用ITest2第一服务(也实现ITest )和ITest2Service第二服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM