我有一个 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:服务器无法处理请求。---> 无法获得计算机名。 服务器堆栈跟踪: 在 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime 操作,ProxyRpc& rpc) 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出) 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime 操作) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)
如何在仍然使用 WCF 的客户端解决此问题?我无法更改服务器代码,我需要在客户端使用 WCF,我无法添加 Web 引用。