autofac

Autofac resolve dependency in CQRS CommandDispatcher

无人久伴 提交于 2019-11-29 12:33:07
问题 I'm trying to implement a simple CQRS-application example. This is a structure of my "Command" part: public interface ICommand { } //base interface for command handlers interface ICommandHandler<in TCommand> where TCommand: ICommand { void Execute(TCommand command); } // example of the command public class SimpleCommand: ICommand { //some properties } // example of the SimpleCommand command handler public class SimpleCommandHandler: ICommandHandler<SimpleCommand> { public void Execute

What happened to Lazy<T> support in Autofac?

强颜欢笑 提交于 2019-11-29 10:02:44
In beta builds of Autofac 2.1 there was support for automatic resolution of Lazy<T> as described in Nicholas Blumhardt's Lazing Around with Autofac blog post. The code still seems to be in the source on Google Code, but I can't find LazyDependencyModule in any of the .NET 4.0 binaries I've looked at. Has it moved somewhere else? How do I use Autofac's automatic Lazy<T> resolution with the latest Autofac builds? You don't need to register LazyDependencyModule yourself in the production Autofac 2 builds. It is a part of the default container, so just register T and Lazy<T> will be provided. Make

Can multiple Autofac lifetime scopes be specified on a registration?

我只是一个虾纸丫 提交于 2019-11-29 09:01:00
问题 I'm using the Autofac IoC container with the MVC4 add-on which provides the InstancePerHttpRequest lifetime scope. However within my project I have the web, web-api and background worker threads. In the following example I assume the InstancePerHttpRequest scope doesn't mean much when not originating from a web request. builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>() .InstancePerHttpRequest() I'm wondering if it is possible to do something like the following example and have

UserNamePasswordValidator: When DI and Framework collide

十年热恋 提交于 2019-11-29 08:09:59
I am building a custom UserNamePasswordValidator for a WCF service. I am wiring up the service with Autofac + WCF/multitenant, all fitting neatly together. However I'm not sure what strategy to use to wire/implement this authentication class. Ideally, I would start with public class MyValidator : UserNamePasswordValidator { public MyValidator(Func<Owned<IMyUserService>> userservicefactory) { ... } } However, this isn't strictly possible because of the way that a UserNamePasswordValidator is consumed by WCF (the only option appears to be parameterless constructor). So, questions: Am I correct

Resolve dependency with autofac based on constructor parameter attribute

元气小坏坏 提交于 2019-11-29 07:57:22
I'm using Autofac. I want to inject a different implementation of a dependency based on an attribute I apply to the constructor parameter. For example: class CustomerRepository { public CustomerRepository([CustomerDB] IObjectContainer db) { ... } } class FooRepository { public FooRepository([FooDB] IObjectContainer db) { ... } } builder.Register(c => /* return different instance based on attribute on the parameter */) .As<IObjectContainer>(); The attributes will be providing data, such as a connection string, which I can use to instance the correct object. How can I do this? It sounds like you

Autofac: any way to resolve the innermost scope?

不想你离开。 提交于 2019-11-29 07:56:47
I'm currently trying out Autofac in a new ASP.NET MVC project after having used Ninject, Castle Windsor and other IoC containers in the last years. So while I know about IoC containers in general, I'm fairly new to Autofac and I'm still looking for some best practices. Currently I'm trying to find out if there is a way to resolve the innermost nested scope. I have the following situation: a component that is registered as SingleInstance() has a method that creates a nested lifetime scope, providing a configuration action to configure some components as InstancePerLifetimeScope, and within this

Self-Registering Libraries with Autofac 4 and vNext

丶灬走出姿态 提交于 2019-11-29 07:52:30
i'd like to create a Plugin Enviroment for my ASP.Net 5.0 / MVC 6 Application. I'm using Autofac as IOC Container and i like to load the Plugins (Class Libraries) from the build in DNX LibraryManager. The goal of using the Library Manager is, that i don't have to care about NuGet Packages and Frameworks. The Problem i have is the LifeCycle, i have to build the IOC Container before the instance of the LibraryManager is available. Because the Autofac Container provides his own IServiceProvider Instance which i have to inject within the ConfigureService() Method call (AddAutofac). Does anyone

How to wire events with methods using Autofac?

六眼飞鱼酱① 提交于 2019-11-29 07:11:37
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.SomeOtherType, Whatever"> <listener event="Output" method="Input"> <ref object="SomeType" /> </listener> </object>

Resolve instance - Autofac

拥有回忆 提交于 2019-11-29 04:23:40
I'm trying to figure out how to resolve a instance somewhere in the code. At the application startup I registered a type static void Main() { var builder = new ContainerBuilder(); builder.RegisterType<Foo>().As<IFoo>(); } Now, how can I resolve an instance somewhere in the code ? In StructureMAP there is a static object ObjectFactory.GetInstance<IFoo>() Read up on Getting Started . It should get you started. First off, what you are looking for is the container . Build it from the ContainerBuilder like in this simple WinForms app: static void Main() { using (var container = builder.Build()) {

Have to register every class before the autofac container can resolve?

做~自己de王妃 提交于 2019-11-29 03:48:40
let's say this scenario: public class B {}; public class C { public C(B b){} } To resolve C from Autofac container, I have to register both B and C to container. But, today I used Unity , it seems I just need to register B to container, then C can be resolved. So Autofac can't do as Unity do? With out-of-the-box Autofac it is expected that every type you want to use is registered with the container, either directly using the Register... methods or in bulk using RegisterAssemblyTypes . But there are other options too, take a look at Nicholas article about resolving everything . So yes, Autofac