我有一个ASMX Web服务,它需要一个soap标头和一个通过服务引用(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引用。
答案 0 :(得分:1)
根据我刚才回答的另一个问题(关于读取SOAP头),这种方法应该适用于在从WCF客户端调用ASMX服务时包含SOAP头的要求:
Service1SoapClient client = new Service1SoapClient(binding, address);
using(OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
// set the message in header
MessageHeader header = MessageHeader.CreateHeader("MyHeader", "urn:Sample-NS", "Some Value");
OperationContext.Current.OutgoingMessageHeaders.Add(header);
string expected = client.MyMethod(new MyCustomSoapHeader(){SomeProperty = true});
}
我希望这有效 - 我现在没有真正的基础设施来测试这个......
答案 1 :(得分:0)
此时看起来您的问题与SOAP Header完全无关......“无法获取计算机名称”错误来自其他地方。
您在客户端上将哪些内容指定为服务URL?服务是在与客户端相同的计算机上运行还是在另一台计算机上运行?该服务是否与其他非WCF客户端一起使用?