WCF 消息协定命名空间问题 [英] WCF Message Contract namespace issue

查看:31
本文介绍了WCF 消息协定命名空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 MessageContract

I have following MessageContract

 [MessageContract(IsWrapped = true, WrapperName = AuthorizationXmlElementNames.ElementNames.GetUserRightsResponse, 
        WrapperNamespace = AuthorizationXmlElementNames.Namespace)]
    public class GetUserRightsResponseMessage
    {
        #region Properties

        /// <summary>
        /// Gets or sets GetUserRightsResponse.
        /// </summary>
        [MessageBodyMember(Namespace = AuthorizationXmlElementNames.Namespace)]
        public GetUserRightsResponse GetUserRightsResponse { get; set; }

        /// <summary>
        /// Gets or sets ResponseHeader.
        /// </summary>
        [MessageHeader(
            Name = XmlCoreElementNames.ElementNames.ResponseHeader, 
            Namespace = XmlCoreElementNames.Namespace,
            ProtectionLevel = ProtectionLevel.None)]
        public ResponseHeader ResponseHeader { get; set; }

        #endregion
    }

GetUserRightsResponse 类看起来像这样

GetUserRightsResponse class looks like this

[XmlRoot(ElementName = AuthorizationXmlElementNames.ElementNames.GetUserRightsResponse, 
        Namespace = AuthorizationXmlElementNames.Namespace, IsNullable = false)]
    [Serializable]
    //[MessageContract(WrapperName = AuthorizationXmlElementNames.ElementNames.GetUserRightsResponse, WrapperNamespace = AuthorizationXmlElementNames.Namespace)]
    public class GetUserRightsResponse
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="GetUserRightsResponse"/> class. 
        /// Initialize a new instance of the <see cref="GetUserRightsResponse"/> class.
        /// </summary>
        public GetUserRightsResponse()
        {
            this.UserServiceAccesses = new UserServiceAccesses();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the user rights for the current user.
        /// </summary>
        //[MessageBodyMember(Namespace = AuthorizationXmlElementNames.Namespace)]
        public UserServiceAccesses UserServiceAccesses { get; set; }

        #endregion
    }

XmlCoreElementNames.Namespace 是命名空间的常量字符串,它的值为 urn:MyNamespace:Authorization

XmlCoreElementNames.Namespace is the constant string for namespace and it's value is urn:MyNamespace:Authorization

我从我的操作合同中返回了一个 GetUserRightsResponseMessage 的实例.但是对于包含在返回对象中的 GetUserRightsResponse 实例,我总是得到错误的命名空间.返回的 XML 部分如下所示.

I return an instance of the GetUserRightsResponseMessage from my operation contract. But I always get wrong namespace for the GetUserRightsResponse instance contained in the returned object. The returned XML part looks like the following.

<s:Body u:Id="_0">
   <GetUserRightsResponse xmlns="urn:MyNamespace:Authorization">
      <GetUserRightsResponse i:nil="true" 
            xmlns:a="http://schemas.datacontract.org/2004/07/MyMessageContract.MessageContracts" 
            xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
   </GetUserRightsResponse>
</s:Body>

为什么我总是为返回的对象中的属性获取命名空间 xmlns:a="http://schemas.datacontract.org/2004/07?xmlns:a 是什么意思?

Why do i always get the namespace xmlns:a="http://schemas.datacontract.org/2004/07 for the property in the returned object? What does xmlns:a mean?

推荐答案

两个 GetUserRightsResponse 元素都在命名空间 "urn:MyNamespace:Authorization" 中,因为默认命名空间声明外层由内层继承.

Both GetUserRightsResponse elements are in namespace "urn:MyNamespace:Authorization", because the default namespace declaration on the outer one is inherited by the inner one.

xmlns:a="http://schemas.datacontract.org/2004/07/MyMessageContract.MessageContracts" 是一个命名空间声明,它定义了一个命名空间前缀 ("a"),它不是't 在您的消息中实际使用.所以它对 XML 消息的含义完全没有影响,可以省略.它的存在是 Microsoft 实现的一个怪癖,可能是由您的消息包装元素名称和数据协定名称之间的命名冲突触发的(但我只是在猜测这一点).

xmlns:a="http://schemas.datacontract.org/2004/07/MyMessageContract.MessageContracts" is a namespace declaration which defines a namespace prefix ("a") which isn't actually used in your message. So it has no effect at all on the meaning of the XML message and could be omitted. Its presence is a quirk of the Microsoft implementation, perhaps triggered by the naming collision between your message wrapper element name and the data contract name (but I'm just guessing about this).

如果您的客户完全符合 XML,那么这对您来说根本不成问题.然而,有一些不兼容的 SOAP 客户端工具集对命名空间声明的处理很挑剔.如果您非常不走运,您可能会发现自己被未使用的命名空间前缀声明弄糊涂了.

If your clients are fully XML compliant, this shouldn't be a problem for you at all. However, there are some non-compliant SOAP client toolsets around which are fussy about the treatment of namespace declarations. If you are very unlucky you might find you have one which is confused by an unused namespace prefix declaration.

这篇关于WCF 消息协定命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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