autofac

How to register Autofac component per http request in web.config?

冷暖自知 提交于 2019-12-10 11:24:59
问题 In MVC3 application I have my custom implementation of IPrincipal. I want to register it in web.config and then inject it to constructors by Autofac. Autofac XML-configuration has attribute "instance-scope" but it does not support "per-http-request". Any ideas how can I register component per http scope in web.config? I already tried different combinations of per-dependency, single-instance or per-lifetime-scope with ownership external but results are very buggy. 回答1: As Nicholas Blumhardt

Property injection into custom WebViewPage using Autofac

一笑奈何 提交于 2019-12-10 10:59:48
问题 I'm creating an internal application framework that other dev teams in our organisation will use to build MVC applications from. As part of that, I'm creating the menu structure for all views, that is read from configuration and modified based on the current user's permissions. To create the menu as part of the framework, I've created a custom WebViewPage implementation with a custom HTML Helper class that needs to take a dependency on an ApplicationDataReader to construct the menu. I've read

Autofac WebApi 2 OWIN Not Working

戏子无情 提交于 2019-12-10 10:37:25
问题 Having an issue with Autofac and WebApi2 using OWIN. Basically the Constructor isn't getting Injected. public class Startup { public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "Default Route", routeTemplate: "{controller}.{ext}" ); config.Routes.MapHttpRoute( name: "Default Route with Id", routeTemplate: "{controller}/{id}.{ext}", defaults: new { id = RouteParameter.Optional } ); var builder =

Configure Autofac Container for background thread

ε祈祈猫儿з 提交于 2019-12-10 10:35:20
问题 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

Autofac Resolve constructor instance from Container?

倖福魔咒の 提交于 2019-12-10 10:15:54
问题 How can I register a type which takes another type as part of it's constructed without passing an actual instance. Say I have two ISurface types registered. I want to register a Car but i don't want to pass in a brand new instance. I want to use one of the surfaces already defined. Per the documentation they state : Autofac sees that IDateWriter maps to TodayWriter so starts creating a TodayWriter. Autofac sees that the TodayWriter needs an IOutput in its constructor . Autofac sees that

Pass Autofac container to WPF UserControl

对着背影说爱祢 提交于 2019-12-10 10:01:02
问题 I am using autofac to resolve Views and ViewModels in a WPF application. IComponentContext is being passed into the View automatically. An example: public BusinessAuto(int proposedCoverageId, IComponentContext componentContext) { DataContext = componentContext.Resolve<BusinessAutoViewModel>(new TypedParameter(typeof(Int32), proposedCoverageId)); InitializeComponent(); } In the XAML for this view there are UserControls being created that have their own ViewModels. An example: <userControl

How to inject two instances of same object using Autofac?

最后都变了- 提交于 2019-12-10 09:19:24
问题 I'm using Autofac constructor injection. I need to figure out how to inject a single object instance into more than one constructor argument, without needing to explicitly resolve each argument during the container setup phase. I have a complex scenario which would be simplified by this behavior; the following example is just a simplified scenario so I can demonstrate the behavior I'm looking for. Example: Say I have these two interfaces, IOpenable and ICloseable: public interface IOpenable {

Autofac scope lifetime issue

六月ゝ 毕业季﹏ 提交于 2019-12-10 09:05:43
问题 I have ASP.NET MVC application where I registered a component with an InstancePerHttpRequest scope. builder.RegisterType<Adapter>().As<IAdapter>().InstancePerHttpRequest(); then I have an async piece of code where I'm resolving the Adapter component. The following code is simplified Task<HttpResponseMessage> t = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t => // IHandleCommand<T> takes an IAdapter as contructor argument var h = DependencyResolver.Current.GetServices

Authorization with ASP.NET Identity & Autofac OWIN integration

爷,独闯天下 提交于 2019-12-10 05:02:24
问题 (an update has been added at the bottom of this question) I have a web application that uses both MVC5 and WebAPI2 with Autofac for DI. The app uses ASP.NET Identity and oAuth bearer tokens, though the latter may be beside the point. This has all worked perfectly well, but at this point I need the same instances of my injected services to be shared throughout the OWIN pipeline as well as the rest of my application and therefore I'm trying to setup Autofac's OWIN integrations for MVC and Web

Autofac, MVC (with ActionFilters), Web.Forms - dependency resolution conflict

梦想与她 提交于 2019-12-10 04:33:49
问题 I've got a legacy Web.Forms app that been partially rewritten to MVC. MVC part uses autofac as a dependency injection container. MVC part have custom filter defined: public class CustomActionFilter : ActionFilterAttribute { protected ILogger Logger { get; set; } public CustomActionFilter(ILogger logger) { Logger = logger; } public override void OnActionExecuting(ActionExecutingContext filterContext) { Logger.Log("OnActionExecuting"); } } It works fine when Web.Forms integration is disabled in