3

I have a C# (Azure function app) project in Visual Studio. .Net 6.0

I updated RestSharp to version 110.2.0, and now I get this error:

[2023-04-29T21:34:10.399Z] Executed 'RequestItemsPage' (Failed, Id=8eefbb7c-3bc0-44db-81e4-14c56096fc81, Duration=63ms)
[2023-04-29T21:34:10.401Z] System.Private.CoreLib: Exception while executing function: RequestItemsPage. System.Private.CoreLib: Exception has been thrown by the target of an invocation. RestSharp: Could not load file or assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
[2023-04-29T21:34:10.408Z] eb4802876b9e44839f63422be68619dc: Function 'RequestItemsPage (Activity)' failed with an error. Reason: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
[2023-04-29T21:34:10.411Z]  ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
[2023-04-29T21:34:10.412Z] File name: 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
[2023-04-29T21:34:10.413Z]    at RestSharp.Serializers.Json.SystemTextJsonSerializer..ctor()
[2023-04-29T21:34:10.423Z]    at System.RuntimeType.CreateInstanceOfT()
[2023-04-29T21:34:10.423Z]    --- End of inner exception stack trace ---
[2023-04-29T21:34:10.427Z]    at System.RuntimeType.CreateInstanceOfT()
[2023-04-29T21:34:10.429Z]    at System.Activator.CreateInstance[T]()
[2023-04-29T21:34:10.430Z]    at RestSharp.Serializers.SerializerConfig.<>c__5`1.<UseSerializer>b__5_0()
[2023-04-29T21:34:10.434Z]    at RestSharp.Serializers.SerializerConfig.UseSerializer(Func`1 serializerFactory)
[2023-04-29T21:34:10.438Z]    at RestSharp.RestClient.ConfigureSerializers(ConfigureSerialization configureSerialization)
[2023-04-29T21:34:10.439Z]    at RestSharp.RestClient..ctor(RestClientOptions options, ConfigureHeaders configureDefaultHeaders, ConfigureSerialization configureSerialization, Boolean useClientFactory)
[2023-04-29T21:34:10.444Z]    at ContentHub_ItemUpdates.Producers.PullAllContentHubtemsByPage.RequestItemsPage(Int32 skip, ILogger log) in C:\Users\bwarrick\Workspace\.NET\Integrations_MDM\ContentHub_ItemUpdates\ContentHub_ItemUpdates\Producers\PullAllContentHubtemsByPage.cs:line 58
[2023-04-29T21:34:10.479Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments)
[2023-04-29T21:34:10.480Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeWithTimeoutAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 581
[2023-04-29T21:34:10.481Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 527
[2023-04-29T21:34:10.499Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 306. IsReplay: False. State: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.9.4. SequenceNumber: 7. TaskEventId: 0

This is odd because this version of RestSharp had a dependency on System.Text.Json >= 7.0.2. I checked the dependencies, and it's there -- and I also saw when updating that it was upgrading System.Text.Json.

I tried installing the NuGet package for System.Text.Json. Didn't work. I then tried uninstalling and reinstalling RestSharp. Cleaning. Rebuilding. Restarting Visual Studio.

I also tried downgrading Restsharp. I'm starting to think I broke my project and need to scrap it...

Any ideas about this?

BenW
  • 737
  • 10
  • 41
  • Downgrade to RestSharp that uses the same version of .net 6 – Train Apr 29 '23 at 21:47
  • .NET 6 uses System.Text.Json v6 so I am guessing you may need to nuget [7.0.2](https://www.nuget.org/packages/System.Text.Json/7.0.2), possibly along with its dependencies [System.Runtime.CompilerServices.Unsafe 6.0.0](https://www.nuget.org/packages/System.Runtime.CompilerServices.Unsafe/6.0.0) and [System.Text.Encodings.Web 7.0.0](https://www.nuget.org/packages/System.Text.Encodings.Web/7.0.0). – dbc Apr 29 '23 at 21:54
  • You may also need to add binding redirects to prevent the .NET 6 itself from loading the older versions, see [Serializing objects from projects written for different major versions of `System.Text.Json`](https://stackoverflow.com/a/76096525/3744182). – dbc Apr 29 '23 at 21:54
  • 1
    I tried these ideas. Thanks. But the error persists. I ended up downgrading RestSharp – BenW Apr 30 '23 at 20:47
  • As mentioned when I am using Azure function apps anything at the moment that targets the .net 7.0 or version 7+ appears to give errors of missing assemblies even though it compiles correctly. Downgrading those and the function app to 6.0 if you moved it to 7 appears to fix the issue. – TheNerdyNerd May 01 '23 at 10:58
  • `System.Text.Json` supports .NET 6, it has nothing to do with the .NET 6 runtime. Azure functions only support .NET 6 runtime, so you need to target it, but your deployment must include all the dependencies. It's hard to diagnose your issue as it might be related to the way you deploy your app. – Alexey Zimarev May 11 '23 at 15:13

2 Answers2

6

I got the same question as an issue, and the issue is caused by Azure Functions deployment method, which strips some assemblies that are considered "unnecessary" as they are presumably included in the runtime.

The correct answer is provided here, it's basically to add a property to the project file:

<PropertyGroup>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
0

I have created a sample Http Azure function to check if the versions of both packages are compatible with each other as mentioned in doc. I can see no issues with packages and its version.

As you mentioned, RestSharp has System.Text.Json as dependency as shown below: enter image description here

  • I suggest you check by removing the RestSharp and System.Text.Json DLLs from the project directory and re-add them by installing the latest versions of those packages to make sure no conflicts.

Try to use latest pre-release version of RestSharp 110.2.1-alpha.0.1. This version is also compatible with System.Text.Json as shown in this doc.

enter image description here

If that doesn't work, create a new project and install both the packages and if the issue persists.


Created a sample project and installed RestSharp 110.2.0 and System.Text.Json 7.0.0 packages and observed that both the package versions are compatible with each other.

enter image description here

Result:

enter image description here

Pravallika KV
  • 2,415
  • 2
  • 2
  • 7