WCF XmlSerializerFormat and OperationContract: nested soap parameters serialization
After reading the post XmlSerializer with specified pattern not working I try to implement such a service : OperationContract with the XmlSerializerFormat. But my Soap message contains an additional tag that is the operation parameter. How can I remove that tag ?
Here is my service sample
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://mynamespace.com/", ConfigurationName = "ConfigName")]
public interface MyInterfacePort
{
[System.ServiceModel.OperationContractAttribute(Action = "http://mynamespace.com/opName", ReplyAction = "*")]
[System.ServiceModel.FaultContractAttribute(typeof(MyError), Action = "http://mynamespace.com/opName", Name = "opErr")]
[System.ServiceModel.XmlSerializerFormatAttribute()]
opResponse opName(opRequest request);
Then the serialized request:
[System.Seriali开发者_StackOverflowzable]
public partial class opRequest
{
public string myProperty;
generated soap message:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<opName xmlns="http://mynamespace.com/">
<request>
<myProperty>262157</myProperty>
</request>
</opName>
</s:Body>
</s:Envelope>
My service doesn't handle the additional <request> tag
Thank's for your help.
You can use an unwrapped [MessageContract]
class if you want to remove the additional element in the XML request. The code below shows your example with such contract.
public class StackOverflow_7607564
{
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://mynamespace.com/", ConfigurationName = "ConfigName")]
public interface MyInterfacePort
{
[System.ServiceModel.OperationContractAttribute(Action = "http://mynamespace.com/opName", ReplyAction = "*")]
[System.ServiceModel.FaultContractAttribute(typeof(MyError), Action = "http://mynamespace.com/opName", Name = "opErr")]
[System.ServiceModel.XmlSerializerFormatAttribute()]
opNameResponse opName(opNameRequest request);
}
public class MyError { }
[MessageContract(IsWrapped = false)]
public class opNameRequest
{
[MessageBodyMember(Name = "opName")]
public opRequest request;
}
[MessageContract(IsWrapped = false)]
public class opNameResponse
{
[MessageBodyMember(Name = "opNameResponse")]
public opResponse response;
}
[System.Serializable]
public partial class opRequest
{
public string myProperty;
}
[System.Serializable]
public partial class opResponse
{
public string myProperty;
}
public class Service : MyInterfacePort
{
public opNameResponse opName(opNameRequest request)
{
return new opNameResponse { response = new opResponse { myProperty = request.request.myProperty } };
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(MyInterfacePort), new BasicHttpBinding(), "");
host.Open();
Console.WriteLine("Host opened");
var factory = new ChannelFactory<MyInterfacePort>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
var proxy = factory.CreateChannel();
Console.WriteLine(proxy.opName(new opNameRequest { request = new opRequest { myProperty = "hello world" } }).response.myProperty);
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
精彩评论