1

我正在使用EasyPost 集成制作一个类库以更简单地使用他们的 API,但我收到了这个错误:

Managed Debugging Assistant 'DateTimeInvalidLocalFormat' has detected a problem in 'C:\Projects\TestClient.vshost.exe'.

Additional information: A UTC DateTime is being converted to text in a format that is only correct for local times. This can happen when calling DateTime.ToString using the 'z' format specifier, which will include a local time zone offset in the output. In that case, either use the 'Z' format specifier, which designates a UTC time, or use the 'o' format string, which is the recommended way to persist a DateTime in text. This can also occur when passing a DateTime to be serialized by XmlConvert or DataSet. If using XmlConvert.ToString, pass in XmlDateTimeSerializationMode.RoundtripKind to serialize correctly. If using DataSet, set the DateTimeMode on the DataColumn object to DataSetDateTime.Utc.

当我Create在 EasyPostShipment对象中调用该方法时出现此错误。下面的代码:

Shipment shipment = new Shipment() {
  to_address = toAddress,
  from_address = fromAddress,
  parcel = parcel
};

shipment.Create();

这个创建函数可能会调用他们的 REST API,并试图将 json 响应转换为他们的模型之一。

为了解决这个错误,我试图将 UTC 设置为我的库的默认值,所以每当我使用时,我都会DateTime.ToString()使用DateTime.ToString("o"). 我不知道这是否真的能解决问题,但我不知道如何强制它(使用 UTC 作为库默认值)。我已经尝试了下面的一段代码,但它不起作用

CultureInfo newCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentCulture = newCulture;

你能帮助我吗?

4

0 回答 0