autofac

Configure Autofac Container for background thread

邮差的信 提交于 2019-12-06 11:47:43
I have an asp.net MVC site which has many components registered using an InstancePerHttpRequest scope, however I also have a "background task" which will run every few hours which will not have an httpcontext. I would like to get an instance of my IRepository which has been registered like this builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)) .InstancePerHttpRequest(); How do I do this from a non http context using Autofac? I think the IRepository should use the InstancePerLifetimeScope There are several ways of how you can do that: The best one in my opinion. You can

How to do action injection for MVC 3 using Autofac?

血红的双手。 提交于 2019-12-06 11:36:20
问题 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

Using Navigation in MasterDetailPage with Autofac and Xamarin.Forms

时光总嘲笑我的痴心妄想 提交于 2019-12-06 09:30:49
I'm trying to use Autofac in Xamarin.Forms project. I created basic samples successfully, even used ViewFactory for some more complicated samples. However, I'm unable to use MasterDetailPage along with Navigation . I'm using factories and services written by Jonathan Yates . You can find his code here My Application Bootstrapper: protected override void ConfigureApplication(IContainer container) { var viewFactory = container.Resolve<IViewFactory>(); var mainPage = viewFactory.Resolve<TestViewModel1>(); var navigationPage = new NavigationPage(mainPage); var masterPage = new ContentPage();

Autofac: Resolve a specific dependency to a named instance

拜拜、爱过 提交于 2019-12-06 09:06:20
问题 Using Autofac, I would like to register a component and specify for a specific dependency to be resolved to a named instance. I found samples like the following using constructor injection which is almost what I want. builder.Register(c => new ObjectContainer(ConnectionStrings.CustomerDB)) .As<IObjectContainer>() .Named("CustomerObjectContainer"); builder.Register(c => new ObjectContainer(ConnectionStrings.FooDB)) .As<IObjectContainer>() .Named("FooObjectContainer"); builder.Register(c => new

PostSharp AssemblyLoadException Autofac

本小妞迷上赌 提交于 2019-12-06 08:59:29
I am setting up a new solution in which I want to use the latest Autofac (3.4) and PostSharp 3.1.42. After referencing the NuGet packages I get the following error and I can't figure out what is going on. I never selected Autofac 3.3.0 package. packages.config: <package id="Autofac" version="3.4.0" targetFramework="net451" /> <package id="Autofac.Extras.Multitenant" version="3.1.1" targetFramework="net451" /> <package id="Autofac.Mvc5" version="3.3.0" targetFramework="net451" /> <package id="PostSharp" version="3.1.42" targetFramework="net451" /> MSBuild log: Error 3 Unhandled exception (3.1

Autofac 小试

陌路散爱 提交于 2019-12-06 08:49:36
ContainerBuilder builder = new ContainerBuilder(); ////builder.RegisterType<GetTest>().As<IBufrTest>(); //builder.RegisterType<GetTest>().AsImplementedInterfaces(); //builder.RegisterType<gettest2>().As<IBufrTest>(); string dllFIle = System.IO.Directory.GetCurrentDirectory() + "\\DemoBufr.dll"; builder.RegisterAssemblyTypes(Assembly.LoadFile(dllFIle)).AsImplementedInterfaces(); IContainer resorlver = builder.Build(); var test = resorlver.Resolve<IEnumerable<IBufrTest>>(); StringBuilder sbtext = new StringBuilder(); foreach (var item in test) { sbtext.Append(item.getstring()).Append("\r\n"); }

ASP .Net 4 Web Api RC + Autofac manual resolving

ぃ、小莉子 提交于 2019-12-06 08:22:24
问题 I'm trying to use depedency resolver inside a Web Api method. This worked fine and works fine with classic ASP.NET MVC with the DepedencyResolver.GetService() But I can't get this to work inside WepApi methods. My registration register all instances as InstancePerApiRequest and if I add any of all the types I have registred in my bootstrapper on the constructor of my WebAPiConroller thay inject fine but not anymore when calling them inside. Like this in my say Get Method var userRepository =

Register a Presenter with a View

半城伤御伤魂 提交于 2019-12-06 08:12:05
If I have a presenter like this - public class LandingPresenter : ILandingPresenter { private ILandingView _view { get; set; } private IProductService _productService { get; set; } public LandingPresenter(ILandingView view, IProductService) { .... } } How do I register this Presenter with Autofac considering the dependent view will not be registered (but IProductService will) builder.RegisterType<LandingPresenter>().As<ILandingPresenter>(); ???? Why not register the views in the container as well, put Autofac to work! Then you can hook up presenters and views automagically by using constructor

Migration from ASP.NET Core's container to Autofac

寵の児 提交于 2019-12-06 07:59:06
I'm using ASP.NET Core and its builtin container. I want to migrate my registrations to Autofac. The Autifac docs don't have a "migration guide", so I want to be sure I'm doing things correctly. ASP.NET Core container -> Autofac ---------------------- ------- // the 3 big ones services.AddSingleton<IFoo, Foo>() -> builder.RegisterType<Foo>().As<IFoo>().SingleInstance() services.AddScoped<IFoo, Foo>() -> builder.RegisterType<Foo>().As<IFoo>().InstancePerLifetimeScope() services.AddTransient<IFoo, Foo>() -> builder.RegisterType<Foo>().As<IFoo>().InstancePerDependency() // default services

UOW + Repository + Autofac load two different DbContext

柔情痞子 提交于 2019-12-06 07:00:02
问题 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