Reputation: 43
I have a WCF server which has a call having 2 parameters, but which require a different namespace for each parameter. I can't seem to specify the namespace for the parameters.
So the call which is sent to the server (which I cannot adapt):
<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>
I created a interface + implementation. I need to use XMLSerialization due to some other limitations of the standard serializer. Specifying XMLElement on parameter level (including the namespace) doesn't seem to work at all.
[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;
}
Adding [XmlRoot(Namespace = "testNameSpace2")] doesn't work for the first level. Only for sub-sequent levels (a, b)
So I keep getting the wrong result when I check the WSDL... (ns1:secondParam instead of 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>
Help.
Upvotes: 0
Views: 300
Reputation: 776
The only way I could think out is to use MessageContract, which could control the namespace of parameter at the root level. XmlRoot for some reason couldn't change the root element of your parameter , please refer to the link below
Why does the XmlRoot attribute gets ignored in WCF and how to overcome this
But in your case, you have two parameters, but messagecontract could only has one parameter. So I suggest you could use the contract as follows.
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; }
}
MyContract.
[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" } };
}
}
Upvotes: 1