autofac

Autofac with MVC4: controller does not have a default constructor

江枫思渺然 提交于 2019-12-06 17:55:08
问题 I've been working with Autofac in MVC3 and love it. Now I am trying to implement it with MVC4. I installed the pre-release versions of Autofac MVC4 and Autofac WebApi through the Package Manager Console (Install-Package Autofac.Mvc4 -Pre and Install-Package Autofac.WebApi -Pre) I adjusted my IoC container as following: private static void SetAutofacContainer() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterApiControllers

Resolving AutoFac dependencies inside Module class

大兔子大兔子 提交于 2019-12-06 17:43:03
问题 I'm new to AutoFac and am currently using custom modules inside my app config to boot up some core F# systems. The code I'm using is var builder = new ContainerBuilder(); builder.RegisterType<DefaultLogger>().As<IDefaultLogger>(); builder.RegisterModule(new ConfigurationSettingsReader("autofac")); builder.Build(); And inside my app config I have the appropriate logic to start up the relevant systems. I would like to have access to the DefaultLogger inside my Modules. Metadata for the Module

Updating property of a singleton with Thread Safety

依然范特西╮ 提交于 2019-12-06 15:49:18
Our setup is: Asp.NET + MVC5 using AutoFac for DI. We have a class (which is a singleton) which is managing the access tokens for a variety of services. Every now and then, these tokens get too close to expiry (less then 10 minutes) and we request new tokens, refresh them. My current implementation looks like this: // member int used for interlocking int m_inter = 0; private string Token { get; set; } private DateTimeOffset TokenExpiry { get; set; } public SingletonClassConstructor() { // Make sure the Token has some value. RefreshToken(); } public string GetCredentials() { if ((TokenExpiry -

Autofac returns different instance in asp.net mvc web api

旧时模样 提交于 2019-12-06 14:39:33
问题 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

How to register an open generic decorator for an open generic registration in Autofac?

余生颓废 提交于 2019-12-06 14:16:10
I have an open generic registration for handlers in autofac that looks like this. builder.RegisterAssemblyTypes(assemblies) .AsClosedTypesOf(typeof (ICommandHandler<>)) .AsImplementedInterfaces(); This works fine and registers all my handlers for there closed types. I now want to register a generic decorator for all of the handlers e.g. a LoggingCommandHandlerDecorator<> In look from autofac documentation that you need to name your implementation so the decorator can be the default ICommandHandler. I'm not sure how this works when you are registering open generics. I've tried adding a named to

Use Autofac with a WCF service using WAS

喜夏-厌秋 提交于 2019-12-06 13:31:50
I like the WCF 4.0 capabality to host a service without an .svc file by setting a serviceActivations in the config file. It work great using the default service factory but now I'm trying to do the same using the AutofaServiceHostFactory so my dependencies will be properly injected. In all scenarios I tried, I still got this error when I try to access to service : The service 'WCFAutofacWiring.MyService' configured for WCF is not registered with the Autofac container . I host my service in an empty asp.net web site. Here's what I did : Web.config : <serviceHostingEnvironment>

How do I inject a repository into a custom MembershipProvider using AutoFac

假装没事ソ 提交于 2019-12-06 12:57:41
I created a custom membership provider public class MyMembership : MembershipProvider { private IRepository<User> Repo; } I figured out how to inject MyMembership using autofac: builder.Register(c => Membership.Provider); However, this doesn't work if I have a constructor that takes an IRepository (it only calls the parameterless constructor.) I tried doing changing from a private field to a public property and calling: builder.Register(c => Membership.Provider).AutoWireProperties(); The problem is, MembershipProvider doesn't have a Repo property, it's only my class. For the time being, I've

Autofac scope issues

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 12:37:36
问题 I am trying to get autofac to work, but having issues with my unitofwork / user manager classes. Initially I set my unit of work up as a per request instance like this: builder.RegisterType<UnitOfWork<DatabaseContext>>().As<IUnitOfWork>().InstancePerRequest(); But in my StartupConfig.cs I was trying to set up oAuth like this: private static OAuthAuthorizationServerOptions ConfigureOAuthTokenGeneration(IAppBuilder app, ILifetimeScope scope) { var t = scope.Resolve<IPasswordHasher>(); // Get

Autofac Lifetime Scope Decorator

倖福魔咒の 提交于 2019-12-06 12:23:23
问题 I am implementing a command handler pattern using Autofac and am using it's decorator facility handle cross cutting concerns such as logging, authentication etc. I also have dependencies that I only want scoped to the lifetime of the request / response pipeline. I have an example implementation below: public class Program { public static void Main() { var builder = new ContainerBuilder(); builder.RegisterAssemblyModules(typeof(HandlerModule).Assembly); builder.RegisterType<LifetimeScopeTester

An item with the same key has already been added when registering controller without a specific constructor signature

谁说我不能喝 提交于 2019-12-06 11:58:20
问题 This is really odd. Have an MVC 5 application using Autofac 3.3 that throws the An item with the same key has already been added error when I add a new controller that doesn't have specific constructor signatures. How do I go about debugging this? If the constructor looks like the following then when I navigate to the url for that controller it loads fine: public class ProductController : Controller { private readonly IServiceManager _serviceManager; public ProductController(IServiceManager