Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WCF消息约定命名空间问题_Wcf - Fatal编程技术网

WCF消息约定命名空间问题

WCF消息约定命名空间问题,wcf,Wcf,我有一份合同 [MessageContract(IsWrapped = true, WrapperName = AuthorizationXmlElementNames.ElementNames.GetUserRightsResponse, WrapperNamespace = AuthorizationXmlElementNames.Namespace)] public class GetUserRightsResponseMessage {

我有一份合同

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

为什么我总是得到名称空间
xmlns:a=”http://schemas.datacontract.org/2004/07
返回对象中的属性?就我所见,
xmlns:a
是什么意思?

您没有得到错误的名称空间

所有元素都设置为
“urn:MyNamespace:Authorization”
,我想这就是
AuthorizationXmlElementNames.Namespace


xmlns:a=”http://schemas.datacontract.org/2004/07/MyMessageContract.MessageContracts“
指向Microsoft的邮件合约模式。现在,您发布的代码段中似乎没有使用这个名称空间,所以我很惊讶它竟然存在,但这是一个无辜的名称空间,因为您在该名称空间中没有任何元素或属性。

这两个
GetUserRightsResponse
元素都在名称空间
“urn:MyNamespace:Authorization”
,因为外部名称空间的默认名称空间声明由内部名称空间继承

xmlns:a=”http://schemas.datacontract.org/2004/07/MyMessageContract.MessageContracts“
是一种名称空间声明,它定义了一个名称空间前缀(“a”),而实际上在您的消息中并未使用该前缀。因此,它对XML消息的含义没有任何影响,可以省略。它的出现是Microsoft实现的一个怪癖,可能是由消息包装器元素名称和数据契约名称之间的命名冲突触发的(但我只是猜测一下)


如果您的客户机完全兼容XML,这对您来说应该不会有任何问题。然而,有一些不兼容的SOAP客户机工具集对名称空间声明的处理非常挑剔。如果您非常不走运,您可能会发现有一个名称空间前缀声明未被使用而混淆。

谢谢您的回复。为什么会自动添加?我记得在任何地方都没有具体说明。
xmlns:a
xmlns:i
是什么意思以及何时使用它们?我的客户是ASMX客户,现在正在调用WCF服务。我也需要在客户端检查一些东西吗?这是执行此操作的
序列化程序
xmlns:i
是必需的,因为它是Xml实例名称空间,定义了
i:nil
i:any
等值。您的猜测太棒了!我正在使用
GetUserRightsResponseMessage
中的属性,该属性返回数据协定的
GetUserRightsResponse
实例(但是使用XmlSerializer与ASMX客户端向后兼容。我能做些什么来避免这种情况?我正在将现有的WSE3 web服务移动到WCF,使它们与现有客户端向后兼容。您是否发现有可能成为不遵守SOAP的客户端(因为WSE3)?我不知道有哪种客户端SOAP工具包可以与WSE3愉快地互操作,但不能正确理解名称空间,所以您可能还可以。
[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
    }
<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>