无法设置Autofac 5(Autofac.Configuration 5.0.0和Autofac 5.1.2的问题)

问题描述 投票:0回答:1

我正在尝试使用Autofac 5 IoC Container建立一个简单的。NET Core 3.1项目。

我添加了对最新的自动配置5.0.0

package的引用。

我已经通过简单的JSON配置创建了接口,实现和测试应用程序(this guide之后:

{
  "components": [
    {
      "type": "Implementations.ImplementationN, Implementations",
      "services": [
        {
          "type": "Interfaces.InterfaceN, Interfaces"
        }
      ]
    }
  ]
}

我使用以下代码(根据同一指南):

using Interfaces;
using Autofac;
using Autofac.Configuration;
using Microsoft.Extensions.Configuration;
using System;

------------------------
var config = new ConfigurationBuilder();
config.AddJsonFile("config.json");

var module = new ConfigurationModule(config.Build());

var builder = new ContainerBuilder();    
builder.RegisterModule(module);

var container = builder.Build();
------------------------

但是我得到了System.InvalidOperationException

类型'Implementations.ImplementationN,实现'找不到。它可能需要组装资格,例如“ MyType,MyAssembly”

我已将代码上传到GitHub。任何帮助表示赞赏。

P.S。

Visual Studio 2019 16.4.5 Enterprise,Windows 10 1909 x64 Professional

UPDATE:

要澄清更多-我的最终目标是让Interface.dll没有特殊引用,Implementation.dll仅引用Interface.dll和< [Test.exe仅引用Interface.dll(当然还有Autofac软件包)。我正在尝试设置一个使用Autofac 5 IoC容器的简单.NET Core 3.1项目。我添加了对最新的Autofac.Configuration 5.0.0包的引用。我已经创建了接口,实现和...
c# .net-core dependency-injection inversion-of-control autofac
1个回答
0
投票
问题是主项目正在寻找您输入到config.json文件中的类型,但是它没有对实现项目的引用,因此找不到该模块。我的解决方案是,首先添加对实现项目的引用,然后在实现项目中创建一个autofac模块(您需要在实现的实现中使用autofac lib进行项目设计,所以最好为所有项目都使用共享内核或简单共享项目):] >

public class ImplementationModule : Module { protected override void Load(ContainerBuilder builder) { builder.RegisterType<ImplementationN>().As<InterfaceN>(); } }

© www.soinside.com 2019 - 2024. All rights reserved.