在我这个框架中需要引用一下几个dll

Autofac,Autofac.Configuration,Autofac.Mvc4,Autofac.WebApi


配置方面我用的是XML文件配置

在添加完引用之后,就可以配置了!下面是配置代码! 首先要弄清楚,整个MyautoFac.Web的程序入口Global.asax.cs文件中的Application_Start()方法里

 public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AutofacRegister.Register();//常规注入 -------》App_Start 文件夹下

         }

    }


App_Start 文件夹下 代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Autofac;
using Autofac.Configuration;
using Autofac.Integration.Mvc;


namespace SupervisionWin.Web
{
    public class AutofacRegister
    {
        public static void Register()
        {
            var builder = new ContainerBuilder();


            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
            builder.RegisterSource(new ViewRegistrationSource());//页面注入
            builder.RegisterFilterProvider();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
        }
    }
}


XML 配置

 然后在Web.config里填代码!如下:

        

           

           

 

        

         

         

           

           

             

             Configuration.config"section="autoFac">

             Configuration.config"section="autoFac">

           

         




      至于Configuration.config文件,我只举一个例子说明!单独新建一个