autofac

How to use the Autofac container as an Abstract Factory?

筅森魡賤 提交于 2019-12-07 12:44:30
问题 I would like to modify my current LetterFactory implementation and remove the call to Activator.CreateInstance with a call to the container to resolve the current Letter fully initialized with constructor injection. I have read the docs here and here, and even this SO Post while penning this post, but nothing seems to click. Notes: 1) IDocumentServicesCore is an Aggregate. 2) All Letters are decorated with the LetterTypeAttribute (hundreds of them) 3) This LetterFactory itself is registered

Including Autofac.Module in custom module breaks Orchard

淺唱寂寞╮ 提交于 2019-12-07 09:17:23
See here & here for why I'm trying to do this. I have a custom module that I'm building (and which is it's own .csproj in the Orchard .sln, as normal) I have a (relatively complex) dependency that I wish to inject into Orchard. Currently if I build and run the code it's perfectly happy until it hits my dependency at which point it null-refs (as I'd expect it to). I can access the admin site and any pages that don't reference the dependency. I add a reference to Autofac to my project. -> All fine. I add the following class: using System; using Autofac; namespace MyCustom.Module.Namespace {

Cannot resolve AutoMapper.IMapper using AutoMapper 4.2 with Autofac

感情迁移 提交于 2019-12-07 08:07:35
问题 I have tried various permutations of this but my current configuration (as it relates to AutoMapper) is like this: builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>(); builder.Register(c => new MapperConfiguration(cfg => { foreach (var profile in c.Resolve<IEnumerable<Profile>>()) { cfg.AddProfile(profile); } })).AsSelf().SingleInstance(); builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope(); builder

What is the proper way to create objects which require parameters using autofac?

此生再无相见时 提交于 2019-12-07 06:54:28
I think I get most things about dependency inversion and using an IoC container, but one thing still does not appear clear to me. How do I use autofac to automate the following factory: public class WidgetFactory { public static IWidget Create(int foo, double bar) { return new Widget(foo, bar); } } public class Widget { private readonly int foo; private readonly double bar; public Widget(int foo, double bar) { this.foo = foo; this.bar = bar; } } elsewhere... public class FoobarUser { public void Method() { var widget = WidgetFactory.Create(3, 4.863); // Do something with my widget // Possibly

Registering async factory in Autofac

情到浓时终转凉″ 提交于 2019-12-07 05:11:04
问题 I have a Wallet class that I get from a repository. I'm trying to properly register both in Autofac so classes using the wallet could have a proper instance injected. The problem is that the repository uses an async method (returning Task). Does Autofac support such cases? This doesn't work: cb.RegisterType<WalletRepository>() .As<IWalletRepository>() .SingleInstance(); cb.Register(async c => await c.Resolve<IWalletRepository>().CreateAsync(App.WalletPath)); cb.RegisterType<ViewModel>()

Autofac - Inject properties into a asp.net mvc controller

不打扰是莪最后的温柔 提交于 2019-12-07 04:56:53
问题 I have a base controller from which inherit all my controllers. This base controller has some properties I'd like to inject the using property injection. My controller registration looks like this builder.RegisterControllers(Assembly.GetExecutingAssembly() I don't know how to access the base class and inject the properties. 回答1: This should work: builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired(); Some more info on the autofac website: http://code.google.com/p

How should I scope dependency injection of Entity Framework DbContext in a web app? (InstancePerHttpRequest vs SingleInstance)

岁酱吖の 提交于 2019-12-07 04:35:22
问题 I have read that DbContext object should be created as InstancePerHttpRequest, not SingleInstance, because of its thread-unsafe nature and it might consume too much resource between requets which makes sence. But I am using Repository objects which uses DbContext instance. Should I make them InstancePerHttpRequest or make them SingleInstance and use DependencyResolver to get the current DbContext. What would the best object creation design be, for Autofac (or any other DI), DbContext,

How to make Autofac perform property injection in Orchard CMS

女生的网名这么多〃 提交于 2019-12-07 02:55:01
问题 Is it possible to do property injection with the OrchardCMS? I know that Orchard uses Autofac and that Autofac does do property injection, but I need to know how to do property injection for the IOrchardServices interface. Our team is looking at Orchard but our code base is all in ASP.NET 4.0 WebForms and so we will continue to serve aspx pages and slowly migrate those said pages into Orchard as time permits. With that, we'll need a way to get access to the OrchardServices object. I'm

Handling errors/exceptions in a mediator pipeline using CQRS?

丶灬走出姿态 提交于 2019-12-07 02:26:54
问题 I'm trying to follow this post by Jimmy Bogard to implement a mediator pipeline so I can use pre/post request handlers to do some work. From the comments on that article I come to this github gist. I don't quite understand how to hook all of this up yet, so here is my first go. FYI - I'm using Autofac for DI and Web Api 2. Following CQRS, here is a query. public class GetAccountRequest : IAsyncRequest<GetAccountResponse> { public int Id { get; set; } } //try using fluent validation public

Register all autofac modules from assembly by one line

对着背影说爱祢 提交于 2019-12-07 00:26:43
问题 I can register all Autofac modules (classes derived from Autofac.Module) one by one using line like this builder.RegisterModule(new LoggingInjectionModule()); But if I have 10 or more modules I want just to specify assembly, where Autofac can find all modules, that it need to register. Is there is any way to register all modules from specific assembly, namespace by single line or two lines? 回答1: You can use the different overloads (see API doc) of the RegisterAssemblyModules method: builder