vik*_*ikp 2 .net c# wcf wshttpbinding .net-core
我正在寻找 dotnet core WCF wsHttpBinding 解决方法。
我知道 .net core WCF 实现目前不支持 wsHttpBinding (请参阅此处的支持矩阵https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md)
我正在与似乎仅支持 wsHttpBinding 的遗留第三方服务集成。我们的技术堆栈是 .net core,因此我无法恢复到 .net 框架的完整版本或 mono 变体。
问题是是否可以通过自定义绑定使用该服务?我希望有一种解决方法,可能无法完全发挥作用,但至少允许我使用该服务。
var cBinding = new CustomBinding();
var textBindingElement = new TextMessageEncodingBindingElement()
{
MessageVersion = MessageVersion.Soap12WSAddressing10
};
cBinding.Elements.Add(textBindingElement);
var httpBindingElement =
new HttpsTransportBindingElement
{
AllowCookies = true, MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue,
};
cBinding.Elements.Add(httpBindingElement);
var myEndpoint = new EndpointAddress("https://..../Service.svc/wss");
using (var myChannelFactory = new ChannelFactory<ISearch>(cBinding, myEndpoint))
{
myChannelFactory.Credentials.UserName.UserName = "...";
myChannelFactory.Credentials.UserName.Password = "...";
ISearch client = null;
try
{
client = myChannelFactory.CreateChannel();
var result = client.Find(new Search("Criteria")).Result;
((ICommunicationObject)client).Close();
myChannelFactory.Close();
}
catch (Exception ex)
{
(client as ICommunicationObject)?.Abort();
}
}
Run Code Online (Sandbox Code Playgroud)
创建客户端并对服务进行调用,但失败,因为:
Message =“无法处理消息。这很可能是因为操作“”不正确,或者因为消息包含无效或过期的安全上下文令牌,或者因为绑定之间不匹配
算了,它们与 Core 并不完全兼容。在某些特定情况下,可能会有对 WsHttpBinding 的基本调用。你可以参考下面的例子。
服务器。
Uri uri = new Uri("http://localhost:11011");
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.None;
using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
{
sh.AddServiceEndpoint(typeof(IService), binding, "");
ServiceMetadataBehavior smb;
smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb==null)
{
smb = new ServiceMetadataBehavior()
{
};
sh.Description.Behaviors.Add(smb);
}
Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
sh.Opened += delegate
{
Console.WriteLine("Service is ready");
};
sh.Closed += delegate
{
Console.WriteLine("Service is clsoed");
};
sh.Open();
Run Code Online (Sandbox Code Playgroud)
客户端(自动生成)
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.WSHttpBinding_IService))
{
System.ServiceModel.Channels.CustomBinding result = new System.ServiceModel.Channels.CustomBinding();
System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
result.Elements.Add(textBindingElement);
System.ServiceModel.Channels.HttpTransportBindingElement httpBindingElement = new System.ServiceModel.Channels.HttpTransportBindingElement();
httpBindingElement.AllowCookies = true;
httpBindingElement.MaxBufferSize = int.MaxValue;
httpBindingElement.MaxReceivedMessageSize = int.MaxValue;
result.Elements.Add(httpBindingElement);
return result;
}
Run Code Online (Sandbox Code Playgroud)
调用。
ServiceReference1.ServiceClient client2 = new ServiceReference1.ServiceClient();
try
{
var res = client2.SayHelloAsync();
Console.WriteLine(res.Result);
}
catch (Exception)
{
throw;
}
Run Code Online (Sandbox Code Playgroud)
而在大多数情况下,无法调用 WsHttpBinding 创建的 WCF 服务。官方也无意继续支持wshttpbinding计划。以下是相关讨论。
https://github.com/dotnet/wcf/issues/31
https://github.com/dotnet/wcf/issues/1370
| 归档时间: |
|
| 查看次数: |
7409 次 |
| 最近记录: |