autofac

Autofac and Func factories

谁说胖子不能爱 提交于 2019-11-29 01:58:21
问题 I'm working on an application using Caliburn.Micro and Autofac. In my composition root I'm now facing a problem with Autofac: I have to inject the globally used IEventAggregator into my FirstViewModel, and a second IEventAggregator that has to be used only by this FirstViewModel and it's children. My idea was to make the second one be injected as Owned<IEA> , and it works, the container provides a different instance of IEA. public FirstViewModel( IEventAggregator globalEA, IEventAggregator

Autofac dependency injection in implementation of OAuthAuthorizationServerProvider

不问归期 提交于 2019-11-29 01:15:10
I am creating a Web Api application and I want to use bearer tokens for the user authentication. I implemented the token logic, following this post and everything seems to work fine. NOTE: I am not using the ASP.NET Identity Provider. Instead I have created a custom User entity and services for it. public class Startup { public void Configuration(IAppBuilder app) { ConfigureOAuth(app); var config = new HttpConfiguration(); var container = DependancyConfig.Register(); var dependencyResolver = new AutofacWebApiDependencyResolver(container); config.DependencyResolver = dependencyResolver; app

NHibernate and AUTOFAC in WinForm application

安稳与你 提交于 2019-11-29 00:36:02
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? 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 : Form { var formScope = scope.BeginLifetimeScope("FormScope"); var form = formScope.Resolve<TForm>(); form

Automatic factory with Common.Logging and Autofac?

落爺英雄遲暮 提交于 2019-11-28 23:41:56
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? 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 Remco Schoeman I've tried to do this too, but I couldn't find a way to get access to the activation stack of

Autofac Scanning Assemblies for certain class type

…衆ロ難τιáo~ 提交于 2019-11-28 23:30:40
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 = builder.Build(); var pluginClasses = container.Resolve<IEnumerable<PluginBase>>(); //pluginClasses is

Hangfire dependency injection lifetime scope

一世执手 提交于 2019-11-28 22:41:09
问题 I'm rewriting this entire question because I realize the cause, but still need a solution: I have a recurring job in Hangfire that runs every minute and check the database, possibly updates some stuff, then exits. I inject my dbcontext into the class containing the job method. I register this dbcontext to get injected using the following builder.RegisterType<ApplicationDbContext>().As<ApplicationDbContext>().InstancePerLifetimeScope(); However, it seems that Hangfire does not create a

Autofac RegisterInstance vs SingleInstance

二次信任 提交于 2019-11-28 22:17:53
问题 IProductRepositoryProxy ProductDataServiceProviderInstance = new ServiceProductDataProvider(); builder.RegisterInstance(ProductDataServiceProviderInstance).As<IProductRepositoryProxy>(); VS builder.RegisterType<ServiceProductDataProvider>().As<IProductRepositoryProxy>().InstancePerRequest(); I saw this code from an ex-employee here and wonder if the guy wanted to register a .SingleInstance() behavior. builder.RegisterType<ServiceProductDataProvider>().As<IProductRepositoryProxy>()

Injecting NLog with Autofac's RegisterGeneric

て烟熏妆下的殇ゞ 提交于 2019-11-28 22:03:13
Note: Updated with suggested improvements, closer but still not quite there! Similar to this question - Passing in the type of the declaring class for NLog using Autofac - I am trying to inject NLog instances into my repository classes. Interface: public interface ILogger<T> where T: class { ... } Implementation: public class NLogger<T> : ILogger<T> where T: class { private readonly Logger _logger; public NLogger() { _logger = LogManager.GetLogger(typeof(T).FullName); } public void Debug(string message) { _logger.Debug(message); } ... } Registered with Autofac as: builder.RegisterGeneric

Dependency injection not working with Owin self-hosted Web Api 2 and Autofac

与世无争的帅哥 提交于 2019-11-28 21:12:00
I'm finding my feet with Web Api 2, Owin and Autofac and need some guidance, please. Overview I have an Owin self-hosted Web Api that uses Autofac for IoC and dependency injection. The project is a console app acting like a service, meaning it can be stopped and started. I have an Authentication controller with two constructors: one parameter-less and the other injects a repository. Problem When I run the service and call the api, my parameter-less constructor is called and my repository never gets injected (_repository = null). Research I've done a fair bit of research and found some helpful

Replace factory with AutoFac

心不动则不痛 提交于 2019-11-28 21:03:18
问题 I'm accustomed to creating my own factories as shown (this is simplified for illustration): public class ElementFactory { public IElement Create(IHtml dom) { switch (dom.ElementType) { case "table": return new TableElement(dom); case "div": return new DivElement(dom); case "span": return new SpanElement(dom); } return new PassthroughElement(dom); } } I'm finally getting around to using an IoC container (AutoFac) in my current project, and I'm wondering is there some magic way of achieving the