簡體   English   中英

如何解決 .NET Core 客戶端應用程序中的 WS2007HttpBinding WCF 服務綁定

[英]How Can I resolve WS2007HttpBinding WCF Service Binding in .NET Core Client Application

我處理了一個用 WCF 4.6.1 框架實現的 WCF 服務,其中包含一個與 WS2007HttpBinding 綁定的端點。 我的目的是實現一個調用此服務的 .NET Core 客戶端應用程序。 當我嘗試使用 CustomBinding 時,它會生成此錯誤:

System.InvalidOperationException:“具有合同“IWSTrust13Sync”的 ServiceEndpoint 上的 CustomBinding 缺少 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 的替代品?

請注意,我不應該更改服務中的任何內容,因為它正在被其他應用程序使用

如錯誤所示,您缺少 TransportBindingElement。 Binding 由 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM