我部署了一个WCF服务,该服务由WCF 4.6.1框架实现,其中包含一个与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的替代品?
请注意,我不应该更改服务中的任何内容,因为其他应用程序正在使用它
答案 0 :(得分: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,您无需在其中添加binging元素。(它们已添加)。
下面是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>