Search code examples
c#asp.net-mvcazure-functionsrestsharpjsonserializer

RestSharp: RestClient constructor throws System.IO.FileNotFoundException


I am developing a .NET Core 3.1 Azure Function App in Visual Studio 2022. After upgrading to the latest version of RestSharp today, I'm getting the following exception when I try to instantiate a new RestClient:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. File name: 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' at RestSharp.Serializers.Json.SystemTextJsonSerializer..ctor()

Here is the code I'm using to instantiate the client:

var clientOptions = new RestClientOptions
{
    BaseUrl = new Uri("https://xxxxxxx.com"),
    Timeout = 29000
};
_restClient = new RestClient(clientOptions);   

The error occurs regardless of whether I pass in a ClientOptions object to the constructor.

I have tried installing version 5.0.0.0 of System.Text.Json via Nuget, but to no avail. I have also removed other packages which might have a dependency on a different version of System.Text.Json - but this doesn't seem to work either.

I have tried cleaning my solution, deleting the obj and bin folders, closing and reopening Visual Studio, and even rebooting my machine. Still no go.

Any suggestions? I'm tearing my hair out.


Solution

  • If you are targeting .net core function app you can use the .net core references.

    Here if you are using RestSharp which contains .net remove it from your .netcore project

    enter image description here

    Here I am using RestSharp.netcore which target .netcore azure function

    enter image description here

    The Resharp.Newtonsoft.Json.NetCore package is deprecated so you can redirect to RestSharp.Serializers.NewtonsoftJson you need to install this package.

    And In your .csproj file you have to mention the below line of code under < PropertyGroup>

    <_FunctionsSkipCleanOutput> true </_FunctionsSkipCleanOutput>
    

    Which is used to skip the reference cache of output which previously loaded. Every time it loads new references.

    enter image description here

    Here is the issue we added the RestSharp.Serializers.NewtonsoftJson package and Not added the _FunctionsSkipCleanOutput

    enter image description here

    After Adding the RestSharp.Serializers.NewtonsoftJson package and _FunctionsSkipCleanOutput

    enter image description here