autofac

Using Autofac with Domain Events

穿精又带淫゛_ 提交于 2019-12-03 02:54:00
问题 I'm trying to introduce domain events into a project. The concept is described in Udi Dahan's post - http://www.udidahan.com/2009/06/14/domain-events-salvation/ Here's the domain event code public interface IDomainEvent { } public interface IHandleDomainEvents<T> where T : IDomainEvent { void Handle(T args); } public interface IEventDispatcher { void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent; } public static class DomainEvents { public static IEventDispatcher

Autofac - Register multiple decorators

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Given the following: public interface ICommandHandler<in TCommand> { void Handle(TCommand command); } public class MoveCustomerCommand { } public class MoveCustomerCommandHandler : ICommandHandler<MoveCustomerCommand> { public void Handle(MoveCustomerCommand command) { Console.WriteLine("MoveCustomerCommandHandler"); } } public class TransactionCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand> { private readonly ICommandHandler<TCommand> _decorated; public TransactionCommandHandlerDecorator(ICommandHandler<TCommand> decorated) {

Autofac and IDisposable interface

六月ゝ 毕业季﹏ 提交于 2019-12-03 02:05:16
Assuming that I have the following interface and class: public interface IFooRepo : IDisposable { //... } public FooRepo : IFooRepo { //Methods here //Properly implement the IDisposbale.Dispose() here } I use Autofac as IoC container in my application and if I register this as below, can I be sure that it will disposed properly? private static IContainer RegisterServices(ContainerBuilder builder) { builder.RegisterType<FooService>().As<IFooService>(); return builder.Build(); } Or should I take further steps depending on the application type I am using. (In this case, I using ASP.NET MVC but I

Resolving IOwinContext in MVC5 application using Autofac

偶尔善良 提交于 2019-12-03 01:53:45
I have trouble using MembershipReboot with the new ASP MVC5 template and Autofac . I have used the default MVC5 template to set up the site and then tried to wire up the MembershipReboot framework as a replacement for the ASP Identity framework that ships with the template. This issue I am having is trying to resolve an IOwinContext from the Autofac container. Here is my wiring in the Startup class (cut down to basics). This is the wiring used in the samples for the MembershipReboot Owin application (except there he uses Nancy). public partial class Startup { public void Configuration

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

丶灬走出姿态 提交于 2019-12-03 01:53:23
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当中,这样就完成了对各个对象之间关系的控制

Explicit resolving of ILog in Autofac when using with Log Injection Module

匿名 (未验证) 提交于 2019-12-03 01:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I use the following code in order to register log4net for all the classes that need it. public class LogInjectionModule : Module { private readonly string _configPath; public LogInjectionModule(string configPath) { _configPath = configPath; } protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration) { XmlConfigurator.Configure(new FileInfo(_configPath)); registration.Preparing += OnComponentPreparing; } private static void OnComponentPreparing(object sender, PreparingEventArgs e)

How to integrate Autofac with WepApi 2 and Owin?

非 Y 不嫁゛ 提交于 2019-12-03 01:22:11
I am using this package to integrate Autofac with my WebApi Owin application: https://www.nuget.org/packages/Autofac.WebApi2.Owin And following this post: http://alexmg.com/owin-support-for-the-web-api-2-and-mvc-5-integrations-in-autofac/ My code in Startup.cs looks like this: var config = new HttpConfiguration(); IContainer container = EngineContext.InitializeEngine(); var dependencyResolver = new AutofacWebApiDependencyResolver(container); config.DependencyResolver = dependencyResolver; app.UseAutofacMiddleware(container); app.UseAutofacWebApi(config); WebApiConfig.Register(config); app

SignalR + Autofac + OWIN: Why doesn&#039;t GlobalHost.ConnectionManager.GetHubContext work?

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to use OWIN, SignalR and Autofac in a single project. I'm setting things up with regards to signalR as follows: // Create the AutoFac container builder: var builder = new ContainerBuilder(); // ...[Register various other things in here]... // register signalR Hubs builder.RegisterHubs(Assembly.GetExecutingAssembly()); // Build the container: var container = builder.Build(); // Configure SignalR with the dependency resolver. app.MapSignalR(new HubConfiguration { Resolver = new AutofacDependencyResolver(container) }); My issue is

Using log4net with Autofac

匿名 (未验证) 提交于 2019-12-03 01:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to use log4net with Autofac. I've pasted this code http://autofac.readthedocs.org/en/latest/examples/log4net.html , and from Program.cs/Main() I am doing var iocBuilder = new ContainerBuilder(); iocBuilder.RegisterModule(new LoggingModule()); var iocContainer = iocBuilder.Build(); now I would like to try this out immediately (in the next line), writing a simple line to the log file. How should I do it? I am thinking of something like this var ls = iocContainer.Resolve<LoggingModule>(); ls.Info("the logging is working"); Thanks a

DbContext has been disposed and autofac

匿名 (未验证) 提交于 2019-12-03 01:06:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a controller: private readonly ILogger _logger; private readonly IRepository _repository; public HomeController(ILogger logger, IRepository repository) { _logger = logger; _repository = repository; } This is the repository: public class EfRepository : IRepository { // ...methods for add, delete, update entities // .... public void Dispose() { if (this._context != null) { this._context.SaveChanges(); (this._context as IDisposable).Dispose(); this._context = null; } } } Finally, registration types in IoC: _builder.RegisterType ().As ();