参数序列化——指定命名空间
Parameter serialization — specify namespace
我有一个 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>
求助。
我能想到的唯一方法是使用MessageContract,它可以在根级别控制参数的命名空间。
由于某些原因,XmlRoot 无法更改您的参数的根元素,请参考下面的 link
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);
}
我的服务
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" } };
}
}
结果。
我有一个 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>
求助。
我能想到的唯一方法是使用MessageContract,它可以在根级别控制参数的命名空间。 由于某些原因,XmlRoot 无法更改您的参数的根元素,请参考下面的 link
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);
}
我的服务
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" } };
}
}
结果。