autofac

Autofac框架详解

*爱你&永不变心* 提交于 2020-01-15 00:29:40
一、组件   创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式:    1、类型创建RegisterType   AutoFac能够通过反射检查一个类型,选择一个合适的构造函数,创造这个对象的实例。主要通过RegisterType<T>() 和 RegisterType(Type) 两个方法以这种方式建立。   ContainerBuilder使用 As() 方法将Component封装成了服务使用。 builder.RegisterType<AutoFacManager>(); builder.RegisterType<Worker>().As<IPerson>();    2、实例创建   builder.RegisterInstance<AutoFacManager>(new AutoFacManager(new Worker()));    单例   提供示例的方式,还有一个功能,就是不影响系统中原有的单例:   builder.RegisterInstance(MySingleton.GetInstance()).ExternallyOwned();  //将自己系统中原有的单例注册为容器托管的单例   这种方法会确保系统中的单例实例最终转化为由容器托管的单例实例。    3、Lambda表达式创建  

Autofac官方文档翻译--二、解析服务--1解析参数传递

本秂侑毒 提交于 2020-01-15 00:23:09
Autofac 传递解析参数 注册组件公开相应的服务之后,你可以从container构造器和子lifetime scopes 中解析服务。使用 Resolve() 方法来实现: var builder = new ContainerBuilder(); builder.RegisterType<MyComponent>().As<IService>(); var container = builder.Build(); using(var scope = container.BeginLifetimeScope()) { var service = scope.Resolve<IService>(); }   你会发现在这个例子中,从一个lifetime scope 中解析服务,而不是直接在容器中解析,你也应该这样做。   然而也有可能从根容器中解析组件,在你的应用中这样做在一些情况下可能导致内存泄露。推荐你总是从一个lifetime scope中解析组件,以尽可能确保服务实例妥善处理和垃圾回收。在本节中你可以阅读到很多关于控制范围和生命周期的知识。   当解析服务时,Autofac 将自动链接服务的整个依赖层次结构,并且解析全面构建服务所需的任何依赖关系。如果你有循环依赖关系处理不当或缺失必须的依赖,你会得到一个 DependencyResolutionException 。  

[翻译]Autofac 解析服务

北慕城南 提交于 2020-01-15 00:17:07
注册组件以后,通过容器或 ILifetimeScope 的 Resolve 方法解析服务: var builder = new ContainerBuilder(); builder.RegisterType<MyComponent>().As<IService>(); var container = builder.Build(); using(var scope = container.BeginLifetimeScope()) { var service = scope.Resolve<IService>(); } 请注意,本例使用 ILifetimeScope 解析服务,而不是直接从容器解析。 尽管可以从根容器直接解析组件,但是,某些情况下导致内存泄露。 建议始终从 ILifetimeScope对象解析组件,以确保服务的实例会被处置(Dispose)。请参考 控制范围和生命周期 一节。 解析服务时,Autofac在服务的依赖层次中自动延伸,解析出构造服务对象需要的全部依赖项。如果遇到无法正确处理的循环依赖,或必需的依赖项找不到,将抛出 DependencyResolutionException。 对于不确定否已注册的服务,可以用 ResolveOptional 方法或 TryResolve 方法尝试条件解析。 // 若 IService 已注册,则解析成功,否则,返回

DI (Autofac) in a plugin architecture: Is one separate DI container per plug-in OK?

倾然丶 夕夏残阳落幕 提交于 2020-01-14 07:51:33
问题 I am trying to introduce DI (with Autofac) into an existing Windows Forms application. This application has a basic plug-in architecture where each plugin displays its own form. On startup, the application scans registered assemblies for types that implement IPlugin , and then activates these using Activator.CreateInstance : public interface IPlugin { Form MainForm { get; } } I cannot change this given framework. This means, each plugin class is instantiated through non-DI means, and it seems

Webforms Autofac parameter to constructor using VB.NET

一笑奈何 提交于 2020-01-14 06:18:06
问题 So, i want to do what I feel should be such a simple task... pass in a parameter to a constructor using Autofac! However, I have managed to get a work around working, but just dont think this is correct, I feel i am chaning too much of the recommended code from the Autofac guides I am more than happy for answers in C# or VB.net it doesnt matter, the location of code is all the same So, here is my setup (im not fussed about neatness, just trying to get this to work for now) In my global.asax I

How to determine which constructor Autofac uses when resolving

孤者浪人 提交于 2020-01-14 03:15:22
问题 I'm using a custom JsonConverter and JsonSerializerSettings.TypeNameHandling = TypeNameHandling.Objects to create the required instances during deserialization. The instances are created by resolving the types from an Autofac IOC container. Everything works fine, except... I have several "core objects" that request a unique Id in the constructor from a service (which is correctly injected into the constructor). When deserializing this should not happen because it is fairly expensive and the

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

柔情痞子 提交于 2020-01-13 19:13:08
问题 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

Autofac Exception: Cannot resolve parameter of constructor 'Void .ctor

筅森魡賤 提交于 2020-01-13 08:28:07
问题 I have the following error: ExceptionMessage=None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'RestAPI.DevelopersController' can be invoked with the available services and parameters: Cannot resolve parameter 'Services.DevelopersService userService' of constructor 'Void .ctor(Services.DevelopersService)'. Global.asax.cs protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); AutoMapperConfig

Using Autofac to inject log4net into controller

有些话、适合烂在心里 提交于 2020-01-12 05:24:28
问题 Trying to use Autofac to inject a log4net class into my controller, but I get the following exception: None of the constructors found with 'Public binding flags' on type 'MvcApplication6.Controllers.HomeController' can be invoked with the available services and parameters: Cannot resolve parameter 'log4net.ILog logger' of constructor 'Void .ctor(log4net.ILog)'. I have created a module to inject the Log class using the correct type: public class LogInjectionModule : Module { protected override

Using Autofac to inject log4net into controller

倖福魔咒の 提交于 2020-01-12 05:24:09
问题 Trying to use Autofac to inject a log4net class into my controller, but I get the following exception: None of the constructors found with 'Public binding flags' on type 'MvcApplication6.Controllers.HomeController' can be invoked with the available services and parameters: Cannot resolve parameter 'log4net.ILog logger' of constructor 'Void .ctor(log4net.ILog)'. I have created a module to inject the Log class using the correct type: public class LogInjectionModule : Module { protected override