autofac

Autofac Resolve Open Generic Interface with Open Generic Class

社会主义新天地 提交于 2019-12-05 22:15:13
So I have an interface and class: public interface IMyInterface<T> where T : ISomeEntity {} public class MyClass<T> : IMyInterface<T> where T : ISomeEntity {} I will have some class that calls for it: public class SomeClass : ISomeClass { public SomeClass (IMyInterface<AuditEntity> myInterface) {} } I've done all sorts of things to get it to register the open generic interface and class with no luck. I just want to say something like: container.RegisterType(typeof(MyClass<>)).As(typeof(IMyInterface<>)); It would be annoying if I have to go through and explicitly do something like: container

XUnit Test Constructor dependence injection with Autofac

浪子不回头ぞ 提交于 2019-12-05 20:24:29
I am implementing Xunit with Autofac, I could make it work by below code: using (var scoped = DbFixture.Container.Resolve<UserReponsitory>()) { var result = (scoped.GetAll()).ToList().Count(); Assert.Equal(2, result); } But I want to inject UserReponsitory to test method instead of using DbFixture.Container.Resolve . Is it possible to make below code work? UnitTest1.cs namespace XUnitTestPro { public class UnitTest1:IClassFixture<DbFixture> { private IUserReponsitory _userReponsitory; public UnitTest1(IUserReponsitory userReponsitory) { _userReponsitory = userReponsitory; } [Fact] public void

How to use the Autofac container as an Abstract Factory?

隐身守侯 提交于 2019-12-05 19:15:58
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 in the container. public class LetterFactory : ILetterFactory { private readonly IDocumentServicesCore

Autofac Binding at Runtime

微笑、不失礼 提交于 2019-12-05 18:43:30
I currently use Autofac to do simple constructor injection without any issues. However what I would like to know is how to resolve dependencies at runtime. The example below shows multiple ways in which we can export a document. With simple constructor injection the concrete implementation of IExport is resolved at runtime. However what need to do is to resolve IExport on a user selection from a dropdown list which will happen after the construction of my container. Is there any examples of how I can achieve this? Public interface IExport { void Run(string content); } public class PDFformat :

Injecting a specific instance of an interface using Autofac

浪尽此生 提交于 2019-12-05 18:34:44
I have a controller and it receives a specific instance of an interface. The interface looks something like this: public interface IMyInterface { ... implementation goes here } And then I have some classes that implement this interface like this: public class MyClassA : IMyInterface { ... implementation goes here } public class MyClassB : IMyInterface { ... implementation goes here } In my ControllerA I have the following constructor: private ICustomerService customerService; private IMyInterface myInterface; puvlic ControllerA(ICustomerService customerService, IMyInterface myInterface) { this

How to register two WCF service contracts with autofac

为君一笑 提交于 2019-12-05 17:16:02
问题 I have a WCF service that implements two service contracts... public class MyService : IService1, IService2 and I am self-hosting the service... host = new ServiceHost(typeof(MyService)); Everything was working fine when the service implemented only one service contract, but when I attempt to set up autofac to register both like this: host.AddDependencyInjectionBehavior<IService1>(_container); host.AddDependencyInjectionBehavior<IService2>(_container); ... it throws an exception on the second

Autofac Losing Registrations on Web.Config Edit

会有一股神秘感。 提交于 2019-12-05 16:56:54
I have a layered web application that I built with ASP.NET MVC 4, WebAPI and some other components. I'm using the latest release of Autofac 2.6.2.859 as my DI container along with the MVC and WebAPI integrations. I have autofac modules setup in my different layers and I'm using the new RegisterAssemblyModules to scan the AppDomain assemblies for the various modules. On startup every works great. When I edit the web.config file and the application warms up, my registrations are lost. I get a DependencyResolutionException - None of the constructors found with 'Public binding flags' on type 'My

Register string value for concrete name of parameter

六月ゝ 毕业季﹏ 提交于 2019-12-05 15:33:06
I am using Autofac and I have several classes which ask for parameter of type string and name lang. Is there a way to register a string value to all parameters named "lang", so it resolves automatically? I do not want to edit any of the constructors, since it is not my code (I know accepting e.g. CultureInfo would make registration easy..) Something resulting in short syntax like builder.Register(lang => "en-US").As().Named("lang") would be ideal. Thank you. A fairly simple way to solve this is with a custom Autofac module. First, implement the module and handle the IComponentRegistration

Cannot resolve AutoMapper.IMapper using AutoMapper 4.2 with Autofac

北城以北 提交于 2019-12-05 13:38:39
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.RegisterType<MappingEngine>().As<IMappingEngine>(); I have a constructor using IMapper mapper , however

Autofac - Inject properties into a asp.net mvc controller

笑着哭i 提交于 2019-12-05 12:28:38
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. This should work: builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired(); Some more info on the autofac website: http://code.google.com/p/autofac/wiki/PropertyInjection You may want to consider using an Autofac Aggregate Service : An aggregate