WCF 客户端使用带有 Soap 标头的 ASMX Web 服务
我有一个 ASMX Web 服务,需要一个肥皂头和一个通过服务引用 (WCF) 使用该服务的客户端应用程序。
服务器:
[WebService(Namespace = "http://myserviceurl.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service1 : System.Web.Services.WebService
{
public MyCustomSoapHeader MyHeader;
[WebMethod]
[SoapHeader("MyHeader", Direction = SoapHeaderDirection.In)]
public string MyMethod()
{
if(MyHeader.SomeProperty == false)
{
return "error";
}
return "success";
}
public class MyCustomSoapHeader: SoapHeader
{
public bool SomeProperty { get; set; }
}
}
客户端:
public class MyClient
{
var address = new EndpointAddress(_myServerUrl)
var binding = new BasicHttpBinding();
binding.Name = "SoapBinding";
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.ReceiveTimeout = TimeSpan.FromSeconds(_timeout);
Service1SoapClient client = new Service1SoapClient(binding, address);
string expected = client.MyMethod(new MyCustomSoapHeader(){SomeProperty = true});
}
堆栈跟踪:
System.ServiceModel.FaultException: Server was unable to process request. ---> Computer name could not be obtained. Server stack trace: at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
如何在仍在使用 WCF 的客户端上修复此问题?我无法更改服务器代码,并且需要在客户端上使用 WCF,无法添加 Web 引用。
I have an ASMX web service that has requires a soap header and a client app consuming this service via a service reference (WCF).
Server:
[WebService(Namespace = "http://myserviceurl.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service1 : System.Web.Services.WebService
{
public MyCustomSoapHeader MyHeader;
[WebMethod]
[SoapHeader("MyHeader", Direction = SoapHeaderDirection.In)]
public string MyMethod()
{
if(MyHeader.SomeProperty == false)
{
return "error";
}
return "success";
}
public class MyCustomSoapHeader: SoapHeader
{
public bool SomeProperty { get; set; }
}
}
Client:
public class MyClient
{
var address = new EndpointAddress(_myServerUrl)
var binding = new BasicHttpBinding();
binding.Name = "SoapBinding";
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.ReceiveTimeout = TimeSpan.FromSeconds(_timeout);
Service1SoapClient client = new Service1SoapClient(binding, address);
string expected = client.MyMethod(new MyCustomSoapHeader(){SomeProperty = true});
}
Stack trace:
System.ServiceModel.FaultException: Server was unable to process request. ---> Computer name could not be obtained. Server stack trace: at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
How can I fix this on the client side still using WCF? I am unable to change the server code and I need to use WCF on the client, I can't add a web reference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于我刚刚回答的另一个问题(关于读出 SOAP 标头),此方法应该可以满足您在从 WCF 客户端调用 ASMX 服务时包含 SOAP 标头的要求:
我希望这可行 - 我真的没有基础设施现在就在手头测试一下......
Based on another question I just answered (about reading out SOAP headers), this approach should work for your requirement of including a SOAP header when calling an ASMX service from a WCF client:
I hope this works - I don't really have the infrastructure at hand to test this right now...
此时您的问题看起来根本与 SOAP 标头无关...“无法获取计算机名称”错误来自其他地方。
您在客户端上指定什么作为服务 URL?服务与客户端运行在同一台机器上,还是不同的机器上?该服务是否可以与其他非 WCF 客户端一起使用?
It doesn't look like your problem at this point is related with the SOAP Header at all... that "Computer name could not be obtained" error comes from somewhere else.
What are you specifying on the client as the service URL? Is the service running on the same machine as the client, or a different one? Is the service working with other, non-WCF clients?