AutoFac使用

AutoFac使用AutoFac使用方法设置读取配置文件的方法AutoFacConfig.cs:需要安装引用Autofac3.5.2Autofac.Configuration3.3.0=>ConfigurationSettingsReaderAutofac.Owin4.0.0Autofac.WebApi24.1.0Autofac.WebApi2.Owin4.0.0代码publicsta…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

AutoFac使用方法

设置读取配置文件的方法
AutoFacConfig.cs:
需要安装引用
Autofac3.5.2
Autofac.Configuration3.3.0 =>ConfigurationSettingsReader
Autofac.Owin4.0.0
Autofac.WebApi24.1.0
Autofac.WebApi2.Owin4.0.0
代码

public static class AutoFacConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //AutoFac4.0以上版本才支持此方法
            //var builder = new ContainerBuilder();
            //builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
            //builder.RegisterControllers(Assembly.GetExecutingAssembly());
            //builder.RegisterControllers();
            //var container = builder.Build();
            //DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

           var builder = new ContainerBuilder();

            // 家在所有程序集
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());    // IHttpController in current assembly
            //builder.RegisterModule<ApiControllerModule>();                      // IHttpController in separate assemblies
            builder.RegisterModule(new ConfigurationSettingsReader());
    
            builder.RegisterWebApiFilterProvider(config);
    
            // Set the dependency resolver to be Autofac
            var container = builder.Build();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    
    }
}

Jetbrains全家桶1年46,售后保障稳定

注册AutoFac

protected void Application_Start(object sender, EventArgs e)
 {
      AreaRegistration.RegisterAllAreas();
      GlobalConfiguration.Configure(WebApiConfig.Register);
      AutoFacConfig.Register(GlobalConfiguration.Configuration);
      //GlobalConfiguration.Configure(AutoFacConfig.Register);
  } 

配置文件
添加引用
配置autofac

    <configSections>
        <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
      </configSections>
      <autofac>
    <components>
	  <!—type=实现 service=接口 格式  MyType MyAssembly-->
      <!--Business Registration of Controllers -->
      <!--<component type="SAC.CMD.Http.Controller.FillingDataController, SAC.CMD.Http.Controller" />-->
      
      <!--Business Registration of Services -->
      <component type="SAC.CMD.Service.FillingDataService, SAC.CMD.Service" service="SAC.CMD.IService.IFillingDataService, SAC.CMD.IService"/>
      
      <!--Business Registration of Repositories -->
      <component type="SAC.CMD.Repository.FillingDataRepository, SAC.CMD.Repository" service="SAC.CMD.IRepository.IFillingDataRepository, SAC.CMD.IRepository" />
      
    </components>
  </autofac>

注:转载请注明出处

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215558.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pytest parametrize fixture_参数化数据

    pytest parametrize fixture_参数化数据前言当某个接口中的一个字段,里面规定的范围为1-5,你5个数字都要单独写一条测试用例,就太麻烦了,这个时候可以使用pytest.mark.parametrize装饰器可以实现测试用例参数化。官方示

    2022年7月30日
    7
  • java hashmap和treemap_java中treemap和hashmap的区别是什么?

    java hashmap和treemap_java中treemap和hashmap的区别是什么?区别 1 HashMap 中元素是没有顺序的 TreeMap 中所有元素都是有某一固定顺序的 2 HashMap 继承 AbstractMap 类 是基于 hash 表实现的 TreeMap 继承 SortedMap 类 是基于红黑树实现的 TreeMap 和 HashMap 的区别 Map 在数组中是通过数组下标来对其内容进行索引的 而 Map 是通过对象来对对象进行索引的 用来索引的对象叫键 key 其对应的对象叫值 va

    2025年8月24日
    3
  • 在win10+Ubuntu双系统下,完美卸载Ubuntu

    在win10+Ubuntu双系统下,完美卸载Ubuntu背景机器:惠普暗影精灵3win10与Ubuntu均为UEFI分区清除ubuntu系统使用软件:diskgenius选择Ubuntu系统使用的几个分区,EFI分区也需要勾选若不确定Ubuntu系统分区,可通过【Windows+X】选择磁盘管理,通过分区大小进行对比判断我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点…

    2022年5月30日
    31
  • delphi中listview选择整行

    delphi中listview选择整行修改RowSelect属性为真

    2022年7月16日
    19
  • 退出vi退出不了_vim怎么保存

    退出vi退出不了_vim怎么保存1.提示没有文件名2.强制退出(!wq)后再次输入依然进入vi模式故障原因:直接vi后未加文件名

    2022年9月30日
    3
  • js特殊符号正则表达式_js正则表达式判断特殊字符

    js特殊符号正则表达式_js正则表达式判断特殊字符JavaScript正则表达式功能:搜索、替换、判断JavaScript正则表达式格式:/正则表达式主体/修饰符JavaScript正则表达式的修饰符:i:忽略大小写g:全局匹配m:多行匹配JavaScript中正则表达式应用场景:搜索功能(字符串方法)search()方法参数为字符串或者是正则表达式返回结果为匹配成功的索引值,如果没有,返回-1替换功能(字符串方法)r…

    2022年9月13日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号