autofac

Adding services after container has been built

北慕城南 提交于 2019-11-29 20:22:27
Is it possible to register a service at run-time, meaning after the ContainerBuilder has been built and the Container has been created (and ContainerBuilder disposed of)? Peter Lillevold Yes you can, using the Update method on ContainerBuilder : var newBuilder = new ContainerBuilder(); newBuilder.Register...; newBuilder.Update(existingContainer); wezzix Since ContainerBuilder.Update has been deprecated, the new recommendation is to use child lifetime scope. Adding Registrations to a Lifetime Scope Autofac allows you to add registrations “on the fly” as you create lifetime scopes. This can help

ASP.NET MVC 3, Action Filters, and Autofac Dependency Injection

╄→尐↘猪︶ㄣ 提交于 2019-11-29 20:15:57
问题 On ASP.NET MVC 2 I have an ActionFilterAttribute called [Transaction] that starts an NHibernate transaction before executing the action and commits or rolls it back afterward, depending on whether or not an exception was thrown. The ISession instance is HttpRequestScoped() and injected by Autofac. It looks like this and works great: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public sealed class TransactionAttribute : ActionFilterAttribute { private ITransaction

Autofac composite pattern

两盒软妹~` 提交于 2019-11-29 20:01:39
问题 I noticed I quite often need to implement composite pattern. For example: interface IService { ... } class Service1 : IService { ... } class Service2 : IService { ... } class CompositeService : IService { public CompositeService(IEnumerable<IService> services) { ... } ... } I want to register CompositeService as IService in container and have dependencies injected. (looks somewhat similar to Decorator but decorating set of services instead of only one) What's the best way to do it in autofac?

Caliburn.Micro. Automatically call eventaggregator.Subscribe() for IHandle implementors with Autofac

爷,独闯天下 提交于 2019-11-29 19:37:19
问题 In Caliburn.Micro documentation the authors mention such possibility: documentation link IHandle inherits from a marker interface IHandle. This allows the use of casting to determine if an object instance subscribes to any events. This enables simple auto-subscribing if you integrate with an IoC container. Most IoC containers (including the SimpleContainer) provide a hook for being called when a new instance is created. Simply wire for your container’s callback, inspect the instance being

Autofac and Quartz.Net Integration

一曲冷凌霜 提交于 2019-11-29 17:37:30
问题 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

Autofac: Hiding multiple contravariant implementations behind one composite

此生再无相见时 提交于 2019-11-29 17:17:23
问题 I was triggered by this SO question about (.NET 4.0) covariance and contravariance support for Autofac, and now I'm trying to achieve something similar, but without any luck. What I am trying to achieve is configure Autofac in such way that when I resolve a single concrete IEventHandler<TEvent> (for the sake of demonstration using container.Resolve , but normally of course using constructor injection), Autofac will return me a MultipleDispatchEventHandler<TEvent> that wraps all registered

Get same instance of a component registered with Autofac as InstancePerLifetimeScope in Global.asax methods as is injected into a controllers?

爱⌒轻易说出口 提交于 2019-11-29 16:02:55
I have a situation where I need to manually instantiate some objects in Application_BeginRequest that are dependent on some of the same components that I've registered with Autofac. I'd like to use the same instances of components that I've registered with Autofac with InstancePerLifetimeScope for injection into my MVC and WebAPI controllers. My config for both MVC and Web API works as expected, and an example of a component registration looks like so: builder.Register(c => new MyDbContext()).AsSelf().InstancePerLifetimeScope(); Now I want to use that same instance in the class I'm

Binding autofac with webapi using generic repository

吃可爱长大的小学妹 提交于 2019-11-29 15:35:45
I am trying to use autofac with a repository and I am trying to add a little generics to try reducing the amount of duplicate code I am writing.However I am going round in circles trying to get autofac to work for me So I created a domainservice and interface that handles our the standard crud operations public class DomainService<T>:IDomainService<T> { protected readonly IDomainService<T> Repository; public DomainService(IDomainService<T> repository) { Repository = repository; } public IQueryable<T> GetQueryable() { return Repository.GetQueryable(); } public virtual Task<T> Add(T entity) {

Explicit resolving of ILog in Autofac when using with Log Injection Module

狂风中的少年 提交于 2019-11-29 15:19:18
问题 I use the following code in order to register log4net for all the classes that need it. public class LogInjectionModule : Module { private readonly string _configPath; public LogInjectionModule(string configPath) { _configPath = configPath; } protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration) { XmlConfigurator.Configure(new FileInfo(_configPath)); registration.Preparing += OnComponentPreparing; } private static void

FxCop indirect reference to System.Core version 2.0.5.0

自作多情 提交于 2019-11-29 14:42:00
I've got a project that uses Autofac, and the WebAPI integration We run FxCop over our solution, and on opening autofac, I get the following: The following indirectly-referenced assembly could not be found. This assembly is not required for analysis, however, without it, analysis results could be incomplete. This assemble was referenced by Autofac.dll System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes This causes the following error on our CI server: FxCop return code (516): PROJECT_LOAD_ERROR ASSEMBLY_REFERENCES_ERROR Is there a way to tell FxCop