Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp updating use of AddHandler mathod to use factory delegate

Tags:

c#

restsharp

The latest version of RestSharp v106.6.9 apparently makes some changes making the overrides of the AddHandler method of a Request obsolete such as this signature:

[Obsolete("Use the overload that accepts a factory delegate")]
public void AddHandler(IDeserializer deserializer, params string[] contentTypes)

As it suggest to use the factory delegate form

public void AddHandler(string contentType, Func<IDeserializer> deserializerFactory)

public void AddHandler(Func<IDeserializer> deserializerFactory, params string[] contentTypes)

Would anyone be able to point me to an example of implementing this. Or explain how to transform my use of a customSerializer implementing IDeserializer below, to a factory delegate:

RestClient.AddHandler("application/json", CustomJsonSerializer.Instance);


public class CustomJsonSerializer : IDeserializer
{
    public static CustomJsonSerializer Instance => new CustomJsonSerializer();

    public string ContentType
    {
        get => "application/json";
        set { } // maybe used for Serialization?
    }

    public string DateFormat { get; set; }

    public string Namespace { get; set; }

    public string RootElement { get; set; }

    public T Deserialize<T>(IRestResponse response) => RestSharpResponseHandlers.DeserializeObject<T>(response);
}
like image 982
axa Avatar asked Mar 05 '19 23:03

axa


People also ask

Does RestSharp use HttpClient?

Since RestSharp uses the HttpClient, we should consider similar issues regarding the RestClient instantiation.

Is RestSharp thread safe?

Recommended usage. RestClient should be thread-safe. It holds an instance of HttpClient and HttpMessageHandler inside. Do not instantiate the client for a single call, otherwise you get issues with hanging connections and connection pooling won't be possible.


2 Answers

According to the source code at https://github.com/restsharp/RestSharp/blob/dev/src/RestSharp/RestClient.cs:

 [Obsolete("Use the overload that accepts a factory delegate")]
 public void AddHandler(string contentType, IDeserializer deserializer) =>
    AddHandler(contentType, () => deserializer);

The obsolete overload just calls the AddHandler(string contentType, Func<IDeserializer> deserializerFactory) overload.

So you can replace your code to add your custom handler as follows:

RestClient.AddHandler("application/json", () => { return CustomJsonSerializer.Instance; });
like image 139
sparkplug Avatar answered Nov 01 '22 04:11

sparkplug


I ran into the same problem. It looks like the OpenAPI code is setting mulitple handlers for multiple context types, so I wrote this little function

        private void AddHandlerHelper(RestClient client, IDeserializer deserializerFactory, string[] contextTypes)
        {
            foreach( var contextType in contextTypes)
            client.AddHandler(contextType,() => deserializerFactory);
        }

And the the call changes from this:

client.AddHandler(() => existingDeserializer, "application/json", "text/json", "text/x-json", "text/javascript", "*+json");

to this

AddHandlerHelper(client, existingDeserializer, new string[] { "application/json", "text/json", "text/x-json", "text/javascript", "*+json"});

There are six places in the generated code I needed the function.

like image 1
Sam Carleton Avatar answered Nov 01 '22 04:11

Sam Carleton