autofac

Is it possible to get container type in AutoFac

北慕城南 提交于 2019-12-03 08:49:13
For example, I have registered class C1 with one parameter in constructor of type System.Type . I have another class (C2) with injected parameter of type C1. And I want receive typeof(C2) automatically in C1 constructor. Is it possible in some way? Example code: public class C1 { public C1(Type type) {} // ... } public class C2 { public C2(C1 c1) {} // ... } // Registration containerBuilder.Register(???); containerBuilder.Register<C2>(); Nicholas Blumhardt This should do it: builder.RegisterType<C1>(); builder.RegisterType<C2>(); builder.RegisterModule(new ExposeRequestorTypeModule()); Where:

Autofac with MVC4: controller does not have a default constructor

匿名 (未验证) 提交于 2019-12-03 08:35:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've been working with Autofac in MVC3 and love it. Now I am trying to implement it with MVC4. I installed the pre-release versions of Autofac MVC4 and Autofac WebApi through the Package Manager Console (Install-Package Autofac.Mvc4 -Pre and Install-Package Autofac.WebApi -Pre) I adjusted my IoC container as following: private static void SetAutofacContainer() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder

How to register many for open generic in Autofac

一笑奈何 提交于 2019-12-03 08:24:32
问题 I'm new to Autofac (not to DI ). Here is the situation: I have these interfaces: public interface IQuery<out TResult> : IQuery { } public interface IQueryHandler<in TQuery, out TResult> where TQuery : IQuery<TResult> { TResult Handle(TQuery query); } and there is a lot of implementation of them in my solution: class GetPersonQuery : IQuery<PersonModel> { } class GetPersonQueryHandler : IQueryHandler<GetPersonQuery, PersonModel> { } class GetArticleQuery : IQuery<ArticleModel> { } class

Using Autofac to inject log4net into controller

走远了吗. 提交于 2019-12-03 08:14:56
Trying to use Autofac to inject a log4net class into my controller, but I get the following exception: None of the constructors found with 'Public binding flags' on type 'MvcApplication6.Controllers.HomeController' can be invoked with the available services and parameters: Cannot resolve parameter 'log4net.ILog logger' of constructor 'Void .ctor(log4net.ILog)'. I have created a module to inject the Log class using the correct type: public class LogInjectionModule : Module { protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration) {

Resolve type without creating object

十年热恋 提交于 2019-12-03 07:58:12
Here's my problem: I have a container where I register concrete types as interfaces. builder.RegisterType<DeleteOrganization>().As<IDeleteOrganization>(); I'm implementing a SerializationBinder for a serialization project I'm doing and the BindToType method that I need to implement wants me to return a Type object. The BindToType method gives me an assemblyName and typeName (both strings) to help me create a type object. What I want to do is if the typeName is an interface, I want to ask Autofac what the concrete implementation Type is for that interface Type without actually having it create

Autofac Lifetimes and the Default Provider within a Matching Lifetime Scope

不问归期 提交于 2019-12-03 07:33:49
I have an ASP.NET MVC web application using Autofac for dependency injection. Occasionally, this web application will start a thread to do some work separate from the request thread. When this background thread starts up, it establishes a new Autofac lifetime scope from the root container and runs some action. public IAsyncResult Run<T>(Action<T> action) { var NewTask = System.Threading.Tasks.Task.Factory.StartNew(() => { using (var Scope = Runtime.Container.BeginLifetimeScope()) { var Input = Scope.Resolve<T>(); action(Input); } }); return NewTask; } One of my dependencies registered with

In Autofac how do I change the instance that is registered after Build has been called?

拈花ヽ惹草 提交于 2019-12-03 06:37:01
问题 So lets say i have this code var builder = new ContainerBuilder(); builder.RegisterInstance(new MyType()); var container = builder.Build(); Then some time later I want to change the instance of MyType for all future resolves that are called on container . 回答1: At the time you want to change the registration, create a new ContainerBuilder , register the new instance, and call Update passing in the container: // at some later point... builder = new ContainerBuilder(); builder.RegisterInstance

How do I resolve Dependency Injection in MVC Filter attributes

可紊 提交于 2019-12-03 06:28:55
I have a custom attribute class derived from AuthorizationAttribute, which performs custom security on controller actions. The OnAuthorizationCore method depends on various other components (e.g. DAL) in order to ajudicate whether a user can invoke an action. I'm using Autofac for dependency injection. The ExtensibleActionInvoker claims to be able to perform property injection on action filters. Setting an attribute's properties at runtime (which seems like a bad idea) will work in a simple unit test, but in a busy, multi-threaded web server it's bound to go wrong, and so this idea seems like

Autofac and ASP.NET Web API ApiController

北战南征 提交于 2019-12-03 06:28:38
I have been using autofac with MVC 3 for a while and love it. I recently upgraded a project to MVC 4 and everything seems to be working except for Web Api ApiControllers. I am getting the following exception. An error occurred when trying to create a controller of type 'MyNamespace.Foo.CustomApiController'. Make sure that the controller has a parameterless public constructor. This seems to me to be an issue with DI via autofac. Am I missing something or is there something in the works. I know, MVC4 just came out and is a beta so I don't expect much but figured I could be missing something.

Autofac: How to limit the lifetime of an IDisposable object without passing around the IoC container

感情迁移 提交于 2019-12-03 06:16:15
I'm currently learning how to use Autofac, and I'm stuck with disposing IDisposable objects deterministically. Let me first present the situation before I'll state my problem. Starting position: Let's say my object model is defined through the following interfaces: interface IApple : IDisposable { void Consume(); } interface IHorse { void Eat(IApple apple); // is supposed to call apple.Consume() } interface IHorseKeeper { void FeedHorse(); // is supposed to call horse.Eat(apple) // where 'horse' is injected into IHorseKeeper // and 'apple' is generated by IHorseKeeper on-the-fly } Further, I