autofac

Autofac - How to create a generated factory with parameters

假如想象 提交于 2019-12-20 09:54:21
问题 I am trying to create with Autofac a 'generated' factory that will resolve dependencies in real-time based on an enum parameter. Given the following interfaces/classes : public delegate IConnection ConnectionFactory(ConnectionType connectionType); public enum ConnectionType { Telnet, Ssh } public interface IConnection { bool Open(); } public class SshConnection : ConnectionBase, IConnection { public bool Open() { return false; } } public class TelnetConnection : ConnectionBase, IConnection {

Autofac DbContext has been disposed

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 06:29:06
问题 I've read this post DbContext has been disposed and autofac but I'm still getting the same error: The operation cannot be completed because the DbContext has been disposed. public class EFRepository : IRepository { private EFDbContext context; public EFRepository(EFDbContext ctx) { context = ctx; } public TEntity FirstOrDefault<TEntity>(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includes) where TEntity : class, IContextEntity { IQueryable<TEntity>

Autofac Generic Service resolution at runtime

我的梦境 提交于 2019-12-20 03:06:37
问题 Recently, Castle added support for interface factories with implementations provided by the kernel. I am wondering if there is a way to do this in autofac also. I have read about the delegate factories, but I think I might be missing something, and am unable to get it to work. Here is what I am thinking: class Message { } interface IHandle<T> { void Handle(T message); } class Handle<Message> : IHandle<Message> { ... } class Bus { .ctor (? lookup) { _lookup = lookup; } void Send<T>(T message)

Autofac - dependency injection for MVC controller and Web Api controller

狂风中的少年 提交于 2019-12-20 02:35:45
问题 I have MVC controllers (in Controllers folder) and Web Api controllers (in Api folder) in the same project: Here is the folder structure: Controllers ProductController Api ProductController Here is my bootstrapper method: private static void SetAutofacContainer() { var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterControllers(Assembly.GetExecutingAssembly()); //builder.RegisterType<UnitOfWork>().As<IUnitOfWork>()

How to inject AutoMapper with Autofac?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 02:16:18
问题 What is the proper way to inject AutoMapper to other layers? I read this blog post , but this code cause exception below An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code when try mapping in service layer. List<StudentViewModel> list2 = _mapper.Map<List<StudentViewModel>>(list); My AutoFac configuration like below: public static class DependencyRegistration { public static void Config() { var builder = new ContainerBuilder

Autofac and ASP .Net MVC 4 Web API

走远了吗. 提交于 2019-12-19 16:47:33
问题 I am using Autofac for IoC in my ASP .Net MVC 4 project. Autofac is having some trouble initializing the repository and passing it to the API Controller . I am sure I am missing something in my configuration. Here is the error I get when I navigate to: https://localhost:44305/api/integration <Error> <Message>An error has occurred.</Message> <ExceptionMessage> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'EL.Web.Controllers.API

Using Autofac with ASP.NET and the MVP pattern

假如想象 提交于 2019-12-19 11:51:25
问题 I'm trying to integrate Autofac into an exsisting ASP.NET web application. The pages follow the MVP pattern. Each page implements a View and delegate functionality to a Presenter. The View is injected into the Presenter thru the constructor. I was able to register the Presenter and View and the page loads fine but when a postback happens the user controls on the view are null. It seems that Autofac creates a new instance of the Page to give to the presenter instead of giving it the instance

AutoFac - Registering a decorator for some of an open Generic

拥有回忆 提交于 2019-12-19 09:10:22
问题 I'm attempting to set up an Autofac module that seems to have a complicated requirement. here goes: I have a generic interface: public interface IMyInterface<TFoo, TBar> I have a bunch of classes that implement this interface e.g. class MyImpl1 : IMyInterface<string, bool> { } class MyImpl2 : IMyInterface<bool, string> { } class MyImpl3 : IMyInterface<bool, string> { } Finally, I have a decorator: class MyDecorator<TFoo, TBar> : IMyInterface<TFoo, TBar> I only want to "Decorate" the

AutoFac - Registering a decorator for some of an open Generic

北城余情 提交于 2019-12-19 09:10:15
问题 I'm attempting to set up an Autofac module that seems to have a complicated requirement. here goes: I have a generic interface: public interface IMyInterface<TFoo, TBar> I have a bunch of classes that implement this interface e.g. class MyImpl1 : IMyInterface<string, bool> { } class MyImpl2 : IMyInterface<bool, string> { } class MyImpl3 : IMyInterface<bool, string> { } Finally, I have a decorator: class MyDecorator<TFoo, TBar> : IMyInterface<TFoo, TBar> I only want to "Decorate" the

Is order of dependencies guaranteed when injecting IEnumerable<T>

五迷三道 提交于 2019-12-19 05:23:41
问题 I register in container services implementing IMyService. Do I have any guarantees about their order in container.Resolve<IEnumerable<IMyService>> ? 回答1: No, there's no ordering guaranteed here. We've considered extensions to enable it but for now it's something to handle manually. 回答2: Just as extra help for people like me landing on this page... Here is an example how one could do it. public static class AutofacExtensions { private const string OrderString = "WithOrderTag"; private static