使用blazor加载外部.NET Standard 2.0程序集 [英] Loading an external .NET Standard 2.0 assembly with blazor

查看:131
本文介绍了使用blazor加载外部.NET Standard 2.0程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Blazor应用程序中,我想加载一个外部程序集并执行一个方法.为此,我使用Blazor模板创建了一个新的ASP.Net Core Web应用程序.

然后,在Razor页面(将由浏览器/wasm编译并执行)中,我使用反射来加载程序集并运行该方法(基于代码

如您所见,当MyCustomLib.dll构建为 .NET Framework 类库时,这非常有用.但是,我想使用 .NET Standard 类库.

当我将.net Standard 2.0库构建为 MyCustomLib.dll 并执行相同的blazor应用程序时,在浏览器的控制台中出现以下错误:

无法加载文件或程序集'netstandard,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = cc7b13ffcd2ddd51'或其依赖项之一.

我希望mono.wasm会加载必要的依赖项以支持.NET Standard程序集.

  • 将程序集加载到AppDomain中会产生相同的结果.

  var程序集= AppDomain.CurrentDomain.Load(bytes); 

  • 切换到netstandard 1.6会给我一个类似的错误,这次是关于 System.Runtime (因为mono.wasm期望使用 Mono.Runtime ).

  • 也许有一种方法可以对netstandard2.0软件包引用的程序集执行 LoadAssembly ,但我不知道如何.

如何使用Blazor将.NET Standard 2.0加载到浏览器环境中?

解决方案

进行了进一步的研究后,我得出的结论是我的问题是我的外部库未正确链接到mono.net依赖项.这就是为什么当您构建Blazor应用程序时,会将其第二次编译到/dist/_framework/_bin.

我找到了解决此问题的三种可能的解决方案:

1.将外部类库转换为Blazor Web应用程序

通过这种方式,您的应用程序在构建时将自动转换为与单声道兼容的程序集.简单浏览Blazor .csproj即可了解实现此目标所需的依赖项.为了使其正常工作,我必须更改外部程序集的.csproj:

来自默认的netstandard库:

 < Project Sdk ="Microsoft.NET.Sdk">< PropertyGroup>< TargetFramework> netstandard2.0</TargetFramework></PropertyGroup></Project> 

进入网络应用程序:

 < Project Sdk ="Microsoft.NET.Sdk.Web">< PropertyGroup>< TargetFramework> netstandard2.0</TargetFramework>< RunCommand> dotnet</RunCommand>< LangVersion> 7.3</LangVersion></PropertyGroup>< ItemGroup>< PackageReference Include ="Microsoft.AspNetCore.Blazor.Build" Version ="0.7.0" PrivateAssets ="all"/></ItemGroup></Project> 

这些是唯一需要的依赖项.构建时,可在/dist/_framework/_bin文件夹中找到兼容的程序集.然后可以使用问题中描述的方法来加载它.

可行,但感觉有些不客气,因为我们将库转换为Web应用程序的唯一原因是它可以将其自身编译为正确链接的程序集.

2.加载netstandard2.0 mono外观

另一种解决方案是从 Microsoft.AspNetCore.Blazor解压缩Nuget包..build 并获取netstandard.dll.在 tools \ mono \ bcl \ Facades 文件夹中可以找到它.现在,在Blazor主应用中执行以下操作时:

  var netstandard =等待client.GetByteArrayAsync("http://localhost:62633/_framework/netstandard.dll");var externallib =等待客户端.GetByteArrayAsync("http://localhost:62633/_framework/MyCustomLib.dll");AppDomain.CurrentDomain.Load(netstandard);var assembly = AppDomain.CurrentDomain.Load(externallib); 

然后未经修改的 netstandard 2.0库 MyCustomLib 将正确加载.

  • 无需将其更改为网络应用程序
  • 可行,但它感觉比第一个解决方案更加骇人听闻,不确定是否会在此过程中失败...

3.使用Blazor Build工具

目前在此处

In a Blazor app, I want to load an external assembly and execute a method. For this, I have created a new ASP.Net Core webapplication using the Blazor template.

Then, in a Razor Page (which will be compiled and executed by browser/wasm) I use reflection to load the assembly and run the method (based on code found here)

// download external assembly from server
HttpClient client = new HttpClient();
var bytes = await client.GetByteArrayAsync("http://localhost:62633/_framework/MyCustomLib.dll");

//load assembly
var assembly = System.Reflection.Assembly.Load(bytes);

// get type/method info
var type = assembly.GetType("MyCustomLib.MyCustomClass");
var method = type.GetMethod("WriteSomething");

// instantiate object and run method
object classInstance = Activator.CreateInstance(type, null);
method.Invoke(classInstance, null);

The method WriteSomething contains a single Console.WriteLine() which prints something in the browser's console, thanks to blazor/mono.wasm goodness. The complete code in this library is:

namespace MyCustomLib
{
    public class MyCustomClass
    {
        public void WriteSomething()
        {
            System.Console.WriteLine("This is printed from a loaded dll 3 !");
        }
    }
}

Result:

As you can see, this works great when MyCustomLib.dll is built as a .NET Framework Class Library. However, I want to use a .NET Standard Class Library.

When I build a MyCustomLib.dll as a .NET Standard 2.0 library, and execute the same blazor app, I get the following error in the browser's console:

Could not load file or assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

I would expect mono.wasm would have loaded the necessary dependencies to support .NET Standard assemblies.

  • Loading the assembly into the AppDomain yields the same result.

var assembly = AppDomain.CurrentDomain.Load(bytes); 

  • Switching down to netstandard 1.6 gives me a similar error, this time about System.Runtime (because mono.wasm expects Mono.Runtime I assume).

  • Maybe there's a way to perform LoadAssembly on the assemblies referenced by the netstandard2.0 package, but I wouldn't know how.

How can I load a .NET Standard 2.0 into the browser environment using Blazor?

解决方案

After doing some further investigation, I've concluded that my problem is that my external library is not properly linked to the mono.net dependencies. This is why, when you build a Blazor app, it is compiled a second time to /dist/_framework/_bin.

I've found three possible solutions to this problem:

1. Turn the external class library into a Blazor Web app

This way, your app will automatically be converted to a mono-compatible assembly when built. A simple peek in to a Blazor .csproj shows the dependencies needed to achieve this. For it to work, I had to change the .csproj of my external assembly:

from a default netstandard library:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>
</Project>

into a web app:

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <RunCommand>dotnet</RunCommand>
        <LangVersion>7.3</LangVersion>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="0.7.0" PrivateAssets="all" />
    </ItemGroup>
</Project>

These are the only dependencies needed. On build, the compatible assembly will be found in the /dist/_framework/_bin folder. It can then be loaded using the methods described in the question.

This works, but feels a bit hacky because the only reason we're turning the library into a web app is so that it can compile itself into a properly linked assembly.

2. Load the netstandard2.0 mono facade

Another solution is to unzip the Nuget Package from Microsoft.AspNetCore.Blazor.Build and grab the netstandard.dll. It's found in the tools\mono\bcl\Facades folder. Now, when doing the following in the main Blazor app:

var netstandard = await client.GetByteArrayAsync("http://localhost:62633/_framework/netstandard.dll");
var externallib = await client.GetByteArrayAsync("http://localhost:62633/_framework/MyCustomLib.dll");
AppDomain.CurrentDomain.Load(netstandard);
var assembly = AppDomain.CurrentDomain.Load(externallib);

then the unmodified netstandard 2.0 library MyCustomLib will be loaded without errors.

  • No need to change it to a web app
  • This works, but it feels even hackier than the first solution, unsure whether this will fail later along the way...

3. Use the Blazor Build tools

The Blazor Build tools, currently found here, they have a ResolveRuntimeDependenciesCommand command for the CLI which seems to do exactly what a blazor web app is doing when it spits output to /_framework/_bin. I'm still looking at how this could be used to convert a "non blazor-webapp" assembly into a mono-compatible one.

Feel free to comment or answer with additional information. I'm leaving this question open until a "cleaner" solution is found.

这篇关于使用blazor加载外部.NET Standard 2.0程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆