autofac

How to wire events with methods using Autofac?

有些话、适合烂在心里 提交于 2019-11-28 00:52:49
问题 Is it possible to wire events to methods with Autofac instead of whole object via interfaces/classes (through constructor and property injection). I want to bind at function level instead of type level. Programmatically I expect the following job to be done (in C#): someType.Output += someOtherType.Input; For example Spring.net does support the following construct to achieve that: <object id="SomeType" type="Whatever.SomeType, Whatever" /> <object id="SomeOtherType" type="Whatever

Autofac - The request lifetime scope cannot be created because the HttpContext is not available - due to async code?

回眸只為那壹抹淺笑 提交于 2019-11-27 23:36:18
Short Question: Same as this unanswered problem Long Question: I just ported some code over from an MVC 4 + Web Api solution that was using Autofac into my new solution which is also using Autofac but only with Web Api 2 (no MVC 5.1 project, just a web api). In my previous solution I had MVC4 and Web Api so I had 2 Bootstrapper.cs files, one for each. I copied over just the Web Api bootstrapper for the new project. Now I have 2 other projects in the new solution that need to pull a dependency. Lets just assume I have to use DependencyResolver.Current.GetService<T>() despite it being an anti

Configuring an Autofac delegate factory that's defined on an abstract class

时光怂恿深爱的人放手 提交于 2019-11-27 21:56:51
问题 I'm working on a C# project. I'm trying to get rid of a Factory class that has a large switch statement. I want to configure Autofac to be able to construct a dependency based on a parameter, thereby allowing Autofac to take the place of the Factory. I've looked at the DelegateFactories page of the Autofac wiki, but I can't figure out how to apply the pattern to an abstract class. Here's some code showing the situation: public enum WidgetType { Sprocket, Whizbang } public class SprocketWidget

Get a list of all registered objects implementing a certain interface

拜拜、爱过 提交于 2019-11-27 19:51:28
Consider the following builder.Register(c => new A()); builder.Register(c => new B()); builder.Register(c => new C()); B and C are both ISomeInterface . I would now like to get an IEnumerable of all registered objects that implement ISomeInterface . How can I accomplish this in Autofac? catflier Just tried this, works and does not depend on lifetime context: Enumerate types using Activator instead var types = con.ComponentRegistry.Registrations .Where(r => typeof(ISomeInterface).IsAssignableFrom(r.Activator.LimitType)) .Select(r => r.Activator.LimitType); Then to resolve: IEnumerable

How can I log all resolve requests to Autofac container?

旧巷老猫 提交于 2019-11-27 18:48:53
I am trying to debug some problems in a legacy code base. I think is being caused by an exception being thrown because something can't be resolved from the Autofac container. However I think the exception is getting buried somewhere and I am not seeing the root cause. I am sure something is being requested from a controller that either can't be found or something that can be found has a dependency that can't be satisfied. There aren't any guard clauses etc. so I think I am getting a null reference issue. To try and debug this I want to see all requests that aren't found in the container. Is

Resolving Generic Interface with Autofac

♀尐吖头ヾ 提交于 2019-11-27 18:31:48
Given the following code, how do I resolve the right SomeInstance in autofac? public class BaseClass {} public class SubClass1 : BaseClass {} public class SubClass2 : BaseClass {} public interface IGenericInterface<T> where T : BaseClass {} public class SomeInstance1<T> : IGenericInterface<T> where T : SubClass1 public class SomeInstance2<T> : IGenericInterface<T> where T : SubClass2 I want to choose SomeInstance1 or 2 based on the type of of the generic on the sub classes. So for example I have a collection of sub classes (SubClass1, 2....) and while iterating over them I want to choose the

NHibernate and AUTOFAC in WinForm application

拈花ヽ惹草 提交于 2019-11-27 15:23:37
问题 I'm looking for a good tutorial to configure AUTOFAC with NHibernate in a WinForm application injecting the ISession when a form is created and disposing the ISession on form close. I found a lot of MVC and ASP.NET example but none using WinForm. Can you point me in the right direction? 回答1: I would do something like this public class FormFactory { readonly ILifetimeScope scope; public FormFactory(ILifetimeScope scope) { this.scope = scope; } public TForm CreateForm<TForm>() where TForm :

Automatic factory with Common.Logging and Autofac?

笑着哭i 提交于 2019-11-27 14:59:54
问题 I would like to inject ILog into my classes, not an ILoggerFactoryAdapter, but the ILoggerFactoryAdapter needs the name of the calling class (the class that wants to log something, so i can be properly categorized) so can Autofac somehow identify the class which are requesting the ILog and automaticly create the ILog from the factory? 回答1: Bailey Ling came up with a great approach that doesn't use stack walking - see post here: http://groups.google.com/group/autofac/msg/704f926779cbe8b3 回答2:

Autofac Scanning Assemblies for certain class type

人盡茶涼 提交于 2019-11-27 14:49:06
问题 I've started using Autofac and want to scan some DLL's and get Autofac to register some of the classes within them. The classes that I'm interested in all inherit from a PluginBase class but the below code doesn't seem to be registerting them. Can anyone help? var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(assemblies) .Where(t => t.BaseType == typeof(PluginBase)) .AsImplementedInterfaces() .AsSelf(); var container

MVC Web API not working with Autofac Integration

痴心易碎 提交于 2019-11-27 14:39:34
I used the MVC integration from autofac like this: ... var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); But now I want to recreate the solution with the new Web Api RTM. And I want to use the new AutofacWebApiDependencyResolver class. But if I do this with AutofacWebApiDependencyResolver i got this error: The type Autofac.Integration.WebApi.AutofacWebApiDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator. I read that i have to do this now for setting the resolver: GlobalConfiguration