2010-09-30 143 views
1

我有一个ASMX Web服务,它需要一个SOAP头和一个客户端应用程序通过服务引用(WCF)使用此服务。WCF客户端使用带SOAP头的ASMX Web服务

服务器:

[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

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

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

+0

客户端的URL是“http://MyServer/Services/Service1.asmx”。为了开发,客户端与服务在同一台机器上运行。但是,在生产中它不会。我有一种感觉,我的直觉是这是一个环境问题,而不是WCF问题,但如果我们可以在同一个盒子上运行服务和客户端用于开发/测试目的,那将是非常好的。该服务对非WCF客户端工作正常。 – Adam 2010-10-01 13:21:42

+0

因为两者都在同一台计算机上,所以如果您使用localhost而不是MyServer指定URL,它是否工作?听起来像一个DNS问题可能会阻碍。 – tomasr 2010-10-01 14:50:10

+0

刚刚尝试将端点切换到本地主机。同样的例外。 – Adam 2010-10-01 16:16:36

1

基于另外一个问题我刚才已经回答(约读出SOAP头),这种方法应该为你的包括SOAP头的要求,从调用WCF客户端的ASMX服务时工作:

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

希望这是有效的 - 我现在没有真正的基础架构来测试这个...

+0

刚试过这个。同样的例外。你能解释CreateHeader方法的参数吗?也许我把错误的东西放在我的实际实现中。我看起来像MessageHeader.CreateHeader(_header.GetType()。Name,“urn:Sample-NS”,_header); – Adam 2010-10-04 14:51:34