WCF DataContractSerializer无法获取合同属性...为什么不呢? [英] WCF DataContractSerializer doesn't pick up contract attributes... why not?

查看:88
本文介绍了WCF DataContractSerializer无法获取合同属性...为什么不呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类型,可以在WCF中用作消息合同:

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

当我使用 svcutil.exe 生成这种类型的代理时,我得到了一个客户端,该客户端能够与托管它的服务进行通信,并且元素上的XML名称空间根据邮件合同属性.

When I generate a proxy to this type using svcutil.exe, I get a client which is able to communicate to a service which hosts it, with the XML namespaces on the elements correct according to the Message Contract attributes.

当我在其实例上使用Message.CreateMessage(...)时,名称空间将还原为默认名称( http://schemas.datacontract.org/2004/07/.. .).当我使用DataContractSerializer的实例时,会发生相同的事情.我尝试将名称空间传递给DataContractSerializer构造函数,并且只有包装器包含在名称空间中:

When I use Message.CreateMessage(...) on an instance of it, the namespaces revert to the default (http://schemas.datacontract.org/2004/07/...). When I use an instance of DataContractSerializer, the same thing happens. I try to pass a namespace to the DataContractSerializer constructor, and only the wrapper gets 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());

此时,数据"为:

<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 如何完成这项工作?

Why does the DataContractSerializer appear to ignore the MessageContract attributes? How does svcutil get this work?

推荐答案

这是因为消息合同不是数据合同,所以数据合同使用不同的属性来标记其类.尝试使用类型化的消息转换器;

It's because message contracts are not data contracts, data contracts use different attributes to mark their classes. Try using a typed message converter;

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

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

然后您将准备好一条消息,可以根据需要拉出请求正文.

You'll then have a message all ready to go and can pull the request body out if you need to.

这篇关于WCF DataContractSerializer无法获取合同属性...为什么不呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆