设置使用ContractNamespace属性WCF DataContract命名空间 [英] Setting WCF DataContract namespace using ContractNamespace attribute

查看:637
本文介绍了设置使用ContractNamespace属性WCF DataContract命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计我的服务,我决定,我想自定义出现在生成的WSDL的命名空间。

In designing my service, I decided that I wanted to customize the namespaces that appeared in the resulting WSDL.

对于DataContracts,我遇到了<一href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.contractnamespaceattribute.aspx"相对=nofollow> ContractNamespace 属性,它似乎是一个很好的快捷方式替代明确设置了相同的命名空间为每个DataContract。我最初的尝试是这样的:

For the DataContracts, i came across the ContractNamespace attribute which seemed a nice shortcut alternative to setting the same namespace explicitly for every DataContract. My initial attempt looked like this:

[assembly:ContractNamespace("http://types.mycompany.com/2010/08/03")]
namespace MyCompany.MyContracts
{
    [DataContract]
    //...multiple datacontract classes here
}

要我吃惊的是,这没有奏效。经过一番摆弄,我是唯一成功的时候,我终于将 ClrNamespace 属性等于我的CLR命名空间(在本例中MyCompany.MyContracts)的财产。因此,像这样

To my surprise, this did not work. After much tinkering, I was only successful when I finally set the ClrNamespace property of the attribute equal to my CLR namespace (MyCompany.MyContracts in the example). So something like this

[assembly:ContractNamespace("http://types.mycompany.com/2010/08/03",
          ClrNamespace="MyCompany.MyContracts")]

我的问题是:为什么这不工作的第一种方式?我的预期是,不指定一个CLR namepsace,此属性将影响所有datacontracts组装范围。

My question is: why did this not work the first way? My expectation was that by not specifying a CLR namepsace, this attribute would affect all datacontracts assembly-wide.

推荐答案

如果我没有记错,如果 ClrNamespace 被省略,则合同的命名空间设置适用于全局命名空间中的对象。

If I'm not mistaken, if the ClrNamespace is ommitted, then the contract namespace setting applies to objects in the global namespace.

这篇关于设置使用ContractNamespace属性WCF DataContract命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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