【发布时间】:2015-05-30 21:47:37
【问题描述】:
我正在尝试让 autofac 与我正在开发的 mvc6 应用程序一起工作。我找到了this blog article,但它似乎有点过时了。看起来它使用的是 beta3 位
我正在使用这个 clr 版本
1.0.0-beta5-11911
我的项目有这两个参考
"Autofac": "4.0.0-alpha2",
"Autofac.Dnx": "4.0.0-alpha2",
文章里面讲了如何修改startup.cs
// Create the Autofac container builder.
var builder = new Autofac.ContainerBuilder();
// Add any Autofac modules or registrations.
builder.RegisterModule(new AutofacModule());
// Populate the services.
builder.Populate(services);
// Build the container.
var container = builder.Build();
return container.Resolve<IServiceProvider>();
上面的代码抱怨builder.Populate(services);给我一个错误
类型“IServiceDescriptor”在未引用的程序集中定义。您必须添加对程序集“Microsoft.Framework.DependencyInjection.IServiceDescriptor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”的引用。
根据我的研究,它看起来在 beta4 DependencyInjection.IserviceDescriptor 中被删除了。
有其他人设法让 autofac 使用最新的 beta5 位吗?
【问题讨论】:
-
稍作修改即可使用 var builder = new ContainerBuilder(); // 创建容器并使用默认应用程序服务作为后备 AutofacRegistration.Populate(builder, services); // builder.RegisterType
().As ().InstancePerLifetimeScope(); builder.Register(c => new Logger()) .As () .InstancePerLifetimeScope(); -
我们仍在努力支持 beta 5。我们也需要它来支持配置。检查 Autofac MyGet 提要以获取最新信息;在 VS 也能很好地与 beta 5 一起工作之前,我们可能不会推送到 NuGet。
-
我遇到了同样的问题。我正在使用来自 myget 提要的 mvc6 beta5 + 最新版本的 Autofac,它导致以下异常:
System.MissingMethodException Method not found: 'Boolean Microsoft.Framework.DependencyInjection.ServiceCollectionExtensions.TryAdd(Microsoft.Framework.DependencyInjection.IServiceCollection, Microsoft.Framework.DependencyInjection.ServiceDescriptor)'. -
正如这篇博文alexmg.com/… 所述,您需要添加包
Autofac.Framework.DependencyInjection而不是Autofac.Dnx
标签: c# asp.net dependency-injection autofac asp.net-core-mvc