Search code examples
.netwcf

How to configure the datetime format in a System.ServiceModel.ClientBase


I'm trying to call a web service with a call like follows. It fails.

<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">
        <createObject>
            <clientInfo>
                <firstName>Adam</firstName>
                <lastName>Smith</lastName>
                <sex>M</sex>
                <birthDate>2000-12-23T00:00:00</birthDate>
                <language>EN</language>
                <email>[email protected]</email>
                <sendEmail>false</sendEmail>
                <phone>0123456789</phone>
            </clientInfo>   
        </createObject>
    </s:Body>
</s:Envelope>

Then I find out that the date is in wrong format, it should be like this 2000-12-23T00:00:00Z That little Z in the end makes a difference and the call like this succeeds:

<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">
        <createObject>
            <clientInfo>
                <firstName>Adam</firstName>
                <lastName>Smith</lastName>
                <sex>M</sex>
                <birthDate>2000-12-23T00:00:00Z</birthDate>
                <language>EN</language>
                <email>[email protected]</email>
                <sendEmail>false</sendEmail>
                <phone>0123456789</phone>
            </clientInfo>   
        </createObject>
    </s:Body>
</s:Envelope>

So my challenge now is to configure the call or the client in a way that the date appears in the correct format. Does anyone have any suggestion? Thanks in advance

The client inherits the System.ServiceModel.ClientBase class, instantiated as Autofac module


Solution

  • Ok, the solution is quite simple.

    When setting the DateTime value you need to specify its kind:

    birthDate = model.Birthdate.SpecifyKind(DateTimeKind.Utc);