tags:

views:

6

answers:

1

Hi everyone..

I have a WCF service that uses Message contract for request and replies of data. This is the service contract :

 [ServiceContract()]
 public interface ISalesFinderService
 {
     [OperationContract()]
     Response DoSomething(Request i_Info);
 }

The problem occurs when the service returns a response. This is the Response Message:

[MessageContract()]
public class ObjectInfo
{
    [MessageBodyMember(Name = "Name")]
    public string  m_Name;
}

[MessageContract()]
public class Response
{
   private List<ObjectInfo> m_MyListOfObjects;

   public FindSalesByLocationInfoMessageResponse()
   {
       m_MyListOfObjects = new List<ObjectInfo>();
   }

   [MessageBodyMember(Name = "ObjectsInfo")]
   public List<ObjectInfo> MyListOfObjects
   {
            get { return m_MyListOfObjects;  }
            set { m_MyListOfObjects = value; }
   }
}

When the response comes out I can see (through wireshark) that all the fields inside the list (of type ObjectInfo) were serialized to XML and got the tag <a:m_Name></a:m_Name> although I added specifically MessageBodyMember attribute above the m_Name data member and gave it "Name" as it's tag.

How can I change those tags names as I wish ???

thanks..

+1  A: 

I think you're misunderstanding the reason for the message contract and how to use it. Check out the MSDN documentation on Using Message Contracts for a more thorough discussion.

The message contract allows you to define a layout of your message with header(s) and body elements. Those can be atomic types like string or int, and if not, you need to define composite types as classes.

Those composite parts (classes) that you use in that message contract ought to be marked as [DataContract] with [DataMember] attributes on the individual fields you want to serialize.

Furthermore, your message contract and data contracts should never contain any functions and anything that needs to execute. The data structures will be serialized as data only, any behavior will be lost in the transfer.

So in your case, you should try:

[MessageContract()]
public class Response
{
    private List<ObjectInfo> m_MyListOfObjects = new List<ObjectInfo>();

    [MessageBodyMember(Name = "ObjectsInfo")]
    public List<ObjectInfo> MyListOfObjects
    {
        get { return m_MyListOfObjects;  }
        set { m_MyListOfObjects = value; }
    }
}

[DataContract]
public class ObjectInfo
{
    [DataMember(Name = "Name")]
    public string m_Name;
}
marc_s
Thank you very much , you solved the name issue , but each member inside the list still gets the "a" prefix <a:Name></a:Name>i can see that through the message log (svcTraceviewer - information level trace) , how can i get rid of that "a"prefix ??thanks.
Liran
That "a:" prefix must be part of a XML namespace of some sort. See if you find an XML namespace definition somewhere in your XML message - something like `xmlns:a=.......`. But why does that bother you?? It really shouldn't be any kind of a problem, I think.....
marc_s