WCF DataContractSerializer does not take contract attributes ... why not?

I have the following type, which I use as a message contract in WCF:

[MessageContract(IsWrapped = true, 
                 WrapperNamespace = "http://example.com/services", 
                 WrapperName = "EchoRequest")]
public class EchoRequest
{
    public EchoRequest() { }
    public EchoRequest(String value)
    {
        Value = value;
    }

    [MessageBodyMember(Name = "Value", 
                       Namespace = "http://example.com/services", 
                       Order = 0)]
    public String Value { get; set; }
}

When I create a proxy server for this type using svcutil.exe, I get a client that can contact the service that hosts it, while the XML namespaces on the elements are correct in accordance with the attributes of the Message Contract.

When I use Message.CreateMessage(...)in its instance, namespaces revert to default ( http://schemas.datacontract.org/2004/07/ ... ). When I use an instance DataContractSerializer, the same thing happens. I am trying to pass a namespace to a constructor DataContractSerializer, and only the shell is included in the namespace:

var requestMessage = new EchoRequest("hello, world!");
var serializer = new DataContractSerializer(typeof(EchoRequest), 
                                            "EchoRequest", 
                                            "http://example.com/services");
var stream = new MemoryStream();
serializer.WriteObject(stream, requestMessage);
var data = Encoding.UTF8.GetString(stream.ToArray());

In this case, the "data":

<EchoRequest xmlns="http://example.com/services"
             xmlns:a="http://schemas.datacontract.org/2004/07/TestClient"
             xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:Value>hello, world!</a:Value>
</EchoRequest>

DataContractSerializer MessageContract? svcutil ?

+4
1

, , . ;

EchoRequest echoRequest = new EchoRequest{ value = "Hello" };

TypedMessageConverter echoMessageConverter = TypedMessageConverter.Create(
                 typeof(echoRequest),
                 "YourActionNameHere",
                 "http://example.com/services");
Message request = echoMessageConverter.ToMessage(
    echoRequest,MessageVersion.Soap11);

, , , .

+4

All Articles