autofac

Register Container Itself Using Autofac

♀尐吖头ヾ 提交于 2019-12-04 18:28:31
问题 I was wondering is there's any side effect to registering the container within itself IContainer container; ContainerBuilder builder = new ContainerBuilder(); container = builder.Build(); builder.RegisterInstance(container).As<IContainer>(); and the using it like this builder.RegisterType<IManagmentServiceImp>().As<ManagmentServiceImp>() .WithParameter(new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(IContainer) && pi.Name == "Container", (pi, ctx) => container )); or whether it

Autofac returns different instance in asp.net mvc web api

点点圈 提交于 2019-12-04 18:22:24
I'm using autofac in an asp.net mvc and webapi project. In the configuration I'm doing this : var builder = new ContainerBuilder(); builder.Register(x => NHibernateConfigurator.BuildSessionFactory()).SingleInstance(); builder.Register(x => x.Resolve<ISessionFactory>().OpenSession()).InstancePerHttpRequest(); builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource()); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); var container = builder.Build(); DependencyResolver.SetResolver(new

AutoFac and Log4Net - Registering and using

允我心安 提交于 2019-12-04 17:16:22
I have been instructed on using AutoFac (instead of Castle Windsor) along side log4net and am lost on how to correctly use these technologies together. Using the example on the autofac website I have the following LoggerModule public class LoggingModule : Module { private static void InjectLoggerProperties(object instance) { var instanceType = instance.GetType(); // Get all the injectable properties to set. // If you wanted to ensure the properties were only UNSET properties, // here's where you'd do it. var properties = instanceType .GetProperties(BindingFlags.Public | BindingFlags.Instance)

Autofac - Make sure that the controller has a parameterless public constructor

青春壹個敷衍的年華 提交于 2019-12-04 16:51:32
问题 I know it's been asked and answered before - the reason I'm asking is because (I think) I tried all suggested solutions to this problem but still can't resolve it. I have an ASP.NET Web API 2.0 project. I have Autofac , Autofac.Mvc5 and Autofac.WebApi2 dependencies installed. When I try to call an API controller I get the following error: An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor. In my

How to peek at message while dependencies are being built?

末鹿安然 提交于 2019-12-04 16:17:12
I building multitenancy into the unit of work for a set of services. I want to keep the tenancy question out of the way of day-to-day business domain work, and I do not want to touch every existing consumer in the system (I am retrofitting the multitenancy onto a system without any prior concept of a tenant). Most messages in the system will be contexted by a tenant. However, there will be some infrastructure messages which will not be, particularly for the purpose of automating tenant creation. I need a way of determining whether to use a tenant-contexted unit of work, or a infrastructure

How to do action injection for MVC 3 using Autofac?

 ̄綄美尐妖づ 提交于 2019-12-04 15:27:08
I'm creating an ASP.NET MVC 3 application trying to take advantage of controller action injection as described here . Controller constructor injection works without any issues, but I can't seem to get action injection to work. I've set up Autofac in Global.asax like this: var builder = new ContainerBuilder(); builder.Register<ITestInject>(c => new TestInject { TestString = "hi" }); builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>(); builder.RegisterControllers(typeof(MvcApplication).Assembly).InjectActionInvoker(); var container = builder.Build(); DependencyResolver

RegisterAssemblyTypes with UsingConstructor

僤鯓⒐⒋嵵緔 提交于 2019-12-04 13:11:39
I want to register assembly types which has multiple constructors Autowiring chooses the wrong constructor and want to specify it as I do in RegisterType builder.RegisterType(typeof(IController)) .UsingConstructor(typeof(IUnitOfWork)); But when I do this builder.RegisterAssemblyTypes(typeof(IController).Assembly) .UsingConstructor(typeof(IUnitOfWork)); I get "No matching constructor exists on type 'System.Object'." I think this is due to the fact that assembly type is a bit more complex than i thought, but the problem remains open What should I do? By doing builder.RegisterType(typeof

Injecting Generic type parameters with AutoFac

戏子无情 提交于 2019-12-04 12:34:16
I think I am really confused about what I can do with AutoFac, can someone please put me on track. I have a base type class PersonBase{ public string SaySomething(){ return "I am base"; } } I derive two concrete classes class FakePerson : PersonBase{ public override string SaySomething(){ return "I'm so Fake"; } } class RealPerson : PersonBase{ public override string SaySomething(){ return "I am For Real"; } } Create a generic class, PersonHandler, to deal with different types of people and would like the PersonHandler to instantiate the person at the appropriate time, so I do not want an

UOW + Repository + Autofac load two different DbContext

我的未来我决定 提交于 2019-12-04 11:46:35
I'm facing an issue today and I'm unable to solve it, I searched a lot and can't get into a solution, please help me if you can. I'm implementing a MVC application which uses EF + Repository Pattern + Unit Of Work with Autofac as the Dependency Injector. I was able to work with one DbContext class, but I'm facing a situation where I need to use another DbContext instance (which access another database with another user credentials) Let me explain better: I have EntityA which comes from database A (and have a DatabaseA_Context class). So I need a EntityB, which comes from database B (with its

Autofac with WebApi & Business Layer

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 11:43:05
I am very new to AutoFac and am trying to use it for my new project with WebApi and Business Layer with contracts and their respective implementations. I have written the IocConfiguration for webapi and invoke from global.asax. However, for my Business Logic how do I setup all my contracts and implementations with autofac? I did go through some tutorials online but however I could not find anything helpful, If someone has a sample app, links that really helps. Edit: AutoMapper profile. public class CustomProfile : Profile { protected override void Configure() { CreateMap<MyViewModel, MyModel>(