【问题标题】:The type could not be found. It may require assembly qualification, e.g. "MyType, MyAssembly"找不到类型。它可能需要装配资格,例如“我的类型,我的程序集”
【发布时间】:2021-04-08 04:05:38
【问题描述】:

我有 .NET 核心项目,由 dotnet 构建。然后我在构建后进行 dotnet store 和 dotnet publish。 “Test.dll”在运行时包存储和它之后的发布文件夹中生成。当我运行“dotnet MyProject.dll”时,在 builder.Build() 期间出现异常:

System.InvalidOperationException:找不到类型“TestModule,Test”。它可能需要装配资格,例如“我的类型,我的程序集”。

MyJsonFile.json

{
    "modules": [
     {
        "type": "TestModule, Test",
        "properties": {
            "PluginInstanceNames": [ "Test" ]
        }
      }
   ]
}

MyProject.cs

var configBuilder = new ConfigurationBuilder();
configBuilder.AddJsonFile("MyJsonFile.json");
var configModule = new ConfigurationModule(configBuilder.Build());

builder = new ContainerBuilder();
builder.RegisterModule(configModule);
builder.Build();

为什么它不在运行时包存储中的 .json 文件中搜索模块?它是如何工作的?

仅当我添加了此回调函数时才有效,其中“路径”是字符串数组,其中包含运行时存储文件夹和发布文件夹的路径。

AssemblyLoadContext.Default.Resolving += (context, name) =>
{
     var dll = Array.Find(path, f => f.EndsWith(name.Name + ".dll"));
     if (dll == null)
     {               
         return null;
     }
     Assembly assembly = context.LoadFromAssemblyPath(dll);
     return assembly;
}

【问题讨论】:

    标签: c# .net autofac


    【解决方案1】:

    Autofac 不负责程序集加载,.NET 框架负责。在 .NET Core 中加载程序集与在桌面上的经典 .NET 框架中加载不同,并且确实需要额外的工作,如您所见。 There's an issue here (closed) in Autofac.Configuration 解释;我们有一个currently open issue to document this in more detail,但总而言之:您需要自己使用AssemblyLoadContext,就像您在加载插件时所做的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 2019-05-12
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 2019-04-30
      • 2019-08-22
      • 1970-01-01
      相关资源
      最近更新 更多