【问题标题】:How Can I resolve WS2007HttpBinding WCF Service Binding in .NET Core Client Application如何解决 .NET Core 客户端应用程序中的 WS2007HttpBinding WCF 服务绑定
【发布时间】:2019-06-05 17:10:11
【问题描述】:

我处理了一个使用 WCF 4.6.1 框架实现的 WCF 服务,其中包含一个使用 WS2007HttpBinding 绑定的端点。 我的目的是实现一个调用此服务的 .NET Core 客户端应用程序。 当我尝试使用 CustomBinding 时,它会生成此错误:

System.InvalidOperationException:“ServiceEndpoint 上的 CustomBinding 与合同“IWSTrust13Sync”缺少 TransportBindingElement。每个绑定都必须至少有一个从 TransportBindingElement 派生的绑定元素。'

public class Program
{
    public static void Main(string[] args)
    {
        Message requestmessage = Message.CreateMessage(
          MessageVersion.Soap12WSAddressing10,
          "http://test/test",
           "This is the body data");

        var binding = new CustomBinding();
        var endpoint = new EndpointAddress(new Uri("http://servicetest.service.svc"));
        var channelFactory = new ChannelFactory<ServiceSTS.IWSTrust13Sync>(binding, endpoint);
        var serviceClient = channelFactory.CreateChannel();
        var result = serviceClient.Trust13ValidateAsync(requestmessage);
        channelFactory.Close();

        Console.WriteLine(result.Status);
    }
}

.NET Core 中有 WS2007HttpBinding 的替代品吗?

请注意,我不应该更改服务中的任何内容,因为它正在被其他应用程序使用

【问题讨论】:

    标签: c# visual-studio wcf .net-core service-model


    【解决方案1】:

    如错误所示,您缺少 TransportBindingElement。 绑定由 BingingElement 组成,没有 bingingelement 的自定义绑定毫无意义。 如果你想使用自定义绑定,你应该在其中添加 binging 元素。

    BindingElement[] elements = new BindingElement[]
            {
    
                           new TextMessageEncodingBindingElement(),
              new HttpTransportBindingElement()
            };
            CustomBinding binding = new CustomBinding(elements);
                        using (ChannelFactory<ICalculatorService> channelFacoty = new ChannelFactory<ICalculatorService>(binding, new EndpointAddress("http://localhost:4000/calculator")))
            {
                ICalculatorService cal = channelFacoty.CreateChannel();
               Console.WriteLine( cal.Add(1, 3));
                Console.Read();
            }
    

    但是既然您在服务器端使用 ws2007httpbinging,为什么不直接在客户端使用 ws2007httpbinding。使用 ws2007httpbinging,你不需要在其中添加 bingingelements。(它们已被添加)。

    下面是ws2007httpbinding的简单使用。

      WS2007HttpBinding binding2 = new WS2007HttpBinding();
            using (ChannelFactory<ICalculatorService> channelFacoty = new ChannelFactory<ICalculatorService>(binding2, new EndpointAddress("http://localhost:4000/calculator")))
            {
                ICalculatorService cal = channelFacoty.CreateChannel();
               Console.WriteLine( cal.Add(1, 3));
                Console.Read();
            }
    

    但是如果你的服务端有ws2007httpbinging的特殊配置,你最好根据服务端的配置来配置你的客户端的ws2007httpbinging。

    如果你的服务端已经发布了它的元数据,你可以添加对它的引用,直接使用客户端调用服务。 https://www.dotnetforall.com/adding-wcf-service-reference-client/ 对于svc,默认的服务元数据地址是它的地址+WSDL,

    例如,您的元数据地址是http://servicetest.service.svc?wsdl

    您也可以使用 App.config 来配置您的客户端。

    <client>
       <endpoint address="http://localhost:4000/calculator" binding="ws2007HttpBinding"
      contract="ServiceInterface.ICalculatorService" name="cal" />
    </client>
    

    然后,您可以直接创建客户端,如下所示,

     using (ChannelFactory<ICalculatorService> channelFacoty = new ChannelFactory<ICalculatorService>("cal"))
            {
                ICalculatorService cal = channelFacoty.CreateChannel();
               Console.WriteLine( cal.Add(1, 3));
                Console.Read();
            }
    

    请根据您服务器端的端点配置配置您的客户端。 我的是

    <service name="Service.CalculatorService"  >
                 <endpoint address="http://localhost:4000/calculator"  binding="ws2007HttpBinding"   contract="ServiceInterface.ICalculatorService"></endpoint>
      </service>
    

    【讨论】:

    • 我正在使用 .NET CORE 客户端应用程序,因此我没有 App.config 文件,也没有 WS2007HttpBinding。但是您的解决方案给了我另一种反思方式。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多