2

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.

jazb
  • 5,498
  • 6
  • 37
  • 44
QBBryant
  • 161
  • 1
  • 11
  • 2
    add nuget `RestSharp.Serializers.SystemTextJson` – Lei Yang Jan 28 '22 at 01:21
  • Hi, thanks for the reply @lei-yang. Unfortunately the suggestion to add the above nuget package didn't fix it for me. – QBBryant Jan 28 '22 at 14:50
  • consider commit a minimal project to github for investigating? – Lei Yang Jan 28 '22 at 15:02
  • Interestingly, I created a new VS solution from scratch and the RestClient works now! So I guess there's something awry about my original project. I'll try removing nuget packages one at a time until it works (If I discover anything I will post my findings here for the benefit of others who encounter this issue). – QBBryant Jan 28 '22 at 15:41
  • looks good! when you face difficulties sometimes step back can be useful. – Lei Yang Jan 28 '22 at 15:55
  • Well....it seems to be related to the Microsoft packages which are required for function apps: `Microsoft.Azure.WebJobs.Extensions.*` and/or `Microsoft.NET.Sdk.Functions`. I verified this by converting my test VS project into a function app. Apparently the newest version of RestSharp isn't 100% compatible with .NET Core 3.1 Function Apps. I am going to try reverting to an earlier version of RestSharp and/or updating my Function App to .NET 5 or 6. – QBBryant Jan 28 '22 at 17:45
  • what is 'function app'? – Lei Yang Jan 29 '22 at 01:03
  • The `RestSharp.Serializers.SystemTextJson` package is deprecated, it's the default serializer for RestSharp since v107. The RestSharp package itself has a dependency on `System.Text.Json >= 5.0`, so you should normally get it installed implicitly when you add the RestSharp package. It's weird that you get this error... – Alexey Zimarev Jan 29 '22 at 09:25
  • This might be a similar issue: https://learn.microsoft.com/en-us/answers/questions/510459/error-loading-assembly-using-graph-api.html – Alexey Zimarev Jan 29 '22 at 09:31

1 Answers1

1

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

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15