ably / ably-dotnet

.NET, MAUI, Xamarin, Mono and Unity client library SDK for Ably realtime messaging service
https://ably.com/download
Apache License 2.0
45 stars 22 forks source link

Ably C# SDK version 1.2.14 failed to create instance of AblyRealTime #1286

Closed bhaskarsoni-ai closed 6 months ago

bhaskarsoni-ai commented 6 months ago

I am receiving following error when tried to use Ably NuGet package 1.2.14 with a .NET Framework 4.7.2 project.

Could not load file or assembly 'System.Threading.Channels, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified. Please see following image.

image (2)

After a discussion with Ably Support team, the recommendation was to add a nuget reference of System.Threading.Channels=4.6.0 Even though we are getting following exception

Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Error2

┆Issue is synchronized with this Jira Task by Unito

sacOO7 commented 6 months ago

Hi @bhaskarsoni-ai can you try installing System.Threading.Tasks.Extensions (= 4.5.2). Basically, that's the one direct dependency of System.Threading.Channels as per https://www.nuget.org/packages/System.Threading.Channels/4.6.0#dependencies-body-tab. Also, I am skeptical about nuget package manager in your system, since it's not installing dependents for given packages.

sacOO7 commented 6 months ago

Also you can try editing your csproj file for assembly binding as per https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/redirect-assembly-versions

sacOO7 commented 6 months ago

If issue still persists, it will be useful if you can post steps to reproduce above issue !

bhaskarsoni-ai commented 6 months ago

@sacOO7 Steps to reproduce are very simple. You create a .net framework 4.7.2 project. Use ably nuget package and try to create the AblyRealTime instance. The exception is thrown.

We have tried this on multiple machines and it seems not to be a nuget package manager issue. We also using multiple nuget packages on same environment and we didn't face any such issues with other nuget packages.

Of course we can use this workaround https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/redirect-assembly-versions. But we have other nuget packages which might using different versions of System.Threading.Tasks.Extensions (= 4.5.2) and i suspect it might start giving errors somewhere else.

sacOO7 commented 6 months ago

@bhaskarsoni-ai understood. We will take a look at it and will post an update once we are able to reproduce it from our side.

sacOO7 commented 6 months ago

Meanwhile you might like to take a look at https://nickcraver.com/blog/2020/02/11/binding-redirects/

sacOO7 commented 6 months ago

I strongly believe this is caused by other transitive dependencies in the project. So, using proper binding-redirects should be a viable fix for now. Hopefully, this is addressed in the future versions of dotnet-framework.

sacOO7 commented 6 months ago

@bhaskarsoni-ai let me know if the fix is working for you. Just to be sure, you can try installing ably-dotnet in a plain netframework project where there are no transient dependency conflicts.

sacOO7 commented 6 months ago

Found one more useful link if you are interested -> https://davidsekar.com/asp-net/fixing-assembly-redirects-issue-quickly#!#google_vignette

bhaskarsoni-ai commented 6 months ago

@sacOO7 Thanks for sharing the link and multiple solutions. Ours wrapper on Ably will be a standalone class library where Assembly redirect will difficult to use, as Assembly redirect can only be added inside App.Config. In a standalone library we don't have a app.config file.

sacOO7 commented 6 months ago

Okay, one final solution I can suggest is to add reference DLL's as a part of the project. You can follow https://stackoverflow.com/a/50716032. We are doing the same in case of unity -> https://github.com/ably/ably-dotnet/tree/main/unity/Assets/Ably/Plugins. You can see 3 different DLLs present

  1. System.Threading.Channels.dll
  2. System.Threading.Tasks.Extensions.dll
  3. System.Runtime.CompilerServices.Unsafe.dll Since, we are not installing them via from Nuget, ideally shouldn't generate conflicts. If there are conflict warnings, you should disable them. So, at runtime right version of assembly is resolved.

Note - Being a netframework project, I am skeptical about not being able to add assembly redirects. I think you should be able to add it via some different configuration ( maybe editing .csproj or .sln file etc ). Also, since you are publishing it as a library, you might have to add those redirects in the project where it's being used. e.g. test netframework project

bhaskarsoni-ai commented 6 months ago

@sacOO7 Please suggest what specific version of the dll I should be referring, suggested by you above

sacOO7 commented 6 months ago

You should visit https://www.nuget.org/packages/ably.io/1.2.15#dependencies-body-tab. under dependencies tab, you should be able to see the dependency with respective version. You need to download that dependency and use dll for the same. Also, you can click on the dependency to find out it's dependency, download it again till you finally download compiler.unsafe dll

sacOO7 commented 6 months ago

@bhaskarsoni-ai let me know if the mentioned fix is working for you !

bhaskarsoni-ai commented 6 months ago

Hi @sacOO7 We are in the process of verifying the fix by using the workaround suggested. Will update once done.

sacOO7 commented 6 months ago

Found one more useful article you might like to take a look at https://learn.microsoft.com/en-us/dotnet/framework/deployment/how-the-runtime-locates-assemblies?source=recommendations. It does mention various methods to add support for assembly redirects.

sacOO7 commented 6 months ago

Hi @bhaskarsoni-ai do we have any update on the above?

bhaskarsoni-ai commented 6 months ago

With using Assembly redirect we are not seeing any issues for now. Will keep you posted if something new observed @sacOO7

sacOO7 commented 6 months ago

Thanks for the update @bhaskarsoni-ai . I will be closing this issue for now. You can open a new issue in the future if needed !