Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties in a response object after a webservice call are null

I can see the object in Fiddler but the object is not deserializing on my end. Has anyone seen this before?

like image 900
Sean Avatar asked Dec 28 '10 15:12

Sean


3 Answers

"Response is null" or "Response contains nulls" or "Request is null" or "Request contains null" almost always mean that you have a namespace mismatch. For instance, the response may contain:

<response xmlns="http://foo.com"/>

but should in fact be

<response xmlns="http://bar.com"/>

In this case, null will be received.

like image 61
John Saunders Avatar answered Nov 13 '22 12:11

John Saunders


I had a similar case where I created a client via SVCUTIL / Service Reference from VS. The response was received successfully with correct data (confirmed via IClientMessageInspector.AfterReceiveReply method) however the values at the object level were not being populated. There were no de-serialization errors (confirmed via system.diagnostics output)

The problem was twofold:

1) Certain objects were named exactly as their types but had different namespaces from their types. This seems to have confused the proxy generator in assigning the namespace parameter (in the System.Xml.Serialization.XmlElementAttribute annotation) of the class to the one of the object

2) The order parameter (in the System.Xml.Serialization.XmlElementAttribute annotation) of the properties was not required and also the namespace parameter was missing

so from: [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=0)]

to: [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Namespace="http://www.whathevernamespaceiscorrect.com")]

So basically, in the generated proxy I needed to fix the namespace of the class to the one specified in the type, and replace the order parameter with the namespace parameter setting it to the correct namespace according to the wsdl

like image 38
mrd3650 Avatar answered Nov 13 '22 12:11

mrd3650


I had the same problem, and as suggested the namespace problem was the root cause.However, my proxy class has nested classes and long chain of nested namespace.

It was confusing to identify the correct namespace to apply in Cs code for proxy class. Here, i describe how to figure out the namespace which is required to be updated in client proxy.

What i did was intercept the request in ClientMessageInspector class, AfterReceiveReply method (Enables inspection or modification of a message after a reply message is received but prior to passing it back to the client application.) Verified the namespace of the object which were returning null in Response by using XMLDocument. I updated the proxy class with the namespace retreived from XML. After making the changes, the objects were not null in response.

 public class MyMessageInspector : IClientMessageInspector
{
    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message request, object correlationState)
    {

        MemoryStream ms = new MemoryStream();
        XmlWriter writer = XmlWriter.Create(ms);
        request.WriteMessage(writer); 

        writer.Flush();
        ms.Position = 0;
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(ms);
        this.ReadMessage(xmlDoc);


        ms = new MemoryStream();
        xmlDoc.Save(ms);
        ms.Position = 0;
        XmlReader reader = XmlReader.Create(ms);
        Message newMessage = Message.CreateMessage(reader, int.MaxValue, request.Version);
        newMessage.Properties.CopyProperties(request.Properties);
        request = newMessage;

    }

    private void ReadMessage(XmlDocument xmlDoc)
    {
        XmlNode v1 = xmlDoc.GetElementsByTagName("XPAth");
        //Actual Namespace in XML, which should be used in Proxy Class
        string namespaceURIForObjectInXML = v1.NamespaceURI;
    }

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
    {

    }



}
like image 29
Abhinav Galodha Avatar answered Nov 13 '22 13:11

Abhinav Galodha