DependencyResolver + Owin + WebApi2

One of Owin's greatest strengths is that it is not dependent on System.Web . How do I configure DI if WebApi explicitly requires something in this direction:

 var config = new HttpConfiguration(); var container = new WindsorContainer().Install(new ControllerInstaller()); container.Install(FromAssembly.This()); config.DependencyResolver = ... 

Where config.DependencyResolver require a concrete IDependencyResolver that comes from System.Web.Http.Dependencies ?

I'm especially interested in C # code that uses WebApi + Owin + Castle.Windsor (Google hasn’t helped yet).

+5
source share
2 answers

I managed to get it to work using:

 [assembly: OwinStartup(typeof(Bla.Startup))] namespace Bla { public class Startup { public void Configuration(IAppBuilder app) { //... var container = new WindsorContainer().Install(new ControllerInstaller()); var httpDependencyResolver = new WindsorHttpDependencyResolver(container); config.DependencyResolver = httpDependencyResolver; //... } } public class ControllerInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register(AllTypes.FromThisAssembly() .Pick().If(t => t.Name.EndsWith("Controller")) .Configure(configurer => configurer.Named(configurer.Implementation.Name)) .LifestylePerWebRequest()); //... } } internal class WindsorDependencyScope : IDependencyScope { private readonly IWindsorContainer _container; private readonly IDisposable _scope; public WindsorDependencyScope(IWindsorContainer container) { if (container == null) { throw new ArgumentNullException("container"); } _container = container; _scope = container.BeginScope(); } public object GetService(Type t) { return _container.Kernel.HasComponent(t) ? _container.Resolve(t) : null; } public IEnumerable<object> GetServices(Type t) { return _container.ResolveAll(t) .Cast<object>().ToArray(); } public void Dispose() { _scope.Dispose(); } } internal sealed class WindsorHttpDependencyResolver : IDependencyResolver { private readonly IWindsorContainer _container; public WindsorHttpDependencyResolver(IWindsorContainer container) { if (container == null) { throw new ArgumentNullException("container"); } _container = container; } public object GetService(Type t) { return _container.Kernel.HasComponent(t) ? _container.Resolve(t) : null; } public IEnumerable<object> GetServices(Type t) { return _container.ResolveAll(t) .Cast<object>().ToArray(); } public IDependencyScope BeginScope() { return new WindsorDependencyScope(_container); } public void Dispose() { } } 

The problem I am facing is that using:

 config.DependencyResolver = httpDependencyResolver; 

introduces a dependency on system.web. Therefore, I am having problems when I try to use owin testerver in some integration tests. I will post one more question.

+6
source

Take a look right here - “Injecting Dependencies in ASP.NET Web Interface Using Castle Windsor by Mark Seemann” . Then go to the Mark Seemans blog blog . He talks a lot about the DI and WEB API, and he uses Castle Windsor a lot. I bet Windsor Castle is his favorite DI container. When you look at the archive, do not look only at the WEB API. Sometimes he writes about the WEB API under a different heading .

If you read your excellent book , you will get a very good understanding on IoC / DI. A very good book.

+4
source

Source: https://habr.com/ru/post/1200814/


All Articles