autofac

(2)ASP.NET Core 依赖关系注入(服务)

五迷三道 提交于 2021-02-10 11:25:27
1.前言 面向对象设计(OOD)里有一个重要的思想就是依赖倒置原则(DIP),并由该原则牵引出依赖注入(DI)、控制反转(IOC)及其容器等老生常谈的概念,初学者很容易被这些概念搞晕(包括我在内),在学习Core依赖注入服务之前,下面让我们先了解下依赖倒置原则(DIP)、依赖注入(DI)、控制反转(IOC)等概念,然后再深入学习Core依赖注入服务。 2.依赖倒置原则(DIP) 高层模块不依赖于低层模块的实现,而低层模块依赖于高层模块定义的接口 。通俗来讲,就是高层模块定义接口,低层模块负责实现。 2.依赖注入(DI) 2.1依赖(D) 当一个类需要另一个类协作来完成工作的时候就产生了依赖。 示例1: public class MyDependency { public MyDependency() { } public Task WriteMessage( string message) { Console.WriteLine($ " MyDependency.WriteMessage called. Message: {message} " ); return Task.FromResult( 0 ); } } public class IndexModel : PageModel { MyDependency _dependency = new MyDependency();

Retrieving Autofac container to resolve services

风格不统一 提交于 2021-02-10 06:40:16
问题 In a C# WindowForms application I start an OWIN WebApp that creates a singleton instance of my other class Erp: public partial class Engine : Form { const string url = "http://*:8080"; //49396 private IDisposable webApp; public Engine() { InitializeComponent(); StartServer(); } private void StartServer() { webApp = WebApp.Start<Startup>(url); Debug.WriteLine("Server started at " + url); } private void btnDoSomething(object sender, System.EventArgs e) { // needs to call a method in erp } }

AutoMock - How to unit test with Keyed Registrations?

拥有回忆 提交于 2021-02-10 04:53:30
问题 I'm having trouble writing tests around a factory that uses Autofac keyed registrations. In an Autofac module, I register things like this: builder.RegisterType<TypeAMessageHandler>().As<IMessageHandler>() .Keyed<IMessageHandler>(MessageTypeEnum.A); builder.RegisterType<TypeBMessageHandler>().As<IMessageHandler>() .Keyed<IMessageHandler>(MessageTypeEnum.B); builder.RegisterType<MessageHandlerFactory().As<IMessageHandlerFactory>(); Then the constructor for the factory gets a nice Index

AutoMock - How to unit test with Keyed Registrations?

筅森魡賤 提交于 2021-02-10 04:52:09
问题 I'm having trouble writing tests around a factory that uses Autofac keyed registrations. In an Autofac module, I register things like this: builder.RegisterType<TypeAMessageHandler>().As<IMessageHandler>() .Keyed<IMessageHandler>(MessageTypeEnum.A); builder.RegisterType<TypeBMessageHandler>().As<IMessageHandler>() .Keyed<IMessageHandler>(MessageTypeEnum.B); builder.RegisterType<MessageHandlerFactory().As<IMessageHandlerFactory>(); Then the constructor for the factory gets a nice Index

The AutofacServiceHost.Container static property must be set before services can be instantiated

北城以北 提交于 2021-02-07 07:56:28
问题 Within my XXXX.WS WCF services project I'm trying to get DI/IOC using autofac going...been at it all day but I think I'm close (different errors are progress here)...this error I can't understand how to shake..."AutofacServieHost.Container static property must be set..."..but I think I am setting it!?! What am I doing wrong? protected void Application_Start(object sender, EventArgs e) { var builder = new ContainerBuilder(); builder.Register(c => new DatabaseFactory()).As<IDatabaseFactory>()

The AutofacServiceHost.Container static property must be set before services can be instantiated

白昼怎懂夜的黑 提交于 2021-02-07 07:53:11
问题 Within my XXXX.WS WCF services project I'm trying to get DI/IOC using autofac going...been at it all day but I think I'm close (different errors are progress here)...this error I can't understand how to shake..."AutofacServieHost.Container static property must be set..."..but I think I am setting it!?! What am I doing wrong? protected void Application_Start(object sender, EventArgs e) { var builder = new ContainerBuilder(); builder.Register(c => new DatabaseFactory()).As<IDatabaseFactory>()

ASP.NET中IOC容器Autofac(依赖注入DI 控制反转IOC)

☆樱花仙子☆ 提交于 2021-01-30 09:56:37
IOC的一个重点是在程序运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI来实现的。Autofac则是比较流行的一款IOC容器。 IoC和DI有什么关系呢?其实它们是同一个概念的不同角度描述。 一、IOC IOC—Inversion of Control,即“ 控制反转 ”,不是什么技术,而是一种设计思想,一种面向对象编程法则,目的是程序解耦。 谁控制谁,控制什么? 传统程序设计,我们直接通过new object()创建对象,是程序主动去创建依赖对象;而IoC是有专门一个容器来创建这些对象,即由Ioc容器来控制对象的创建。 为何叫控制反转? 有反转就有正转,传统程序是我们自己主动创建并控制依赖对象,叫正转。 而 反转 则是由容器来帮忙创建及注入依赖对象,由容器帮我们查找及注入依赖对象,对象只是被动的接受依赖对象。 二、DI DI—Dependency Injection,即“ 依赖注入 ” 比如对象A需要操作数据库,以前我们总是要在A中自己编写代码来获得一个Connection对象,有了Autofac我们就只需要告诉Autofac,A中需要一个Connection,至于这个Connection怎么构造,何时构造,A不需要知道。在系统运行时,Autofac会在适当的时候制造一个Connection,然后像打针一样,注射到A当中,这样就完成了对各个对象之间关系的控制

callback is not called using moq + autofaq

泪湿孤枕 提交于 2021-01-29 05:09:32
问题 I have a unit test done using moq to mock the objects, and the test is working fine, and now I want to use autofac +moq, but I'm having a few problems. this is the test: using (var mock = AutoMock.GetLoose()) { var issues = new List<Issue>(); issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 1 }); issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 2 }); var numKeys = 0; mock.MockRepository.Create<IStorageService>() .Setup(myMock => myMock

AspNet Core Autofac disposing my DbContext even if its registered as SingleInstance

痞子三分冷 提交于 2021-01-28 21:18:55
问题 In our application, we have an api which will store some data to a database. We are using Entity Framework Core 3.1.1 . After this entity has been stored, a message is posted to Azure Servicebus and a consumer will read this message a store the message to another table in the same DbContext . As far as I understand, a LifetimeScope is defined per request to the api. The LifetimeScope will start when the api request hits the ApiController and finishes when the endpoint is done processing the

Prism DI container - dispose unnecessary objects

半城伤御伤魂 提交于 2021-01-28 06:09:02
问题 I am facing problem with disposing unnecessary objects from DI container. I use Prism for Xamarin.Forms with Unity container. Application gets configuration from some database, creates some services using this configuration and registers this services in container using ContainerControlledLifetimeManager. This services are used while resolving views and viewmodels. When configuration changes application retrieves again changed configuration and now problem comes: how can I remove previous