如何在 System.ServiceModel.ClientBase 中配置日期时间格式

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

我正在尝试通过如下调用来调用 Web 服务。失败了。

<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>adam@smith.com</email>
                <sendEmail>false</sendEmail>
                <phone>0123456789</phone>
            </clientInfo>   
        </createObject>
    </s:Body>
</s:Envelope>

然后我发现日期格式不对,应该是这样的 2000-12-23T00:00:00Z 那个小Z最后产生了不同,这样的调用成功了:

<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>adam@smith.com</email>
                <sendEmail>false</sendEmail>
                <phone>0123456789</phone>
            </clientInfo>   
        </createObject>
    </s:Body>
</s:Envelope>

所以我现在的挑战是以正确格式显示日期的方式配置调用或客户端。 有人有什么建议吗?提前致谢

客户端继承了System.ServiceModel.ClientBaseclass,实例化为Autofac模块

好的,解决方法很简单。

设置 DateTime 值时需要指定其类型:

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