0

We have the following code in our c# .Net 4.7 class library project.

        _policy = CreateTokenRefreshPolicy();

        FlurlHttp.Configure(c =>
        {
            c.JsonSerializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings
            {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
            });
        });

We use it to set global property for all our flurl requests.
Our current challenge is that we have on request which has a large body (a byte array) that we need to send to an external API.

When we attempt to serialize the object in a Post Request as follows:

results = await _policy.ExecuteAsync(context =>
            {
                return _url
                .WithTimeout(TimeSpan.FromMinutes(5))
                .WithOAuthBearerToken(context["AccessToken"]?.ToString())
                .PostJsonAsync(requestPayload)
                .ReceiveJson<ResponseBase<T2>>();
            }, new Dictionary<string, object>
            {
                { "AccessToken", _accessToken },
                { "RefreshToken", RefreshToken }
            });

And we are getting the following error:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

We have looked for a MaxJsonLength property in the serializer configuration but did not find one. Additionally, I have seen various resolutions to this problem in StackOverflow, however, we have the following constraints:

  1. We need to be able to set the value globally.
  2. We do not want to change serializers as this is common code shared by all our Flurl calls and such a change would require retesting everything.
  3. We could create a separate flurl post function that is used only in this one case, however that is not idea.

We are simply looking for a way, within the current configuration, to set some value or property that will allow a large JSON string. Any suggestions would be appreciated.

NOTE: We have tried adding the following to the Web.config file and we received the same error message:

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="50000000"/>
        </webServices>
    </scripting>
</system.web.extensions>
George M Ceaser Jr
  • 1,497
  • 3
  • 23
  • 52

0 Answers0