参数序列化-指定名称空间

时间:2019-01-23 08:10:38

标签: c# wcf namespaces

我有一个WCF服务器,该服务器的调用包含2个参数,但是每个参数都需要一个不同的命名空间。我似乎无法为参数指定名称空间。

因此发送到服务器的呼叫(我无法适应):

<ns1:myWCFCall
  xmlns:ns1="testNameSpace1"
  >
  <ns1:firstParam>
     <ns1:a></ns1:a>
     <ns1:b></ns1:b>
  </ns1:firstParam>
  <ns2:secondParam
    xmlns:ns2="testNameSpace2">
     <ns2:a></ns2:a>
     <ns2:a></ns2:a>
  </ns2:secondParam>
</ns1:myWCFCall>

我创建了一个接口+实现。由于标准序列化程序的其他一些限制,我需要使用XMLSerialization。 在参数级别(包括名称空间)上指定XMLElement似乎根本不起作用。

    [ServiceContract(Namespace = "testNameSpace1")]
    [XmlSerializerFormat]
    public interface ITestService
    {
        [OperationContract]
        myResponseObject myWCFCall(
            [XmlElement(Namespace = "testNameSpace1")] myObject firstParam, 
            [XmlElement(Namespace = "testNameSpace2")] myOtherObject secondParam);
    }

//Implementation
    public class Service1 : IService1
    {

        public myResponseObject myWCFCall(
            [XmlElement(Namespace = "testNameSpace1")] myObject firstParam, 
            [XmlElement(Namespace = "testNameSpace2")] myOtherObject secondParam)
        {
            return new myResponseObject();
        }
     }

//Sample classes
    [Serializable]
    public class myObject
    {
        [XmlElement]
        public string a;
        [XmlElement]
        public string b;
    }

//tried putting this in another C# namespace, no difference in the results.
    [Serializable]
    public class myOtherObject
    {
        [XmlElement]
        public string a;
        [XmlElement]
        public string b;
    }


添加[XmlRoot(Namespace =“ testNameSpace2”)]不适用于第一级。仅适用于后续级别(a,b)

因此,当我检查WSDL时,我总是得到错误的结果...(ns1:secondParam而不是ns2:secondParam):

<ns1:myWCFCall
  xmlns:ns1="testNameSpace1"
  >
  <ns1:firstParam>
     <ns1:a></ns1:a>
     <ns1:b></ns1:b>
  </ns1:firstParam>
  <ns1:secondParam
    xmlns:ns2="testNameSpace2">
     <ns2:a></ns2:a>
     <ns2:a></ns2:a>
  </ns1:secondParam>
</ns1:myWCFCall>

帮助。

1 个答案:

答案 0 :(得分:1)

我能想到的唯一方法是使用MessageContract,它可以在根级别控制参数的命名空间。 XmlRoot由于某种原因无法更改参数的根元素,请参考下面的链接

Why does the XmlRoot attribute gets ignored in WCF and how to overcome this

但是在您的情况下,您有两个参数,但是messagecontract只能有一个参数。因此,我建议您可以按以下方式使用合同。

public class TheFirst
{
    [MessageBodyMember]
    public string AOfTheFirst { get; set; }
    [MessageBodyMember]
    public string BOfTheFirst { get; set; }

}

public class TheSecond
{
    [MessageBodyMember]
    public string AOfTheFirst { get; set; }
    [MessageBodyMember]
    public string BOfTheFirst { get; set; }

}

[MessageContract(IsWrapped = false)]  //IsWrapped= "false" removes the OuterClasselement in the request element
public class OuterClass{
    [MessageBodyMember(Namespace ="www.thefirst.com",Name ="aliasForFirst")]
    public TheFirst TheFirst { get; set; }
    [MessageBodyMember(Namespace ="www.thesecond.com",Name ="aliasForSecond")]
    public TheSecond TheSecond { get; set; }




}

我的合同。

  [ServiceContract]

public interface IXmlSerService
{
    [OperationContract]

    OuterClass wcfCll(OuterClass outerClass);
}

MyService

 public  class XmlSerService:IXmlSerService
{



    public OuterClass wcfCll(OuterClass outerClass)
    {

        return new OuterClass { TheFirst = new TheFirst { AOfTheFirst = "a", BOfTheFirst = "b" },TheSecond = new TheSecond { AOfTheFirst = "a", BOfTheFirst = "b" } };
    }


}

结果。   enter image description here

相关问题