gpt4 book ai didi

c# - 将 DateTime 反序列化为 WCF SOAP 服务 C# 上的参数的异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:07 25 4
gpt4 key购买 nike

在本地环境中开发了一个包含两个 WCF 服务的系统并在另一个环境中对其进行了测试,我们最终将其部署到生产环境中,但显示了一个完全不同的错误。这两个服务运行在四台不同的机器上(每个服务运行在两台机器上,负载均衡)。

InvokeEventWithParameters 操作不工作:

[ServiceContract]
public interface IAccountManager
{
[OperationContract]
int InvokeEventWithParameters(string EventName, DateTime InitialDate, DateTime FinalDate, int InitialId, int FinalId, string Username);

[OperationContract]
int InvokeEvent(string EventName, string Username);
}

尝试运行服务时,返回以下错误:

The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'InvokeEventWithParameters'. The value '1/1/2014 12:00:00 AM' cannot be parsed as the type 'DateTime'.

错误显示在两个服务之间的通信上,在 SOAP 消息上。我无法检查 ISO 8601 是否在服务之间发送,因为它正在生产环境中运行。我已经使用嗅探器检查了测试环境,ISO 得到了尊重。

有趣的是错误上显示的日期格式为“en-US”,而系统和 IIS 配置为“pt-BR”,其格式应符合 ISO 标准。

我已经尝试更改机器文化配置、IIS 全局化配置和 web.config 文件中的文化。这些都不起作用。我已经在日期时间检查了整个项目的“.ToStrings”和“.Parses”,但没有找到它们,因此没有其他可能的异常来源。

在我看来,SOAP 消息的 DateTime 序列化和反序列化(使用 DataContractDeserializer)不应受到任何有关文化的配置的影响,但我在 google 和此处找到的所有内容都与这些设置有关。我的这个假设是否正确?

谢谢你的帮助,埃米利奥

最佳答案

我猜这是一个服务端错误,它正在等待一种日期时间格式,但它正在获取另一种格式。你能改变那个代码吗?如果是这样,请尝试将收到的日期解析为有效格式:

DateTime.ParseExact(yourDateParam, "M/d/yyyy hh:mm:ss t", new CultureInfo("en-US"));

注意 M/d,因为我不知道您是在 (d) 日之前发送月 (M) 还是反之亦然。

编码愉快!

关于c# - 将 DateTime 反序列化为 WCF SOAP 服务 C# 上的参数的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36902767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com