autofac

Autofac and Quartz.Net Integration

萝らか妹 提交于 2019-11-30 11:26:43
Does anyone have any experience integrating autofac and Quartz.Net ? If so, where is it best to control lifetime management -- the IJobFactory, within the Execute of the IJob, or through event listeners? Right now, I'm using a custom autofac IJobFactory to create the IJob instances, but I don't have an easy way to plug in to a ILifetimeScope in the IJobFactory to ensure any expensive resources that are injected in the IJob are cleaned up. The job factory just creates an instance of a job and returns it. Here are my current ideas (hopefully there are better ones...) It looks like most AutoFac

.Net Core 3.0依赖注入替换 Autofac

寵の児 提交于 2019-11-30 09:53:39
今天早上,喜庆的更新VS2019,终于3.0正式版了呀~ 有小伙伴问了一句Autofac怎么接入,因为Startup.ConfigureServices不能再把返回值改成IServiceProvider了,原来的替换依赖注入容器就不可行了,我随口说了一下Host上面. UseServiceProviderFactory 本以为就这么简单,他问我要个例子,自己折腾了一下,发现事情没有这么简单 . UseServiceProviderFactory<TContainer>要搭配Startup里面写一个ConfigureContainer(TContainer builder) Autofac官方的IServiceCollection.AddAutofac内部实现其实是一个单例的容器注入了IServiceProviderFactory<ContainerBuilder> 但是不在UseServiceProviderFactory<TContainer>里面配置的话,就只能 services.AddAutofac(container=> { }); 而且不能有ConfigureContainer(ContainerBuilder builder),要不然会提示 比如我们有个MyClass public class MyClass { public void Hello() {

Autofac resolve dependency in CQRS CommandDispatcher

你。 提交于 2019-11-30 08:43:19
I'm trying to implement a simple CQRS-application example. This is a structure of my "Command" part: public interface ICommand { } //base interface for command handlers interface ICommandHandler<in TCommand> where TCommand: ICommand { void Execute(TCommand command); } // example of the command public class SimpleCommand: ICommand { //some properties } // example of the SimpleCommand command handler public class SimpleCommandHandler: ICommandHandler<SimpleCommand> { public void Execute(SimpleCommand command) { //some logic } } This is interface ICommandDipatcher . It dispatches a command to its

.NET Core下自带容器IServiceCollection以及AutoFac以及AutoFac中AOP简介

强颜欢笑 提交于 2019-11-30 07:34:49
原文: .NET Core下自带容器IServiceCollection以及AutoFac以及AutoFac中AOP简介 https://www.cnblogs.com/artech/p/net-core-di-01.html 大内老A的在.NET Core下对这些的介绍,有一系列文章 https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html https://www.cnblogs.com/artech/p/dependency-injection-in-asp-net-core.html https://www.zybuluo.com/dasajia2lang/note/1481011 下面开始 在上一篇的笔记中,在.NET Freamwork中,有一个第三方容器Unity,可以实现注入,但是在.NET Core里面,有一个IServiceCollection,这个是.NET Core框架自带的一个容器,和Unity很相似,都是个容器。 下面我们新建一个控制台程序,在控制台程序中,对IServiceCollection的使用做介绍。 下面代码,是本次实例中需要注入的类型,需要用的倒是再点开来看吧 namespace Bingle.Core.Interface { public interface ITestServiceA

Autofac runtime parameters

半城伤御伤魂 提交于 2019-11-30 07:27:22
I'm new to autofac and looking to see the best practices on passing runtime values to constructor. I've read a bunch of stackoverflow questions where this is asked but none are fully fleshed out. Should we be using delegates, factory to create service etc. I know passing the container around is not the best way to accomplish this. In my particular case I have a service that access multiple dependencies, say logging, dataprovider, etc. Along with the few services being passed I also have run-time parameters I need to capture, say userid, password. The userid and password are required for the

Can multiple Autofac lifetime scopes be specified on a registration?

自古美人都是妖i 提交于 2019-11-30 07:27:10
I'm using the Autofac IoC container with the MVC4 add-on which provides the InstancePerHttpRequest lifetime scope. However within my project I have the web, web-api and background worker threads. In the following example I assume the InstancePerHttpRequest scope doesn't mean much when not originating from a web request. builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>() .InstancePerHttpRequest() I'm wondering if it is possible to do something like the following example and have the container choose the most appropriate lifetime scope? builder.RegisterType<DatabaseFactory>().As

Autofac - resolving runtime parameters without having to pass container around

守給你的承諾、 提交于 2019-11-30 06:11:17
I have a simpler "ServiceHelper" class that takes two parameters in the constructor: public ServiceHelper(ILogger<ServiceHelper> log, string serviceName) (ILogger generic wrapper for NLog that Autofac is providing just fine, and the serviceName is the name of a Windows service to control that I need to provide at runtime.) I'm having trouble wrapping my head around how to create new instances of this class at runtime passing in different service names, using Autofac. Something like this doesn't work of course since I need to specify different service names at runtime: builder.RegisterType

Autofac and Func factories

女生的网名这么多〃 提交于 2019-11-30 04:50:22
I'm working on an application using Caliburn.Micro and Autofac. In my composition root I'm now facing a problem with Autofac: I have to inject the globally used IEventAggregator into my FirstViewModel, and a second IEventAggregator that has to be used only by this FirstViewModel and it's children. My idea was to make the second one be injected as Owned<IEA> , and it works, the container provides a different instance of IEA. public FirstViewModel( IEventAggregator globalEA, IEventAggregator localEA, Func<IEventAggregator, SecondViewModel> secVMFactory) {} The problem comes when I have to

ASP.NET MVC 3, RavenDB, & Autofac Issue Plus 2 Other Autofac Questions

旧城冷巷雨未停 提交于 2019-11-30 04:11:02
问题 NOTE: There are 3 questions in here and I did not make separate questions since they are all somewhat related to the same code. I have the following code that registers the connection to my RavenDB in the Application_Start once per the application's life cycle: var store = new DocumentStore { Url = "http://localhost:8080" }; store.Initialize(); builder.RegisterInstance(store).SingleInstance(); Now this works fine and this is something that should be created only once per the application's

AutoFac: Inject NULL values

…衆ロ難τιáo~ 提交于 2019-11-30 03:41:00
问题 I want to use AutoFac to inject the current principal in the objects that need it. Suppose I have an object ( AuthorizationValidator ) that is performing security checks. It looks something like this: public AuthorizationValidator : IAuthorizationValidator { public AuthorizationValidator(IDataAccess dataAccess, IPrincipal principal) { // Save injected objects ... } public bool CheckPermission(Guid objectId, Action action) { // Check if we are authorized at all if (this.principal == null)