Search code examples
xmlwcfsoapdatacontractserializer

create soap message from Data Contract


I have a WCF service and I consume it using c#. Basically I add the service reference and consume service operations through it.

This service is being exposed using basicHttpBinding. I would like to allow soapBinding too and give some samples of the soap to my clients.

Is it possible to get soap xml from DataContract class? I mean, I would like to somehow, get xml using my existing code.

[DataContract]
public class Foo
{
    [DataMember]
    public int Id {get;set;}

    [DataMember]
    public string Name {get;set;}
}



[ServiceContract]
public interface IService
{
    [OperationContract]
    bool Import(IEnumerable<Foo> foos);
}

c# client:

var client = new ws.ServiceClient();

var foo = new Foo{
    Id = 1,
    Name = "Test"
};


client.Import(new[] { foo });

Just in case I was not clear enough, I would like to get soap xml reusing code above.


Solution

  • If you just want a plain request for documentation or something you can use WCF tracing.

    Simple and generic soulution for all yours services.

    At your system.serviceModel

    <system.serviceModel>
    ...
        <diagnostics>
          <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" maxMessagesToLog="3000" />
        </diagnostics>     
    ....
      </system.serviceModel>
    

    and add system.diagnostics element

    <system.diagnostics>
        <switches>
          <add name="XmlSerialization.Compilation" value="4"/>
      </switches>
       <sources>
          <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
          <source name="System.ServiceModel.MessageLogging">
            <listeners>
              <add name="xml" />
            </listeners>
          </source>
        </sources>
        <sharedListeners>
          <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\temp\WCFLoging.svclog" />
        </sharedListeners>
      </system.diagnostics>
    

    Then after a service call check service log at (c:\temp\WCFLoging.svclog) there will be a plain request.

    Or if you want to get it in your client code. Use WCF message inspector. Sample use http://www.primaryobjects.com/CMS/Article121