【问题标题】:Getting autofac to work with mvc6 beta5让 autofac 与 mvc6 beta5 一起工作
【发布时间】: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


【解决方案1】:

对于任何想了解如何让 AutoFac 在配置下运行的人允许我在 beta6 中使用它

下面是project.json的sn-p

  "dependencies": {
"Autofac": "4.0.0-beta6-110",
"Autofac.Framework.DependencyInjection": "4.0.0-beta6-110",
"Microsoft.AspNet.Mvc": "6.0.0-beta6",
"Microsoft.AspNet.Server.IIS": "1.0.0-beta6",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta6",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta6"
},

然后是startup.cs的一部分

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        //create Autofac container build
        var builder = new ContainerBuilder();

        //populate the container with services here..
        builder.RegisterType<DemoService>().As<IProjectDemo>();
        builder.Populate(services);

        //build container
        var container = builder.Build();

        //return service provider
        return container.ResolveOptional<IServiceProvider>();
    }

正如@peco 所说,确保你有

using Autofac.Framework.DependencyInjection

这让我没有时间使用 AutoFac :) 希望这会有所帮助!

【讨论】:

  • 现在是 Autofac.Extensions.DependencyInjection :)
  • 对,对于 project.json 中的 RC1,我们需要使用这些依赖项:“Autofac”:“4.0.0-rc1-177”,“Autofac.Extensions.DependencyInjection”:“4.0.0- rc1-177"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 2016-12-13
  • 2016-10-06
  • 2011-11-30
  • 2014-02-19
  • 2011-09-03
相关资源
最近更新 更多