autofac

Autofac registers components multiple times

坚强是说给别人听的谎言 提交于 2020-01-05 04:13:11
问题 In a previous question about got I visualize the graph of my dependencies I got the foundation for the code I now use to visualize my dependency graph as it is resolved by Autofac. Running the code I get a tree that results in code like the following. Usd.EA.Bogfoering.WebApi.Controllers.BogfoerController (3851,7 ms. / 0,0 ms.) Depth: 0 Usd.EA.Bogfoering.WebApi.Controllers.BogfoerController (3851,7 ms. / 0,4 ms.) Depth: 1 Usd.Utilities.WebApi.Controllers.UnikOwinContext (0,1 ms. / 0,0 ms.)

Why am I getting error: “Cannot access disposed object” in .net core 2 with EF and AutoFac?

荒凉一梦 提交于 2020-01-04 13:40:35
问题 First the error: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'MemberContext'

Calling all ISomething instances in Autofac

和自甴很熟 提交于 2020-01-04 02:44:09
问题 I have an interface ISomething with a method Start. I want to get all implementations of this interface (in multiple assemblies, the main one and all referenced ones) and call the Start method on application start. How can I do this with Autofac 2.4.4.705? 回答1: you can resolve IEnumerable<ISomething> and call Start for each of them Forgot to mention, that you should first register all implementations of ISomething. Assembly[] assemblies = ...; var builder = new ContainerBuilder(); builder

ModelBinder is not invoked

最后都变了- 提交于 2020-01-03 21:09:49
问题 Per my previous question, I implemented a model binder that maps /api/v1/widgets/1,2,3 to // WidgetsController.cs: public ActionResult Show(IEnumerable<int> idArgs) { } This was working for a while, but now it is not any longer. My ModelBinder is not even being invoked at all. When my action is invoked, idArgs has a value of the empty list, even if I set its default value to null in the route, which suggests to me that the default model binder thinks it's getting a value from somewhere. The

resolving instance of class inside of method using autofac

若如初见. 提交于 2020-01-03 19:40:32
问题 Using this PipelineX class below, is there any way to resolve the filters applied to the pipline without injecting the autofac container and calling _container.Resolve(); public class PipelineX<T> : FilterBase<T>, IPipelineX<T> { private readonly IContainer _container; public PipelineX(IContainer container) { _container = container; } protected override T Process(T input) { return input; } public PipelineX<T> FilterBy<X>() { var filter = _container.Resolve(typeof(X)) as IFilter<T>; Register

AutoFac RegisterGenericDecorator not working with decorator having type constraint

二次信任 提交于 2020-01-03 18:34:30
问题 I have a generic command handler with many concrete implementations. public interface ICommandHandler<TCommand> I have a decorator with a type constraint that I want to use for multiple, but not all, implementations of the ICommandHandler public class AddNoteCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand> where TCommand : INoteCommand { private readonly ICommandHandler<TCommand> decorated; public AddNoteCommandHandlerDecorator(ICommandHandler<TCommand> decorated) { this

Autofac, IDisposable and manually calling .Resolve in certain circumstances

混江龙づ霸主 提交于 2020-01-03 17:04:50
问题 I've posted a general guideline question when it comes to IDisposable objects and using Autofac here: Dependency Injection and IDisposable. Unfortunately, I did not account for one particular scenario in our project and it's really a separate question that stands on its own, so will ask it here: I have a Repository object that manages the life of a session object inside it. Thus, Repository object is IDisposable and destroys session (Repository is injected with a factory delegate at

How do I use custom model binder that supports dependency injection in ASP.NET Core?

人盡茶涼 提交于 2020-01-03 13:39:15
问题 I am trying to use a custom model binder in MVC that I want resolved from my IoC container. The issue I am having is that I can't access my container while I am adding the MVC service, because my container isn't built yet (and I need to add MVC before building my container). Feels like a chicken/egg issue, and I am sure I am missing a simple solution. Example: services.AddMvc().AddMvcOptions(options => { options.ModelBinders.Add(serviceProvider.Resolve<CustomModelBinder>()); }); My custom

ASP.NET Autofac Singleton running twice constructor injection

拥有回忆 提交于 2020-01-03 05:06:08
问题 I'm not sure if my expectations are wrong. I am using ASP.NET MVC (not core) and Autofac. I have an image that appears on every page of my website, and I have created a service to return a random image name like so INTERFACE public interface IBreadCrumbImage { string GetImage(); } CLASS (snipped for brevity) public class BreadCrumbImage : IBreadCrumbImage { public string GetImage() { return ImageUrl; } } GLOBAL.ASAX.CS (Autofac registration) protected void Application_Start() { var builder =

Migration from ASP.NET Core's container to Autofac

雨燕双飞 提交于 2020-01-02 11:01:28
问题 I'm using ASP.NET Core and its builtin container. I want to migrate my registrations to Autofac. The Autifac docs don't have a "migration guide", so I want to be sure I'm doing things correctly. ASP.NET Core container -> Autofac ---------------------- ------- // the 3 big ones services.AddSingleton<IFoo, Foo>() -> builder.RegisterType<Foo>().As<IFoo>().SingleInstance() services.AddScoped<IFoo, Foo>() -> builder.RegisterType<Foo>().As<IFoo>().InstancePerLifetimeScope() services.AddTransient