简体   繁体   中英

How to use the ASP.NET Core shared framework correctly or how to use its assemblies on their own?

The situation

In our application we have a WPF client and an ASP.NET Core server , both of which uses .NET 5 .

We store all our DLLs on server and the client downloads all the necessary DLLs from server when user logs in. Originally, the client has been independent of any third-party libraries, our other projects, etc. which allowed us, in certain situations, to download the client as DLL (using another application – which I will be calling a loader – specifically designed for this purpose), load the client using Assembly.Load(binaryData) and execute the login method.

Side note: What has been so far referred to as client is actually just a simple application which authenticates the user and downloads the DLLs for the actual client which consists of multiple assemblies and allows us to work with the data on server. For the sake of simplicity, however, I won't distinguish between them as they both stand on the client side.

Recently, I've implemented single sign-on using OpenID Connect based on NetCoreConsoleClient sample from IdentityModel and, as a result, a few references to other assemblies have been added to the client . Now, in order for the SSO to work, the loader has to download these assemblies together with the client .

The problem

Currently, the client uses some of the assemblies from Microsoft.AspNetCore.App shared framework. According to this SO question & answer it should be working on my machine since the runtime is installed – the framework is present on my machine in: C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\5.0.0 . However, when I try logging in using the client downloaded via loader I'm getting System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.AspNetCore.Hosting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.'

Also, if I take the DLL from the path mentioned above and try to load the Microsoft.AspNetCore.Hosting assembly manually, using Assembly.Load(binaryData) , I'm getting System.BadImageFormatException: 'Could not load file or assembly 'Microsoft.AspNetCore.Hosting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)'

I tried searching online and discovered that it might be caused by 32-bit/64-bit architecture conflict. I tried creating a simple demo application where I tried changing between the architectures but had no success.

I also have found a few mentions of the exception I'm getting when loading the assembly manually but none of them seems to be relevant for my specific case.

The questions

Am I using the Microsoft.AspNetCore.App shared framework wrong? How to use it properly?

Is there a way how to actually use Microsoft.AspNetCore.Hosting and other assemblies from Microsoft.AspNetCore.App as a regular DLL so that I can load them using Assembly.Load(binaryData) ?

Eventually, we have been able to figure it out.

The client references Microsoft.AspNetCore.App and Microsoft.WindowsDesktop.App and the loader has to reference them as well since the client DLL is loaded using Assembly.Load(binaryData) inside it.

This is something I have completely missed but was as easy to fix as adding the following piece of markup to the loader project file:

<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM