XiaoFaye / WooCommerce.NET

A .NET Wrapper for WooCommerce/WordPress REST API
MIT License
393 stars 218 forks source link

Suddenly started getting System.Runtime.Serialization.SerializationException from 14th September 2020. #524

Closed mnaeem76 closed 3 years ago

mnaeem76 commented 4 years ago

Hello,

I have started receiving System.Runtime.Serialization.SerializationException from 14th September. The code has been working just fine and there has been no change at my end either.

The code is very simple I'll put it below here

 RestAPI rest = new RestAPI(WooApiUrl, WooKey, WooSecret);
 var wc = new WooCommerceNET.WooCommerce.v3.WCObject(rest);

 //Get all orders
 var ord = wc.Order.GetAll().Result;

The error is occurring on the LIVE deployment which is using the following version of WordPress and WooCommerce

On my local development system though, I'm not facing any problem at all. My local system's WordPress and WooCommerce versions are as below:

I'm not sure if the client has just recently update their WooCommerce version which could be causing this problem. Any help on this matter will be highly appreciated.

In the meantime, I'm going to pull in the raw request data to see why date is causing this issue.

Error: System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type WooCommerceNET.WooCommerce.v3.Order. String was not recognized as a valid DateTime. ---> System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style) at ReadOrderFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString[] ) at System.Runtime.Serialization.Json.JsonClassDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) at System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader) at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract) at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns) at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName) at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) --- End of inner exception stack trace --- at WooCommerceNET.RestAPI.DeserializeJSon[T](String jsonString) at WooCommerceNET.Base.WCItem`1.d__9.MoveNext()

mnaeem76 commented 4 years ago

So here's what I found out in the raw response from my dev server vs. live server.

Pulling raw date from LIVE server shows data as below

    "date_created": "2020-09-14T15:31:18+00:00",
    "date_created_gmt": "2020-09-14T21:31:18+00:00",
    "date_modified": "2020-09-14T16:03:58+00:00",
    "date_modified_gmt": "2020-09-14T22:03:58+00:00",

Pulling from local server shows

    "date_created": "2020-08-11T16:23:16",
    "date_created_gmt": "2020-08-11T11:23:16",
    "date_modified": "2020-08-11T16:23:20",
    "date_modified_gmt": "2020-08-11T11:23:20",

Notice the +00:00 in live data? I thought that would be causing the problem. However, when I de-serialized raw string data to WooCommerceNET.WooCommerce.v3.Order object, It just worked fine.

Below is the code I used.

string response = string.Empty;

using (var httpClient = new HttpClient())
{
    using (var request = new HttpRequestMessage(method, url))
    {
        var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{WooKey}:{WooSecret}"));
        request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");

        var resp = httpClient.SendAsync(request).Result;

        if (resp.StatusCode == HttpStatusCode.OK)
        {
            response = resp.Content.ReadAsStringAsync().Result;
        }
    }
}

var order = JsonConvert.DeserializeObject<WooCommerceNET.WooCommerce.v3.Order>(response);

So I'm really at loss as to why using the API runs into exception while Raw response serializes perfect fine to the order object.

My final thoughts on this is that probably the API is not able to handle +00:00 piece of the date coming in. NewtonSoft's library is able to handle this internally but your library code is probably using different de-serializer which is now breaking the code.

Looking forward to hear from you soon on this.

mnaeem76 commented 4 years ago

Hello again. After looking at the API code I found the following piece of code that deals with the date formats

public string DateTimeFormat => this.IsLegacy ? "yyyy-MM-ddTHH:mm:ssZ" : "yyyy-MM-ddTHH:mm:ss"; this is from the WooCommerceNET.RestAPI class

If the new format is changed to "yyyy-MM-ddTHH:mm:ssK" this issue will be resolved

Proposed change public string DateTimeFormat => this.IsLegacy ? "yyyy-MM-ddTHH:mm:ssZ" : "yyyy-MM-ddTHH:mm:ssK";

Thanks Mudasser

XiaoFaye commented 4 years ago

It seems this has sometime to do with your LIVE server datetime format setting?

mnaeem76 commented 4 years ago

It seems this has sometime to do with your LIVE server datetime format setting?

It may be that since I confirmed they installed some plugins etc. which could be causing this. However, the fix I have suggested will work seamlessly for both date formats i.e. 2020-09-16T20:28:34+00:00 and 2020-09-16T20:28:34 will work with the addition of K.

public string DateTimeFormat => this.IsLegacy ? "yyyy-MM-ddTHH:mm:ssZ" : "yyyy-MM-ddTHH:mm:ssK";

Regards Mudasser

XiaoFaye commented 4 years ago

Sure, will include this changes in the next version.

XiaoFaye commented 3 years ago

Hi, this is fixed in version 0.8.3.