autofac

Autofac Cannot resolve dependency in module

筅森魡賤 提交于 2019-12-11 04:05:53
问题 I'm new to AutoFac and have what I thought should be a simple scenario. var builder = new ContainerBuilder(); builder.Register(c => new EventLogLogger()).As<ILogger>(); builder.RegisterModule(new ConfigurationSettingsReader("autofac")); builder.Build(); I register my ILogger service and then instruct the container to register modules based on values from my app config. The module being loaded has a dependency on ILogger. public class LocalActorSystemModule : Module { private ILogger m_Logger;

Using KeyFilter with ASP.NET Core 2.0

冷暖自知 提交于 2019-12-11 04:04:49
问题 I have problem to using KeyFilter attribute in a simple ASP.NET Core 2.0 WebAPI application. <PackageReference Include="Autofac" Version="4.6.1" /> <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.0" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> Program.cs: public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args

Applying generic decorators conditionally in Autofac based on configuration values

半世苍凉 提交于 2019-12-11 03:39:58
问题 I have an application with an command/handler based architecture. I have the following interface: public interface ICommandHandler<TCommand> { void Handle(TCommand command); } There are many non-generic implementations of this interface. Those implementations are wrapped by generic decorators such as: public class ProfilingCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand> { private readonly ICommandHandler<TCommand> decoratee; public ProfilingCommandHandlerDecorator

Autofac with ASP.NET Identity in MVC 5 does not validate Security Stamp in OWIN pipeline

做~自己de王妃 提交于 2019-12-11 03:35:00
问题 I set up AutoFac to work with ASP.NET Identity in MVC 5. Everything seemed to work fine on surface, i.e. users could create accounts and log in. But then I discovered that the users do not get logged out when Security Stamp is changed. Either by brute force in AspNetUsers table or by users changing password and expecting to be logged out in other browser. This is how I set up AutoFac by following this unofficial article. public void Configuration(IAppBuilder app) { var builder = new

How to register a type with autofac after container creation

风流意气都作罢 提交于 2019-12-11 03:31:44
问题 I have an infrastructure singleton that I would like resolved out of autofac At container creation I register AppPaths as a singleton However, for a variety of reasons (testing, a few infrastructure things) I would like to be able to swap that instance out with a new one during runtime. Let's say a derived type class AppPaths2 : AppPaths . I can't find the API to do this. I can use CommentServiceLocator to get an instance of IComponentContext but I don't see a way to resolve stuff from there.

How to inject NLog using Autofac in ASP.NET Webforms

别说谁变了你拦得住时间么 提交于 2019-12-11 03:22:16
问题 I have an ASP.NET Webforms project that I've been recently converting to use Dependency Injection via Autofac. It has all been going well, that is until I tried to inject my NLog instances. I can't work out how to perform the equivalent of the static GetCurrentClassLogger() method calls with the property injected Logger object. Here is my sample code illustrating the problem: Global.asax.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using Autofac;

Dependency Injection for Presenter

为君一笑 提交于 2019-12-11 03:03:59
问题 I have a Presenter that takes a Service and a View Contract as parameters in its constructor: public FooPresenter : IFooPresenter { private IFooView view; private readonly IFooService service; public FooPresenter(IFooView view, IFooService service) { this.view = view; this.service = service; } } I resolve my service with Autofac: private ContainerProvider BuildDependencies() { var builder = new ContainerBuilder(); builder.Register<FooService>().As<IFooService>().FactoryScoped(); return new

Error after update Autofac.Mvc5 from 3.3.2 to 3.3.3

你离开我真会死。 提交于 2019-12-11 02:44:36
问题 I have a Error after update Autofac.Mvc5 from 3.3.2 to 3.3.3 I posted my issue to github https://github.com/autofac/Autofac/issues/572#issuecomment-63236738 and got response that I need to ask here :) Below my situation: What I have: // Setup DI as default MVC controller factory DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); I need this to use in injected property of Custom Membership Provider protected IMembershipService MembershipService { get { return

Inject Specific Type With Autofac

僤鯓⒐⒋嵵緔 提交于 2019-12-11 02:23:08
问题 I want to inject specific type when some conditions are met. For example, i have got an interface like below. public interface IMyInterface{ } And also two classes that implement this interface public class MyClassA : IMyInterface { } and public class MyClassB : IMyInterface { } Finally i have some service class that gets a constructor parameter as IMyInterface. public class ServiceA{ private IMyInterface _interfaceClass; public ServiceA(IMyInterface interfaceClass){ _interfaceClass =

How can i have a IServiceProvider available in ValidationContext parameter of IValidatableObject.Validate method

青春壹個敷衍的年華 提交于 2019-12-11 01:44:25
问题 Controller calls IValidatableObject.Validate internally and passes a ValidationContext object as an argument. I want to use validationContext.GetService() method to get a service object and use it. I can pass this service as a dependency to controller constructor using AutoFac(DI Injection dll). How do i make it availale to the ValidationContext object? This stackoverflow question might contain the answer but i do not understand it fully : Asp.Net MVC3: Set custom IServiceProvider in