让 autofac 与 mvc6 beta5 一起工作 [英] Getting autofac to work with mvc6 beta5

查看:35
本文介绍了让 autofac 与 mvc6 beta5 一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 autofac 与我正在处理的 mvc6 应用程序一起工作.我找到了这篇博客文章但是它似乎有点过时了.看起来它使用了 beta3 位

I am trying to get autofac working with an mvc6 application I am working on. I found this blog article however it seems to be a little dated. It looks like its using the beta3 bits

我正在使用这个 clr 版本

I am using this clr version

1.0.0-beta5-11911

1.0.0-beta5-11911

我的项目有这两个参考

"Autofac": "4.0.0-alpha2",
"Autofac.Dnx": "4.0.0-alpha2",

文章里面讲了如何修改startup.cs

Within the article is talks about how to modify the 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); 给我一个错误

The above code complains about builder.Populate(services); giving me an error

类型IServiceDescriptor"是在未引用的程序集中定义的.您必须添加对程序集Microsoft.Framework.DependencyInjection.IServiceDescriptor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"的引用.

The type 'IServiceDescriptor' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.Framework.DependencyInjection.IServiceDescriptor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

根据我的研究,它看起来在 beta4 DependencyInjection.IserviceDescriptor 中被删除了.

From me research it looks like in beta4 DependencyInjection.IserviceDescriptor was removed.

有没有其他人设法让 autofac 使用最新的 beta5 位?

Has anyone else managed to get autofac working with the latest beta5 bits?

推荐答案

对于任何想要在配置下运行 AutoFac 的人,我都可以在 beta6 中使用它

For anyone who would be looking how to get AutoFac running below configuration allowed me to use it in beta6

下面是project.json的片段

Below is snippet of project.json

  "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的一部分

And then part of 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 所提到的,确保你有

As mentioned by @peco make sure you have

using Autofac.Framework.DependencyInjection

这让我很快就开始使用 AutoFac :) 希望这会有所帮助!

And that got me going notime with AutoFac :) Hope this helps!

这篇关于让 autofac 与 mvc6 beta5 一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆