autofac

Property injection into custom WebViewPage using Autofac

微笑、不失礼 提交于 2019-12-06 06:51:00
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 various posts that explain that MVC needs the WebViewPage to have a paramterless constructor, so I

Injecting Generic type parameters with AutoFac

纵饮孤独 提交于 2019-12-06 06:36:52
问题 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

Autofac and WebAPI - Default constructor error

天涯浪子 提交于 2019-12-06 06:22:28
问题 I have a webforms project, but am using WebAPI for web services. I am trying to implement Autofac. I am getting: 'MyController' does not have a default constructor According to the Autofac documentation I have the configuration correct but obviously there is a problem. I am using Visual Studio 2010/.Net 4. Here is my Application_Start private void Application_Start(object sender, EventArgs e) { //This enables api controllers in a separate class library GlobalConfiguration.Configuration

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

孤者浪人 提交于 2019-12-06 06:18:01
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. As Nicholas Blumhardt from Autofac team told me per-lifetime-scope should be used. When running under ASP.NET it is equivalent to

Autofac WebApi 2 OWIN Not Working

╄→гoц情女王★ 提交于 2019-12-06 06:10:10
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 = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder

Autofac, ASP.NET MVC 3 httpRequest scope and AutoMapper: No scope with a Tag matching 'httpRequest' is visible

让人想犯罪 __ 提交于 2019-12-06 05:37:08
When I use a web type registered with autofac from an automapper mapping, I get this error: No scope with a Tag matching 'httpRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself. When another type is resolved in the mapping it works. When a web type is resolved from

How to mix decorators in autofac?

我怕爱的太早我们不能终老 提交于 2019-12-06 04:45:04
问题 I'd like to be able to mix and match decorators with Autofac. For example, let's say I have an IRepository interface, implemented by the Repository class. I could have the following decorators: RepositoryLocalCache, RepositoryDistributedCache, RepositorySecurity, RepositoryLogging..., you get the ideea. Based on config settings, I'd like to decorate the basic implementation with the needed decorators. That can be none, one or multiple decorators. I am familiar with the syntax of registering

Dependency Injection in ASP.net Session_Start method

这一生的挚爱 提交于 2019-12-06 04:22:40
I am learning dependency injection and using autofac for the first time. I built the container as mentioned in several autofac examples (see below) and called from my application_start public class ContainerConfig { public static void RegisterContainer() { //Create a new ContainerBuilder var builder = new ContainerBuilder(); // Register all the controllers using the assembly object builder.RegisterControllers(Assembly.GetExecutingAssembly()); //Registering default convention -- IExample and Example builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) .Where(t => t.Name.Single(i => i

Property Injection with internal setter

可紊 提交于 2019-12-06 03:34:31
I have an existing application that I am modifying to use Autofac Property Injection. It seems regardless of which method I use to register my types with properties, the properties are always null unless they have public setters. With other IoC containers (e.g. Structuremap) it's possible to scope the setter internal and make it available using the InternalsVisibleTo attribute on the assembly. This would seem nice to restrict clients from modifying the assignment. Is this possible with Autofac? Or is there another approach when working with property injection to keep the assignments secure? I

Autofac sub-dependencies chain registration

你离开我真会死。 提交于 2019-12-06 02:53:46
问题 Question How do I construct an AutoFac ContainerBuilder such that my sub-dependencies are correctly resolved (assuming more than one concrete implementation of an interface)? Default registration/resolve will not work because I have more than one concrete implementation of a sub-dependency, and the sub-dependency resolution is dependent on the resolution of the primary object. In all cases I wish to use constructor injection. Scenario For example, lets say I need to print a receipt, so I