Search code examples
c#wcfnullresponseproducer-consumer

WCF client receiving null response value


I have a simply wcf consumer client which sends "echo" requests to a 3rd party Java based producer. I have struggled for some time to get the WS-Security functioning correctly but can now send requests and receive replies. Only when the reply reaches my client, the value is null.

I have used IClientMessageInspector to verify I have the correct reply, but my call to the service always returns null.

Here is my client code; (The call to "MyEndPointBehavior" simply adds the message inspector)

string echoString = EchoTesttextbox.Text;

string url = "https://somewhere.com/uk-producer/UKService";

// create the endpoint address
EndpointAddress endPointAddress = new EndpointAddress(new Uri(url), EndpointIdentity.CreateDnsIdentity("TestProducer"));

PortBindingClient client = new PortBindingClient("Port12", endPointAddress);
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.Root, X509FindType.FindByThumbprint, ".....");
client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.LocalMachine, StoreName.TrustedPeople, X509FindType.FindByThumbprint, ".....");
client.Endpoint.Behaviors.Add(new MyEndpointBehavior());

string response = String.Empty;
response = client.echo(echoString);

MessageBox.Show(response);

And my message inpector code;

public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
     // Implement this method to inspect/modify messages after a message
     // is received but prior to passing it back to the client

     // make a copy of the reply that we can play with
     MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
     // the message variable holds a reference to a copy of the initial reply
     Message message = buffer.CreateMessage();

     // assign a copy back to the ref received so it can continue undisturbed
     reply = buffer.CreateMessage();

     StringWriter stringWriter = new StringWriter();
     XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
     message.WriteBody(xmlTextWriter);
     xmlTextWriter.Flush();
     xmlTextWriter.Close();

     string body = stringWriter.ToString();

}

My question is what is happening between IClientMessageInspector.AfterReceiveReply and trying to show the response in my MessageBox? If I can try and understand what is happening I can maybe try and work out why the reponse isn't passing through.


Solution

  • Well that was very frustrating. The reason I was receiving a null value was all to do with the namespace.

    Within IClientMessageInspector I could examine the SOAP body and see this

    <soapenv:Body wsu:Id="Id-1555290177" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <me:EchoResponse xmlns:me="http://somewhere.com/uk-producer/UKService">test11</me:EchoResponse>
    </soapenv:Body>
    

    The WSDL and reference.cs (I've generated my proxy using the "Add Service Reference" feature of VS2012 from the 3rd party WSDL) show this;

    [System.ServiceModel.MessageBodyMemberAttribute(Name="EchoResponse", Namespace="http://somewhere.com/uk-producer/UKService/", Order=0)]
    public string EchoResponse1;
    

    And for a long time that all looked ok. But then, out of frustration I tried altering the reference.cs to this

    [System.ServiceModel.MessageBodyMemberAttribute(Name="EchoResponse", Namespace="http://somewhere.com/uk-producer/UKService", Order=0)]
    public string EchoResponse1;
    

    I only removed the last forward slash of the namespace

    And it all worked.