两个接口,一个具体类的WCF [英] Two Interface and one concrete class in WCF

查看:191
本文介绍了两个接口,一个具体类的WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查下面的例子

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有减()方法,并Itest2有add()方法。

ITest has subtract() method and Itest2 has add() method.

两者都是由一个具体的类称为图G来实现。

Both are implemented by one concrete class called G.

如果我只是想揭露ITEST通过WCF,我有以下端点配置

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

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

当我运行此服务并检查WSDL,我可以看到,这是在itest2方法也出现了WSDL。在本实施例的情况下,减法()方法只应被曝光。但是add()方法也被暴露出来。

when i run this service and check the wsdl, I can see that the methods which are in itest2 also appeared in wsdl. in this example case , subtract() method should only be exposed. But add() method is also exposed.

我的要求是已经在ITest接口方法应该只暴露。在这种情况下,我要揭露它在ITEST声明只减()方法。但无论其执行驻留在只有一个具体的类G。我缺少的是在这里吗?

My requirement is to have methods in ITest Interface should only exposed. in this case , i want to expose only subtract() method which is declared in ITest. But both of their implementation resides in Only one concrete class "G". What am I missing here?

编辑:我已经给我的Service.svc文件内容:

Edit : I have given my Service.svc file content :

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

推荐答案

确认,在&LT的名称属性的值;服务&GT ; 配置中的元素是完全合格的域名服务类的。在你的配置,你必须通过一个命名空间(GServices.itest)合格的端点合同的名称,但服务不是(GQS1)。如果你没有任何服务confugration特定服务,WCF会添加一个默认终结它西港岛线暴露你的问题。例如,在code以下,其中添加了一个端点,该行已被注释掉,在服务的WSDL显示了这两个操作。但是,如果你去掉注释(这将使得服务有型ITEST只有一个终点),只有减操作会显示出来。

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

这篇关于两个接口,一个具体类的WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆