当前位置: 移动技术网 > IT编程>开发语言>.net > Windows服务使用Windsor容器

Windows服务使用Windsor容器

2019年05月25日  | 移动技术网IT编程  | 我要评论

王爷爱上青楼里的我,youtube解禁,垂钓之王攻略

该文章是系列文章 基于.netcore和abp框架如何让windows服务执行quartz定时作业 的其中一篇。

windsor是abp框架自带的ioc容器
关于什么是ioc,你可以bing或者google一下,英文不错的话推荐看一看 。
更多关于castle windsor 可以阅读下列文章

现在比较常用的di组件可以访问awesome系列查看:

微软自带di替换为castle windsor

问题点在于abp框架使用的是castle windsor,而微软提供的官方容器是microsoft.extensions.dependencyinjection

这就涉及到一个使用castle windsor替换microsoft.extensions.dependencyinjection的问题。
castle windsor提供了注册的方法,自己实现的话,可以通过循环访问微软自带di容器,将其一一注册到castle windsor容器。

也可以nuget引用一个包:castle.windsor.msdependencyinjection,源码查看:

代码改自如何在控制台应用(.net core)使用appsettings.json配置,需要再添加两个包

microsoft.extensions.hosting
castle.windsor.msdependencyinjection

需要添加以下代码

        public iconfiguration appconfiguration { get; set; }

        public override void preinitialize()
        {
            var host = new hostbuilder().configureappconfiguration((hostcontext, configapp) =>
            {
                var hostingenvironment = hostcontext.hostingenvironment;
                appconfiguration = appconfigurations.get(hostingenvironment.contentrootpath, hostingenvironment.environmentname);
            }).configureservices((hostcontext, services) =>
            {
                services.addsingleton(appconfiguration);

                windsorregistrationhelper.createserviceprovider(iocmanager.ioccontainer, services);
            });

            host.build();
        }

如何使用castle windsor

  • 构造函数
  • iocmanager.instance.ioccontainer.resolve()

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网