.net core .NET核心控制台应用程序服务.AddAutofac()不工作
我正试图使用Microsoft.Extensions.DependencyInjection和.net core .NET核心控制台应用程序服务.AddAutofac()不工作,.net-core,console-application,autofac,autofac-configuration,.net Core,Console Application,Autofac,Autofac Configuration,我正试图使用Microsoft.Extensions.DependencyInjection和Autofac.Extensions.DependencyInjection软件包将Autofac与.NET Core控制台应用程序一起使用,但我的模块中的加载方法从未被调用,并导致在解析过程中程序为空。我希望在调用AddAutofac()扩展方法或构建服务提供者时加载它们 using Autofac; using Autofac.Extensions.DependencyInjection; usin
Autofac.Extensions.DependencyInjection
软件包将Autofac与.NET Core控制台应用程序一起使用,但我的模块中的加载方法从未被调用,并导致在解析过程中程序
为空。我希望在调用AddAutofac()
扩展方法或构建服务提供者时加载它们
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
public class Startup
{
private static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddAutofac(builder =>
{
builder.RegisterModule(new MyFirstModule(configuration));
builder.RegisterModule(new MySecondModule(configuration));
});
using (var serviceProvider = services.BuildServiceProvider())
{
var program = serviceProvider.GetService<Program>();
program.Start();
}
}
}
使用Autofac;
使用Autofac.Extensions.DependencyInjection;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用制度;
使用System.Collections.Generic;
使用System.IO;
公营创业
{
私有静态void Main(字符串[]args)
{
var services=newservicecolection();
services.AddAutofac(生成器=>
{
注册模块(新的MyFirstModule(配置));
RegisterModule(新的MySecondModule(配置));
});
使用(var serviceProvider=services.BuildServiceProvider())
{
var program=serviceProvider.GetService();
program.Start();
}
}
}
根据,当AddAutofac
方法被添加到文档中时,它也被拆分为和。显然,AddAutofac
是特定于ASP.NET核心应用程序的(因此不同的文档)
对于,注册应如下所示:
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
public class Startup
{
private static void Main(string[] args)
{
var services = new ServiceCollection();
var builder = new ContainerBuilder();
// Once you've registered everything in the ServiceCollection, call
// Populate to bring those registrations into Autofac. This is
// just like a foreach over the list of things in the collection
// to add them to Autofac.
containerBuilder.Populate(services);
// Make your Autofac registrations. Order is important!
// If you make them BEFORE you call Populate, then the
// registrations in the ServiceCollection will override Autofac
// registrations; if you make them AFTER Populate, the Autofac
// registrations will override. You can make registrations
// before or after Populate, however you choose.
builder.RegisterModule(new MyFirstModule(configuration));
builder.RegisterModule(new MySecondModule(configuration));
// Creating a new AutofacServiceProvider makes the container
// available to your app using the Microsoft IServiceProvider
// interface so you can use those abstractions rather than
// binding directly to Autofac.
var container = containerBuilder.Build();
var serviceProvider = new AutofacServiceProvider(container);
var program = serviceProvider.GetService<Program>();
program.Start();
}
}
使用Autofac;
使用Autofac.Extensions.DependencyInjection;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用制度;
使用System.Collections.Generic;
使用System.IO;
公营创业
{
私有静态void Main(字符串[]args)
{
var services=newservicecolection();
var builder=new ContainerBuilder();
//在ServiceCollection中注册所有内容后,请致电
//填充以将这些注册带入Autofac。这是
//就像一个在收集物品清单上的foreach
//将它们添加到Autofac。
containerBuilder.Populate(服务);
//进行Autofac注册。订单很重要!
//如果在调用Populate之前创建它们,则
//ServiceCollection中的注册将覆盖Autofac
//注册;如果在填充后进行注册,则Autofac
//注册将覆盖。您可以进行注册
//在填充之前或之后,由您选择。
注册模块(新的MyFirstModule(配置));
RegisterModule(新的MySecondModule(配置));
//创建新的AutofacServiceProvider将使容器
//使用Microsoft IServiceProvider可用于您的应用程序
//接口,以便您可以使用这些抽象,而不是
//直接绑定到Autofac。
var container=containerBuilder.Build();
var serviceProvider=新的AutofacServiceProvider(容器);
var program=serviceProvider.GetService();
program.Start();
}
}
在dotnet核心应用程序中,您的入口点应该是Program.cs
。我也不知道为什么你有一个私有的Main方法。但无论如何,请遵循以下步骤
Autofac文档说明:
您不必使用Microsoft.Extensions.DependencyInjection。如果你
正在编写需要它的.NET核心应用程序,或者如果您没有使用
您可以使用其他库提供的任何DI扩展
直接使用Autofac
实际上,您可以使用Autofac而无需扩展。请参阅dotnet核心控制台应用程序的一个非常基本的示例。确保安装了nuGet软件包Autofac(当前版本4.8.1)
我们可以有一个界面:
public interface IPrintService
{
void Print();
}
以及该接口的一个实现
public class PrintService
: IPrintService
{
public void Print()
{
Console.WriteLine("I am a print service injected");
}
}
然后,我们可以有一个类,在这个类中,我们通过构造函数注入一个IPrintService
public class Client
{
public Client(IPrintService printService)
{
printService.Print();
}
}
最后,在主控制台程序中,我们可以配置Autofac并注册服务。请注意,我决定自动激活一个客户端
实例,只是为了演示一个示例并证明Autofac正在注入所需的实现
class Program
{
static void Main(string[] args)
{
ConfigureIoCc();
Console.WriteLine("Using Autofac");
Console.ReadKey();
}
public static IContainer ConfigureIoCc()
{
var builder = new ContainerBuilder();
builder
.RegisterType<PrintService>()
.As<IPrintService>();
builder
.RegisterType<Client>()
.AsSelf()
.AutoActivate(); //to automatically instantiate it
var container = builder.Build();
return container;
}
}
类程序
{
静态void Main(字符串[]参数)
{
配置IOCC();
Console.WriteLine(“使用自动传真”);
Console.ReadKey();
}
公共静态IContainer配置IOCC()
{
var builder=new ContainerBuilder();
建设者
.RegisterType()
.As();
建设者
.RegisterType()
.AsSelf()
.AutoActivate();//自动实例化它
var container=builder.Build();
返回容器;
}
}
当前状态下的问题不完整,因此不清楚。阅读问题,然后提供一个可以用来重现问题的答案,以便更好地理解所问的问题。我不确定你会提到哪些其他细节?NET核心服务容器配置似乎非常常见,但我从未在没有IWebHostingEnvironment的WebApi项目之外进行过配置。