繁体   English   中英

WCF客户端使用带有Soap Header的ASMX Web服务

[英]WCF Client consuming ASMX Web Service with Soap Header

我有一个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参考。

基于我刚刚回答的另一个问题(关于读取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});
}

我希望这行得通-我现在还没有真正的基础设施来测试这一点...

目前看来您的问题根本与SOAP Header有关……“无法获取计算机名称”错误来自其他地方。

您在客户端上指定什么作为服务URL? 服务与客户端在同一台计算机上运行还是在另一台计算机上运行? 该服务是否可以与其他非WCF客户端一起使用?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM