autofac

How do I create a Quartz.NET’s job requiring injection with autofac

旧城冷巷雨未停 提交于 2019-12-03 06:12:49
I am trying to get Quartz.net (2.1.2) to work with an IoC container (autofac), as I have services I need to use in the scheduled jobs. I have found similar posts on the subject, but I can't seem to find one with a specific registration example for autofac. The following post deals with the same issue I am having: How to schedule task using Quartz.net 2.0? However, the part I believe I am missing is when the answer says "And don't forget to register the job in the IoC container". I am unsure how to do this exactly, as everything I have tried so far hasn't worked. In the following example, the

autofac's Func<T> to resolve named service

萝らか妹 提交于 2019-12-03 05:11:32
问题 Given registered services: builder.RegisterType<Foo1>().Named<IFoo>("one").As<IFoo>(); builder.RegisterType<Foo2>().Named<IFoo>("two").As<IFoo>(); builder.RegisterType<Foo3>().Named<IFoo>("three").As<IFoo>(); Can I retrieve named implementations of IFoo interface by injecting something like Func<string, IFoo> ? public class SomeClass(Func<string, IFoo> foo) { var f = foo("one"); Debug.Assert(f is Foo1); var g = foo("two"); Debug.Assert(g is Foo2); var h = foo("three"); Debug.Assert(h is Foo3)

Autofac - Register multiple decorators

风流意气都作罢 提交于 2019-12-03 04:25:08
问题 Given the following: public interface ICommandHandler<in TCommand> { void Handle(TCommand command); } public class MoveCustomerCommand { } public class MoveCustomerCommandHandler : ICommandHandler<MoveCustomerCommand> { public void Handle(MoveCustomerCommand command) { Console.WriteLine("MoveCustomerCommandHandler"); } } public class TransactionCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand> { private readonly ICommandHandler<TCommand> _decorated; public

Constructor injection into a base class using autofac

喜欢而已 提交于 2019-12-03 04:14:34
问题 I have an abstract base controller which has a constructor I hoped would be populated by autofac when the controllers were built. public abstract class BaseController : Controller { protected ILogger { get; private set; } protected BaseController() { } protected BaseController(ILogger logger) { Logger = logger; } } This doesnt seem to work when I derive a controller from it. I can only get this to work when I explicitly call the constructor explicitly from the controller. Is this the correct

Is Dependency Injection possible with a WPF application?

纵然是瞬间 提交于 2019-12-03 04:14:27
问题 I want to start using dependency injection in my WPF application, largely for better unit testability. My app is mostly constructed along the M-V-VM pattern. I'm looking at autofac for my IoC container, but I don't think that matters too much for this discussion. Injecting a service into the start window seems straightforward, as I can create the container and resolve from it in App.xaml.cs. What I'm struggling with is how I can DI ViewModels and Services into User Controls? The user controls

Passing parameters to constructors using Autofac

别等时光非礼了梦想. 提交于 2019-12-03 04:06:29
问题 I'm very new to autofac so it's possible that I'm completely misusing it. Let's say I have a class that has this structure: public class HelperClass : IHelperClass { public HelperClass(string a, string b) { this.A = a; this.B = b; } } and I have two classes that use that class, but require different defaults for the constructor. The second constructor is JUST for testing purposes -- we will always want a HelperClass in the "real" app.: public class DoesSomething: IDoesSomething { public

How to inject IHttpContextAccessor into Autofac TenantIdentificationStrategy

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am migrating my multitenant application from Webapi to aspnet core. In webapi version I was using TenantIdentificationStrategy that identified tenants based on request path on HttpContext . Moving to aspnet core, I am able to wire-up autofac successfully. I am not able to figure out how to wireup the tenant strategy. I tried injecting IHttpContextAccessor in ConfigureServices as services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); and my strategy looks like this public class AssetClassIdentificationStrategy:

How do I properly register AutoFac in a basic MVC5.1 website?

岁酱吖の 提交于 2019-12-03 03:08:11
问题 AutoFac has recently been updated for MVC 5.1 but at the time of writing I find that the documentation is lacking (especially for a simple example). I would like to inject dependencies into MVC Controllers and register my own implementations for e.g. e-mail (actual sending vs print to output window) as a basic example. I'm do not know if I am missing a good resource for this and I am slight concerned that because it uses the OWIN specification that the implementation may differ for MVC5.1

FluentValidation Autofac ValidatorFactory

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to be able to provide the IComponentContext to my ValidatorFactory to resolve FluentValidation Validators. I am a little stuck. ValidatorFactory public class ValidatorFactory : ValidatorFactoryBase { private readonly IComponentContext context; public ValidatorFactory(IComponentContext context) { this.context = context; } public override IValidator CreateInstance(Type validatorType) { return context.Resolve(validatorType) as IValidator; } } How do I provide the context and register the ValidatorFactory FluentValidation.Mvc

Keyed delegate factories with runtime constructor parameters?

筅森魡賤 提交于 2019-12-03 02:56:58
Lets say I have the following service and components: public interface IService { void DoWork(); } public class ServiceA : IService { private readonly string _name; public ServiceA(string name) { _name = name; } public void DoWork() { //ServiceA DoWork implementation } } public class ServiceB : IService { private readonly string _name; public ServiceB(string name) { _name = name; } public void DoWork() { //ServiceB DoWork implementation } } Notice that each component takes a constructor parameter name . Lets also say that name is determined at runtime. I've been going through the AutoFac