autofac

How to integrate Autofac with WepApi 2 and Owin?

风流意气都作罢 提交于 2019-12-03 10:54:57
问题 I am using this package to integrate Autofac with my WebApi Owin application: https://www.nuget.org/packages/Autofac.WebApi2.Owin And following this post: http://alexmg.com/owin-support-for-the-web-api-2-and-mvc-5-integrations-in-autofac/ My code in Startup.cs looks like this: var config = new HttpConfiguration(); IContainer container = EngineContext.InitializeEngine(); var dependencyResolver = new AutofacWebApiDependencyResolver(container); config.DependencyResolver = dependencyResolver; app

Autofac with multiple implementations of the same interface

痞子三分冷 提交于 2019-12-03 10:50:02
问题 I'm using Autofac and would like to have multiple implementations of an interface. How can I configure Autofac so to resolve dependencies based on the current type? More specifically, I have one interface and multiple implementations that should be chained together. Let me explain (fictitious classes): public interface IMessageHandler { void Handle(Message message); } public class LoggingMessageHandler : IMessageHandler { private IMessageHandler _messageHandler; public LoggingMessageHandler

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

喜欢而已 提交于 2019-12-03 10:39:18
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 Global.asax I have a call to IocConfig.Config() which I have placed inside App_Start : public static class

Override autofac registration with plugin

孤街醉人 提交于 2019-12-03 10:31:16
I have an IFoo service implemented by DefaultFoo , and I've registered it as such in my autofac container. Now I would like to allow for an alternative implementation of IFoo to be implemented in a plugin assembly, which can be dropped in a "plugins" folder. How do I configure autofac to prefer this alternative implementation if it is present? Memoizer If you register some interface implementations, Autofac will use the latest registration. Other registrations will be overridden. In your case, Autofac will use the plugin registration, if plugin exists and register own IFoo service

Resolving IOwinContext in MVC5 application using Autofac

拈花ヽ惹草 提交于 2019-12-03 10:17:51
问题 I have trouble using MembershipReboot with the new ASP MVC5 template and Autofac . I have used the default MVC5 template to set up the site and then tried to wire up the MembershipReboot framework as a replacement for the ASP Identity framework that ships with the template. This issue I am having is trying to resolve an IOwinContext from the Autofac container. Here is my wiring in the Startup class (cut down to basics). This is the wiring used in the samples for the MembershipReboot Owin

How to organize MVP with an IoC container?

帅比萌擦擦* 提交于 2019-12-03 09:59:17
问题 I'm trying to get the IoC concept down with a winforms app. Say I have a presenter whose constructor takes its view and a service as constructor arguments. So in the form code I have something that amounts to this: mnPresenter = new Presenter(this, new AppService()); where, say AppService is an implementation of IAppService. It's registered in my [autofac] IoC container. What's the recommended way of getting the "new" out of this presenter construction? Isn't the whole point of using an IoC

Autofac Multi-tenant IoC Container in an ASP.NET Web API Application

£可爱£侵袭症+ 提交于 2019-12-03 09:51:35
问题 Autofac 3.0 will have a MultitenantIntegration support and its preview release is out now. To try it out, I created an ASP.NET Web API application with the following configuration: public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { var config = GlobalConfiguration.Configuration; config.Routes.MapHttpRoute("Default", "api/{controller}"); RegisterDependencies(config); } public void RegisterDependencies(HttpConfiguration config) {

How to use Autofac to resolve instance per request dependencies for types in a child lifetime scope created by Nancy

风格不统一 提交于 2019-12-03 09:15:02
We have several applications hosted in Windows services that self host a Nancy endpoint in order to expose instrumentation about the operation of the applications. We use Autofac as our IOC. Several repositories are registered into the root container in a core DLL shared by all applications; this container is then passed to Nancy as its container using a bootstrapper derived from the Nancy.Autofac.Bootstrapper . What we found was that when a web request is received by Nancy it resolves a request for a repository from the root container and this led to memory being consumed by non-garbage

Autofac scope lifetime issue

匿名 (未验证) 提交于 2019-12-03 09:05:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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<IHandleCommand<T>>(); ); The code above is throwing an

Autofac: How to load assemblies that are referenced but not directly used

让人想犯罪 __ 提交于 2019-12-03 08:51:01
We have created a WebApi solution using Autofac for DI. We broke out the bootstrapping of our autofac into a separate project. This way, our WebApi project only references our Bootstrap and Contracts projects. Our bootstrap project then references all other assemblies and wires everything together. I like this design for separation of concerns. We can manually load our assemblies as follows - where our "AutofacModule" classes contain the necessary info to register each module (assembly). ContainerBuilder builder = new Autofac.ContainerBuilder(); builder.RegisterModule(new Business