1

我有一个 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 引用。

4

2 回答 2

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});
}

我希望这行得通——我现在手头还没有基础设施来测试它……

于 2010-10-01T21:05:02.877 回答
0

在这一点上,您的问题看起来与 SOAP 标头根本无关......“无法获得计算机名称”错误来自其他地方。

您在客户端上指定什么作为服务 URL?服务是在与客户端相同的机器上运行,还是在不同的机器上运行?该服务是否与其他非 WCF 客户端一起使用?

于 2010-09-30T23:05:41.083 回答