0

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.Serializable]
 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.

Community
  • 1
  • 1
FXB
  • 1
  • 1
  • 2

1 Answers1

0

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();
    }
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • That is typically what svcutil generate as adapter, but with messageContract you can't use optional fields. Every message bodies will be sent to the service. My need is like the referenced post (http://stackoverflow.com/questions/7557887/xmlserializer-with-specified-pattern-not-working), where "degorolls" suggests to not use MessageContract. – FXB Oct 03 '11 at 06:53