autofac

Autofac Module Scanning for Various Applications

大城市里の小女人 提交于 2019-12-23 17:47:35
问题 Let's say you're working on an ASP.NET MVC project and it is layers split up into different projects in a single solution. Each project has autofac modules created to wire up the dependencies. Now, I would like to scan the assemblies and register all of the modules into the container. I adapted an extension method similar to what was shared here http://goo.gl/VJEct public static void RegisterAssemblyModules(this ContainerBuilder builder, Assembly assembly) { var scanningBuilder = new

Register action with Autofac per application start

牧云@^-^@ 提交于 2019-12-23 16:25:23
问题 I'm using MVC with Autofac. I'd like to register action that runs once per application start. I'd like to achieve sth. like this: public class SomeModule : IOnceRunnable { private IService service; public SomeModule(IService service) { this.service = service; } public void Action() { // this action would be called once on application start } } containerBuilder.RegisterOnceRunnable<SomeModule>(); It is possible to perform such an action? I know I could use built container ( var container =

Using log4net with Autofac

大兔子大兔子 提交于 2019-12-23 15:26:39
问题 I am trying to use log4net with Autofac. I've pasted this code http://autofac.readthedocs.org/en/latest/examples/log4net.html , and from Program.cs/Main() I am doing var iocBuilder = new ContainerBuilder(); iocBuilder.RegisterModule(new LoggingModule()); var iocContainer = iocBuilder.Build(); now I would like to try this out immediately (in the next line), writing a simple line to the log file. How should I do it? I am thinking of something like this var ls = iocContainer.Resolve

Func<Owned<T>> vs Func<T> dependency

半城伤御伤魂 提交于 2019-12-23 14:19:09
问题 I know that Func<T> is different from Func<Owned<T>> and I know how to inject a dependency of each type. However, I often get confused as in when shall I prefer one over the other? Assume, I have an application following MVP pattern and I want to inject a view PrintView . Then, on what grounds shall I decide that I should inject the view as Func<PrintView> or Func<Owned<PrintView>> ? 回答1: Func<T> will resolve an item from the lifetime scope that will be disposed when the lifetime scope is

Autofac - Register all Windows Forms

一世执手 提交于 2019-12-23 13:12:09
问题 Is there a way to ask Autofac to automatically register all Windows Forms in my assembly? This feature would most likely be similar to the Autofac MVC support's ability to register all controllers? Right now, I do: foreach (var type in Assembly.GetExecutingAssembly().GetTypesSafely().Where(type => type.IsSubclassOf(typeof(Form)))) builder.RegisterType(type); 回答1: As far as I know there isn't a built in method to add all Win Forms. But you can do something like this. var assembly = Assembly

Autofac - resolve before build

与世无争的帅哥 提交于 2019-12-23 12:44:06
问题 With Unity dependency can be resolved before the container is build. Is that possible with Autofac as well? Below code demonstrates my scenario - I'd need to resolve the ICacheRepository in order to "new up" the singleton CacheHelper . In Unity that would be simply done with container.Resolve<ICacheRepository>() in the place of ???. What about in Autofac? var builder = new ContainerBuilder(); builder.RegisterType<CacheRepository>().As<ICacheRepository>(); var cacheHelper = new CacheHelper(???

How to use Property Injection in MVC Core and AutoFac

北城余情 提交于 2019-12-23 10:51:40
问题 I can use Constructor Parameter Injection easily In MVC Core. But Property Injection is not supported.I try use AutoFac but fail too. So how to use Property Injection in MVC Core. Here is the code with AutoFac services.AddMvc(); ContainerBuilder builder = new ContainerBuilder(); builder.RegisterType<Test2>().As<ITest>(); builder.RegisterType<HomeController>().PropertiesAutowired(); builder.Populate(services); var container = builder.Build(); //The following code works HomeController test2 =

How to register HttpContextBase with Autofac in Asp.Net (not MVC)

谁说我不能喝 提交于 2019-12-23 08:04:07
问题 This is a Asp.net application (not MVC) running .Net 3.5 I did this: protected void Application_Start(object sender, EventArgs e) { ... builder.Register(c => new HttpContextWrapper(HttpContext.Current)) .As<HttpContextBase>() .InstancePerHttpRequest(); } But it doesn't work. The error I am getting this: 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

How to register HttpContextBase with Autofac in Asp.Net (not MVC)

不羁岁月 提交于 2019-12-23 08:04:06
问题 This is a Asp.net application (not MVC) running .Net 3.5 I did this: protected void Application_Start(object sender, EventArgs e) { ... builder.Register(c => new HttpContextWrapper(HttpContext.Current)) .As<HttpContextBase>() .InstancePerHttpRequest(); } But it doesn't work. The error I am getting this: 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

Why are all my web.configs updated with an assembly redirect when installing autofac?

↘锁芯ラ 提交于 2019-12-23 07:36:12
问题 I just installed autofac into one of my projects, call it project A, in a solution I have using nuget. For some reason all my web.config files, I have multiple web applications and apis in the solution, got updated with an assembly redirect like this one: <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0