Search code examples
c#asp.net.netrestsharp

RestSharp failing to instantiate RestClient on live server, but works fine on local machine


I recently updated restsharp nuget package to 110.2.0 from what I believe was 110.0.0 (not totally sure though). We use rest sharp to send orders to a supplier of ours, and the code was working fine before I updated it. Since I updated it, it now fails to instantiate the RestClient object on our live server, but it works fine on our local server.

Here is the code:

            using (var client = new RestClient("https://pencarrie.com/gateway"))
            {
                var request = new RestRequest();

                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                request.Method = RestSharp.Method.Post;

It fails on this line "using (var client = new RestClient("https://pencarrie.com/gateway"))", with the message: Exception has been thrown by the target of an invocation.

The stack trace is: Stack Trace: at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstanceT at RestSharp.Serializers.SerializerConfig.<>c__51.b__5_0() at RestSharp.Serializers.SerializerConfig.UseSerializer(Func1 serializerFactory) at RestSharp.Serializers.SerializerConfig.UseSerializerT at RestSharp.RestClient.ConfigureSerializers(ConfigureSerialization configureSerialization) at RestSharp.RestClient..ctor(RestClientOptions options, ConfigureHeaders configureDefaultHeaders, ConfigureSerialization configureSerialization, Boolean useClientFactory) at RestSharp.RestClient..ctor(String baseUrl, ConfigureRestClient configureRestClient, ConfigureHeaders configureDefaultHeaders, ConfigureSerialization configureSerialization) at Slingshot.MyStore.Supplier.Pencarrie.CallPencarrieAPI(PencarrieFunctionName PencarrieFunctionName, String parameters) in C:\Users\Rob\source\repos\LaunchPad3_1\Slingshot\MyStore\Supplier\Pencarrie.cs:line 36

I can see there is another post here about it: RestSharp: RestClient constructor throws System.IO.FileNotFoundException, but we are using .net framework, not .net core, and we have it working fine on local machines, but not on the server. What could be going on? Could it be something we need to do in our web.config (which is different on our local machines to our live server)? Or something else?


Solution

  • You need to make sure that your published app contains the correct version of System.Text.Json.dll (it's the cause of the issue you have referenced), and you are running on a .NET runtime that is compatible. The earliest .NET Framework version supported by System.Text.Json is 4.6.2.

    You can also use Newtonsoft.Json serialiser instead of the default one as it might provide better compatibility options for .NET Framework.