autofac

Autofac passing parameter to nested types

心不动则不痛 提交于 2020-01-11 04:59:04
问题 I am using Autofac as my IoC in my WCF service. I have a situation where I want to pass an object to a nested type (ie a type that is not resolved directly, but when resolving another type). As far as I understood, passing this object as a constructor parameter is the preferred way in Autofac. Here is an example of such a situation. The nested type: public class EventLogger<T> : IEventLogger<T> { public EventLogger(IRepository<T> repository, User currentUser) { ... } } The type I am actually

Autofac使用

自闭症网瘾萝莉.ら 提交于 2020-01-11 01:17:31
Autofac使用说明转载 一、组件   创建出来的对象需要从组件中来获取,组件的创建有如下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();  //将自己系统中原有的单例注册为容器托管的单例   这种方法会确保系统中的单例实例最终转化为由容器托管的单例实例。  

微软CMS项目 Orchard 所用到的开源项目

吃可爱长大的小学妹 提交于 2020-01-10 03:28:17
  研发了Orchard一年左右了,时常遇到瓶颈,总觉得力不从心,其实并不是基础不够,关键还是概念性的东西太多,一会儿这个概念名词,一会那个,关于Orchard的技术文档也的确很少,每次看起来总是焦头烂额。   其实Orchard是一大堆开源项目的集合,他是用了一套体系融合了先进几乎所有先进的开源体系或者工具。如果想好好去探索Orchard,如下的开源项目可以先去了解一下,这样就遇到问题也可以有的放矢。   <关于Orchard架构的介绍: http://www.cnblogs.com/xfuture/p/3560896.html >   Orchard所引用的这些类库副本及其许可协议放置在源代码包的“lib”目录下。你也可以在源代码包的根目录下的“CREDITS.txt”文件中快速浏览它们的信息。参考文档 : http://www.orchardproject.net/docs/Orchard-dependencies-and-libraries.ashx    Akismet Website: http://www.codeplex.com/wikipage?ProjectName=AkismetApi Copyright: Copyright (c) 2006, Joel Thoms License: New BSD Orchard中的默认的垃圾过滤服务. ANTLR

How can I log all resolve requests to Autofac container?

亡梦爱人 提交于 2020-01-09 06:20:34
问题 I am trying to debug some problems in a legacy code base. I think is being caused by an exception being thrown because something can't be resolved from the Autofac container. However I think the exception is getting buried somewhere and I am not seeing the root cause. I am sure something is being requested from a controller that either can't be found or something that can be found has a dependency that can't be satisfied. There aren't any guard clauses etc. so I think I am getting a null

Entity Framework中的SqlException-不允许新事务,因为会话中正在运行其他线程

烂漫一生 提交于 2020-01-07 17:58:46
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我目前收到此错误: System.Data.SqlClient.SqlException:不允许新事务,因为会话中正在运行其他线程。 在运行此代码时: public class ProductManager : IProductManager { #region Declare Models private RivWorks.Model.Negotiation.RIV_Entities _dbRiv = RivWorks.Model.Stores.RivEntities(AppSettings.RivWorkEntities_connString); private RivWorks.Model.NegotiationAutos.RivFeedsEntities _dbFeed = RivWorks.Model.Stores.FeedEntities(AppSettings.FeedAutosEntities_connString); #endregion public IProduct GetProductById(Guid productId) { // Do a quick sync of the feeds... SyncFeeds(); ... // get a product... ...

Using Autofac to provide types exported by static factory

非 Y 不嫁゛ 提交于 2020-01-07 05:08:10
问题 I have a dependency which provides a number of services using its static ServiceManager. It also provides a list of available types. Type[] ServiceManager.GetServiceTypes(); object GetService(Type t); In an Autofac Module, I'd like to enumerate these types and register 'dynamic instantiation' of them. It's important that I call ServiceManager.GetService each time an instance is requested. 回答1: I ended up using my own RegistrationBuilder, looks pretty funky but it works. Have I missed an

Autofac IAutofacActionFilter execution order for Web API 2

▼魔方 西西 提交于 2020-01-07 01:48:10
问题 Are there any ways to set the order of execution on web api action filters registered with Autofac? Currently, if I register the following: builder.Register(x => new MyFirstAttribute(x.Resolve<IMyService>())).AsWebApiActionFilterFor<ApiController>().InstancePerRequest(); builder.Register(x => new MySecondAttribute(x.Resolve<IMyService>())).AsWebApiActionFilterFor<ApiController>().InstancePerRequest(); it is 'unknown' which will be executing first and second. Even if I'd create a new

Read modules from web.config in autofac to change container configuration according to solution configurations

断了今生、忘了曾经 提交于 2020-01-06 06:51:35
问题 I have created some Autofac modules... Now I want to register some of them in my container using web.config... In my web.config I have written: <autofac defaultAssembly="Autofac.Example"> <modules> <module type="DebugModuleTest1"></module> <module type="DebugModuleTest2"></module> </modules> </autofac> Now I have to build my container. But the autofac documentation is not clear to me. I do not understand what I have to do to read my modules and build the container. public class

autofac scope around a resolved instance

拈花ヽ惹草 提交于 2020-01-06 05:28:11
问题 I'm somewhat new to autofac, but have already used it successfully with different "simple" configurations. Now I'm trying something more complex and struggle with working this out. Essentially, I have an entry point where all autofac configuration is done and the first objects are resolved, which themselves get dependencies by constructor injection, their dependencies can have other dependencies and so on. Some code: public class Root { public Root(A.Factory factory) { for (int i = 0; i < 3;i

Autofac vs Ninject contextual binding?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-06 04:19:47
问题 1) Autofac and Ninject, which one is better in contextual binding? 2) With Ninject, I can bind to types in a specific namespace: Bind<A>().To<B>().When(context => context.ParentContext.Binding.Service.Namespace == "MyProject.Common"); Can I do this with Autofac? Thanks! (sorry for my English) 回答1: Of the two, only Ninject provides this functionality. It is a useful feature but Autofac excludes it to prevent fragility in other Autofac-specific features that rely on the dependency being unaware