1
votes

I have an ASMX web service that has requires a soap header and a client app consuming this service via a service reference (WCF).

Server:

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

Client:

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

Stack trace:

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)

How can I fix this on the client side still using WCF? I am unable to change the server code and I need to use WCF on the client, I can't add a web reference.

2

2 Answers

1
votes

Based on another question I just answered (about reading out SOAP headers), this approach should work for your requirement of including a SOAP header when calling an ASMX service from a WCF client:

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

I hope this works - I don't really have the infrastructure at hand to test this right now...

0
votes

It doesn't look like your problem at this point is related with the SOAP Header at all... that "Computer name could not be obtained" error comes from somewhere else.

What are you specifying on the client as the service URL? Is the service running on the same machine as the client, or a different one? Is the service working with other, non-WCF clients?