autofac

How to mock Autofaq interface with Moq

此生再无相见时 提交于 2020-04-11 08:33:08
问题 Using C#/Autofac/Moq: I have this class: public class MyService { private readonly IlifetimeScope _scope; public MyService(ILifetimeScope scope) { _scope = scope; } public void DoWork() { var dbContext = _scope.Resolve<IDataContext>(); } } This works at runtime. However, I can't unit test it, since I cannot mock the Resolve() method. It is an extension method. What's the best way to be able to mock it? The obvious thing would be to inject the IDataContext in the constructor instead of the

在C#中使用依赖注入

≡放荡痞女 提交于 2020-04-11 07:54:11
依赖注入(Dependency Injection,缩写为DI)是一种实现(Inversion of Control,缩写为IoC)的方法。在编写C#代码时,使用这种方法能够解决一些场景的需求。本系列将通过若干个实际问题,向读者介绍如何在C#中使用依赖注入。 阅读说明 软件要求 本系列文章将基于以下基本的软件运行环境 项目 内容 操作系统 Microsoft Windows 10 专业版 10.0.17134 IDE Visual Studio 2017 15.8.3 DI框架选择 C#开发中可选的DI框架众多。本系列文章将使用 Autofac 作为DI框架。 本系列文章也会对 Autofac 的基本用法进行介绍。对于更加深入的内容,读者可以前往 Autofac 官网进行了解。 https://autofac.org/ 项目结构 该系列文章均采用目标框架为 Framework 4.6.1 的 控制台项目 作为演练项目。 注意实践 本系列文章采用代码为主的方式进行编写,因此理论介绍较少。希望读者能够在样例代码的区别和实践中体验使用依赖注入带来的区别。 教程链接 在C#中使用依赖注入-三层结构 在C#中使用依赖注入-工厂模式和工厂方法模式 在C#中使用依赖注入-生命周期控制 来源: newbe 文章作者: newbe36524 文章链接: https://www.newbe.pro

Resolving IEnumerable of generic interfaces from Autofac container

北战南征 提交于 2020-04-11 06:31:47
问题 I'm not sure if this is possible, I've seen some other posts asking similar question but none have a satisfactory answer. What I want to do is resolve a collection of interfaces with differing generic types from Autofac. So constructor of class would look something like this: public class SomeClass<T> where T : class { private readonly IEnumerable<ITestInterface<T>> _testInterfaces; public SomeClass(IEnumerable<ITestInterface<T>> testInterfaces) { _testInterfaces = testInterfaces; } } Ideally

.NET Core 2.0 Autofac Register Class InstancePerRequest doesn't work

情到浓时终转凉″ 提交于 2020-04-10 08:17:52
问题 Here is my interface public interface IMatchServices { string MatchList(); } and class here public class MatchServices : IMatchServices { public string MatchList() { return "test"; } } Controller public class SearchController : Controller { private readonly IMatchServices matchservices; public SearchController(IMatchServices matchservices) { this.matchservices = matchservices; } [Route("matchlist")] public string MatchList() { return matchservices.MatchList(); } } and ConfigureService public

autofac resolve all ICommandHandler<ICommand,IMessageResult>

。_饼干妹妹 提交于 2020-03-26 08:39:07
问题 Hi In a MessageDispatch Scenario in .Net i register all my command handlers like this: builder.RegisterAssemblyTypes(commandsAssemblies) .AsClosedTypesOf(typeof(ICommandHandler<,>)) .InstancePerRequest(); in my OnActivating method for my messageDispatcher i want to register the commandHandlers with the messageDispatcher like this. builder.RegisterType<MessageDispatcher>() .As<IMessageDispatcher>().InstancePerRequest() .OnActivating(e => { var handlers = e.Context.Resolve(IEnumerable

Unable to hook up interceptor on classes/interfaces in asp.net core 3.1 application

跟風遠走 提交于 2020-03-25 21:04:46
问题 Packages: Autofac.Extensions.DependencyInjection (6.0.0), Autofac.extras.DynamicProxy(4.5.0) I am trying to prototype an interceptor for all of our service classes/interface serving the Web API controllers in our application, by following the ASP.NET Core 4.0 and the Interceptor documentation. The Interceptor is just a simple Log action using Serlog: public MethodCallInterceptor() { } public void Intercept(IInvocation invocation) { var text = invocation.Method.Name; Log.Logger.Debug($

依赖注入容器Autofac与MVC集成

♀尐吖头ヾ 提交于 2020-03-23 13:17:09
Autofac是应用于.Net平台的依赖注入(DI,Dependency Injection)容器,具有贴近、契合C#语言的特点。随着应用系统的日益庞大与复杂,使用Autofac容器来管理组件之间的关系可以“扁平化”错综复杂的类依赖,具有很好的适应性和便捷度。   在该篇博文中,我们将应用Autofac,以依赖注入的方式建立传统ASP.NET页面与服务/中间层之间的联系,建立“呈现”与“控制”的纽带。   那么,如何将依赖注入(Dependency Injection)植入ASP.NET中呢?   ASP.NET页面生命周期的整个过程均被ASP.NET工作者进程把持,这也就基本上切断了我们想在这个过程中通过“构造函数注入(Constructor injection)”的方式植入服务/中间层的念头,并且要求我们必须在进入页面生命周期之前完成这个植入的过程。基于这种考虑,我们可以采用IhttpModule基础之上“属性注入(Property injection)”方式。以下为实现细节:   1、 引用相应dll 下载Autofac,注意你下载的版本是否与你项目的.net版本一致   向ASP.NET应用程序添加以下引用:   Autofac.dll   Autofac.Integration.Web.dll Autofac.Integration.Web.Mvc.dll   2、

Is there a Autofac integration library for WinForm

时光怂恿深爱的人放手 提交于 2020-03-20 06:00:36
问题 I am working on a Win form application with Autofac Here we resolve the dependencies as following: As seen in doc using (var scope = DIConfig.container.BeginLifetimeScope()) { var us = scope.Resolve<IUsersService>(); usersGrid.DataSource = us.GetUsers(); } However in Web MVC project we could resolve all dependencies Eg as in DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); So that there is no need to resolve the scope each time as in and use the simple code usersGrid

Is there a Autofac integration library for WinForm

落爺英雄遲暮 提交于 2020-03-20 06:00:06
问题 I am working on a Win form application with Autofac Here we resolve the dependencies as following: As seen in doc using (var scope = DIConfig.container.BeginLifetimeScope()) { var us = scope.Resolve<IUsersService>(); usersGrid.DataSource = us.GetUsers(); } However in Web MVC project we could resolve all dependencies Eg as in DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); So that there is no need to resolve the scope each time as in and use the simple code usersGrid

关于.NET中的控制反转及AutoFac的简单说明

狂风中的少年 提交于 2020-03-16 03:21:46
目录 1.控制反转 1.1 什么是依赖? 1.2 什么是控制反转? 1.3 什么是依赖注入? 1.4 简单总结 2.控制反转容器 2.1 IOC容器说明 2.2 使用AutoFac的简介示例 3 使用AutoFac的一些细节 3.1 准备工作 3.2 注册整个程序集中的所有实现类 3.3 注入接口实现类中的接口类型的属性 3.4 关于一个接口有多个不同的实现类 3.5 关于一个实现类实现了多个接口 3.6 关于实例作用域 4.在MVC中使用AutoFac 5.参考及示例源码下载 shanzm-2020年3月16日 02:17:35 1.控制反转 1.1 什么是依赖? 依赖 是面向对象中用来描述类与类之间一种关系的概念。两个相对独立的对象,当一个对象负责构造另一个对象的实例,或者依赖另一个对象的服务,这样的两个对象之间主要体现为 依赖关系 1.2 什么是控制反转? 说反转则要先说“正转”,传统中,在程序中使用new关键字配合构造函数去创建一个对象,这就是程序主动的创建其所依赖对象,这就是“ 正转 ”。 调用者不自己创建被调用者对象,而交由第三方(容器)进行创建被调用者对象,这个过程称为 控制反转 (inversion of control, IOC )。 为什么要控制反转?控制反转是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度,便于扩展和后期维护。 1.3