c# - WCF 中的两个接口(interface)和一个具体类

标签 c# .net wcf known-types

请检查下面的例子

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

两者都由一个名为 G 的具体类实现。

如果我只想通过 WCF 公开 ITest,我有以下端点配置

  <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 中。在这个例子中,subtract() 方法应该只公开。但是 add() 方法也暴露了。

我的要求是只应公开 ITest 接口(interface)中的方法。在这种情况下,我只想公开 ITest 中声明的 subtract() 方法。但是它们的实现都只存在于一个具体类“G”中。我在这里缺少什么?

编辑:我已提供我的 Service.svc 文件内容:

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

enter image description here

最佳答案

确保 name 的值<service> 中的属性配置中的元素是服务类的完全限定名称。在您的配置中,您的端点契约(Contract)名称由命名空间 (GServices.itest) 限定,但服务不是 (GQS1)。如果您没有针对特定服务的任何服务配置,WCF 将添加一个默认端点,这将暴露您遇到的问题。例如,在下面的代码中,添加一个端点的行被注释掉了,服务上的 WSDL 显示了这两个操作。但是,如果您取消注释该行(这将使服务只有一个类型为 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();
    }
}

关于c# - WCF 中的两个接口(interface)和一个具体类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11339853/

相关文章:

c# - 使用 Entity Framework 重构代码

c# - 将对象转换为 List<T> 以便能够使用扩展方法

c# - 如何使用类型调用 default(T)?

c# - 如何使用套接字在客户端服务器应用程序中设置缓冲区大小?

c# - 如何在 .NET 4.0 中正确使用 async/await

c# - Xamarin/Android 和可怕的蓝牙 LE 错误 133 (GATT_ERROR)

c# - 在链接构造函数时初始化 Dictionary<string, string>

C# 创建自定义对话框结果表单

javascript - 400 错误请求 - jquery WCF ajax 调用

c# - 在 WCF 服务调用中禁用输入参数的自动处理