开发者

WCF Message Contract namespace issue

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 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="GetUserRig开发者_如何学PythonhtsResponse"/> 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 is the constant string for namespace and it's value is urn:MyNamespace:Authorization

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>

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?


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" 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).

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.


You are not getting wrong namespace, as far as I can see.

All elements are set to "urn:MyNamespace:Authorization" which I imagine is the AuthorizationXmlElementNames.Namespace.

xmlns:a="http://schemas.datacontract.org/2004/07/MyMessageContract.MessageContracts" points to Microsoft's schema for message contracts. Now this does not seem to have been used in the snippet you published, so I am surprised it is there at all but this is an innocent namespace since you do not have any element or attribute in that namespace.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜