在ASP.NET 5中,已经有了一个默认的DI,它看起来很有趣。我一直在使用MVC5的Autofac,它可以一次注册所有的程序集。以下是在Autofac中注册所有以"Service“结尾的类的示例代码。
// Autofac Configuration for API
var builder = new ContainerBuilder();
builder.RegisterModule(new ServiceModule());
...
...
builder.RegisterAssemblyTypes(Assembly.Load("IMS.Service"))
我正在使用.Net 4.5、ASP.NET MVC 5和NuGet包: Autofac 3.5.2 Autofac ASP.NET ASP.NET集成3.0.0-rc1
我有两个不同的接口实现:
public class MemoryStreamService : IStreamService { ... }
public class HttpStreamService : IStreamService { ... }
另外还有另一个通过构造函数注入使用接口的具体类:
public class MainService : IMainService
{
public MainServi
我试图使用最新的Autofac与asp.net核心2.2项目。然而,autofac的文档似乎已经过时了。我试图使用没有ConfigureContainer的autofac:
// ConfigureServices is where you register dependencies. This gets
// called by the runtime before the Configure method, below.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//
将ASP.NET中的Autofac与ContainerDisposalModule一起使用时,我如何支持需要解决组件依赖关系的触发和忽略调用?我遇到的问题是,ASP.NET请求在任务运行之前完成并处置了请求的生存期范围,因此任何需要在新线程中解析的组件都会失败,并显示消息"Instances be resolved and new cannot be created from this LifetimeScope,因为它已经被处置了“。在ASP.NET中使用Autofac支持触发和忘记呼叫的最好方法是什么?我不想延迟请求来执行某些可以在后台线程上完成的任务。
在ASP.NET Core2.0中,我在Startup类上使用了ConfigureServices方法来连接Autofac,包装现有的services注册并添加其他注册。
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Standard service registrations (ex: services.AddMvc())
// ...
// Autofac
var builder = new ContainerBuilder();
bu
在ASP.NET Core3.0的official Autofac documentation (我使用的是.NET Core3.1)中,官方文档中的startup构造函数参数类型是IHostingEnvironment public Startup(IHostingEnvironment env)
{
// In ASP.NET Core 3.0 `env` will be an IWebHostEnvironment, not IHostingEnvironment.
var builder = new ConfigurationBuilder()
.
在ASP.Net核心应用程序中,使用以下方法很容易配置Autofac:
public class Program
{
public static void Main(string[] args)
{
// ASP.NET Core 3.0+:
// The UseServiceProviderFactory call attaches the
// Autofac provider to the generic hosting mechanism.
var host = Host.CreateDefaultBuilder(args)
.U
我对控制反转和Autofac是新手。通读文档,有很多ASP.NET Web概念我没有完全理解或没有经验,这使得我很难确定我的实现中缺少了什么。 我想应用Autofac到一个现有的ASP.NET Web项目,其中有几个ApiControllers。这些控制器都共享一个父抽象类。这个抽象类有一个负责返回服务实例的方法。我希望用Autofac的依赖注入来代替这个方法。 每个ApiController继承的父级抽象类非常简单。 public abstract class BaseApiController
{
public IMyService serviceClient { get; se
我在使用带有targetframework net45的autofac v3.5.0、Autofac.Extras.CommonServiceLocator v3.2.0、Autofac.Mvc5 v3.3.2的ASP.NET MVC5应用程序时遇到错误:
No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registere