autofac

Autofac / MVC4 / WebApi (RC) Dependency Injection issue after upgrading from beta

天大地大妈咪最大 提交于 2019-12-03 16:16:39
问题 var resolver = new AutofacWebApiDependencyResolver(container); configuration.ServiceResolver.SetResolver(resolver); after updating to ASP.NET MVC4 (RC) I get the following error: 'System.Web.Http.HttpConfiguration' does not contain a definition for 'ServiceResolver' and no extension method 'ServiceResolver' accepting a first argument of type 'System.Web.Http.HttpConfiguration' could be found (are you missing a using directive or an assembly reference?) I realize after reading this (http://www

Use autofac in multiple project solution

我的未来我决定 提交于 2019-12-03 16:09:41
I have large wpf application. I simplify my problem with autofac. Let say I have ViewModelLocator where I create contrainer. ViewModelLocator is in Company.WPF project, this project refers Company.ViewModels project. var builder = new ContainerBuilder(); builder.RegisterType<MainWindowViewModel>().AsSelf().SingleInstance(); container = builder.Build(); Problem: MainWindowViewModel needs ICompanyService (I use CI) which is in Company.Services project, this project should not be reference from Company.WPF. ICompanyService is public and in same project (Company.Services) is also implementation

Global access to autofac dependency resolver in ASP.NET MVC3?

限于喜欢 提交于 2019-12-03 15:57:35
问题 I am using Autofac with ASP.NET MVC integration, all my controllers receive dependencies and Autofac resolves nested dependencies automatically. Great it works But how can I resolve a dependency outside the scope of controller instantiation? In some places deep in my code I need to ask the resolver for my Logger. On the one hand it seems wrong to be passing the Logger as a dependency down to every little object I create, and on the other it seems to wrong to depend on dependency resolver so

Autofac中的依赖囚禁现象

对着背影说爱祢 提交于 2019-12-03 15:39:45
依赖囚禁(俘获) 发生条件: 一个短时间的组件被另一个长时间的组件所持有就会发生 囚禁依赖 。 Autofac不会阻止你创建被囚禁依赖,因为这是开发人员的责任,有时候因为囚禁的发生将会得到一个解析异常。 1. 如何避免囚禁依赖 避免囚禁依赖的一般准则是: 使用该服务的组件的生命周期要小于等于正在使用的服务的生命周期。 例如: 有一个Web应用,我们通过传入请求中的一些信息来决定连接到哪一个数据库。 你可能需要以下组件: 接受当前请求和数据库连接工厂的repository 类似 HttpContext 的 current request ,可以用于决定业务逻辑. 一个数据库连接工厂( database connection factory ),接受一系列参数并且返回正确的数据库连接 在这个示例中,我们考虑以下应该为每个组件设定怎样的生命周期,而当前请求的上下文是一个很明显的上下文环境,我们可以将当前组件的生命周期设定为 每个请求为同一实例(Instance Per Request) 。 如果我们将 repository 设定为 “单例" 模式,即 SingleInstance 模式,那么这个实例将会缓存在应用的整个生命周期内,该请求上下文 (request context) 会被传入 repository 中,并在应用的整个生命周期中一直被 repository 所持有

How do I implement a delegate factory?

狂风中的少年 提交于 2019-12-03 15:02:09
The documentation for Autofac has an interesting page describing its ability to automatically generate delegate factories . It also strongly suggests that you can get similar results without Autofac by writing them by hand. I'm using Unity for IoC and would like to avoid passing the container around to objects that need to create other objects, so how would you write a delegate factory without Autofac? Well I've never used Unity so far, so my answer is quite vague. The principal is simple. You define some delegates which represent factories. Then you create a ‘factory’-class which has public

FluentValidation Autofac ValidatorFactory

跟風遠走 提交于 2019-12-03 13:55:06
I need to be able to provide the IComponentContext to my ValidatorFactory to resolve FluentValidation Validators. I am a little stuck. ValidatorFactory public class ValidatorFactory : ValidatorFactoryBase { private readonly IComponentContext context; public ValidatorFactory(IComponentContext context) { this.context = context; } public override IValidator CreateInstance(Type validatorType) { return context.Resolve(validatorType) as IValidator; } } How do I provide the context and register the ValidatorFactory FluentValidation.Mvc.FluentValidationModelValidatorProvider.Configure(x => x

ABPvnext源码分析 (二):Autofac整合及动态代理

霸气de小男生 提交于 2019-12-03 13:33:39
写在前面: 上一篇我们讲了Abp的核心模块,这一篇我们把DI 的serviceProvider替换成Autofac,另外添加动态代理拦截器功能。动态代理指从DI容器获取组件实例时组件实例不是原实例,而是代理实例。代理实例是对原实例进行了封装, 在实例方法前后添加逻辑处理,让获取的对象表现基于应有对象但又有自己的逻辑。举个例子,代理对象方法可以在原方法前后记录时间戳,来分析原方法的处理时长。Abp Core默认使用的是微软官方的DI实现, 那个功能较简单没有代理功能,为了添加动态代理功能,我们把DI实现替换为Autofac,并使用Autofac集成的代理实现方式Castle Core。 集成Autofac,我们需要添加Volo.Abp.Autofac包,这个包本身是Abp模块,其依赖AbpCastleCoreModule模块(Volo.Abp.CatleCore) namespace Volo.Abp.Autofac { [DependsOn(typeof(AbpCastleCoreModule))] public class AbpAutofacModule : AbpModule { } } 所以我们添加Volo.Abp.Autofac包并让我们的应用模块DependsOn(typeof(AbpAutofacModule))就自动的把AbpAutofacModule

Using autofac with moq

三世轮回 提交于 2019-12-03 12:17:14
I need to register my Autofac container with specific interface, for this case I want to resolved mock. How can I do it? I've tried: var AppContainer = ApplicationContainer.GetApplicationContainer(); var cb = new ContainerBuilder(); cb.RegisterType<Mock<IStudyLoader>>().As<IStudyLoader>().SingleInstance(); cb.Update(AppContainer); I don't want to change my code to resolve something else than IStudyLoader , but Mock<IStudyLoader> is not substitutable for IStudyLoader ; e.g Mock<IStudyLoader>.Object is substitutable for IStudyLoader , but I cant register Mock<IStudyLoader>.Object because it not

Injecting dependencies into custom Web API action filter attribute with Autofac

人走茶凉 提交于 2019-12-03 11:14:58
问题 I'm trying to resolve the dependencies of my custom AuthorizeAttribute which I use to decorate my API controllers in an MVC4 app. Problem is that I keep getting a NullReferenceException on the service dependency I use within my custom filter. Here is my Autofac configuration: public static class WebApiConfig { public static void Register(HttpConfiguration config) { var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterType

What does AsSelf do in autofac? [duplicate]

瘦欲@ 提交于 2019-12-03 10:57:45
This question already has an answer here : What is self-binding in an IoC container? (1 answer) What is AsSelf() in autofac? I am new to autofac, what exactly is AsSelf and what are the difference between the two below? builder.RegisterType<SomeType>().AsSelf().As<IService>(); builder.RegisterType<SomeType>().As<IService>(); Thank you! tdragon Typically you would want to inject interfaces, rather than implementations into your classes. But let's assume you have: interface IFooService { } class FooService { } Registering builder.RegisterType<FooService>() allows you to inject FooService , but