Contribution to Delegation Dependence

I am new to dependency injection, but I am happy with Ninject and Ninject.Extensions.Logging to [Inject] my ILogger wherever I need it.

However, some DelegatingHandlers spoil all the fun.

 public class HttpsHandler : DelegatingHandler { [Inject] public ILogger Logger { get; set; } protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { if (!string.Equals(request.RequestUri.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { Logger.Info(String.Format("{0}: is using HTTP", request.RemoteAddress()); return Task.Factory.StartNew( () => new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("HTTPS Required") }); } return base.SendAsync(request, cancellationToken); } } 

Can someone point me in the right direction how to insert Ninject.Extensions.Logger.Nlog2 into Ilogger inside the delegating manipulators?

Update

I think Pete led me in the right direction in the comments (thanks!). I added the following constructor to the HttpsHandler :

 public HttpsHandler() { var kernel = new StandardKernel(); var logfactory = kernel.Get<ILoggerFactory>(); this.Logger = logfactory.GetCurrentClassLogger(); } 

and now I have Logger running!

All that remains is my question: is this the right way to do this, or is it an anti-pattern?

+6
source share
4 answers

DelegatingHandlers initialized only once in the Web API when the application starts.

This is a known issue / design of the web API function (I guess for performance reasons) - see the bug report here http://aspnetwebstack.codeplex.com/workitem/62 .

Using constructor initialization, as you yourself suggested, will only work if there are dependencies such as singleton (which is "why do you work with the registrar"). Regardless, if you want to delegate the web API permission to DependencyResolver , you must use GetDependencyScope() , which you can undo HttpRequestMessage as a workaround.

I published a walkthrough on this with Ninject some time ago. You should use this approach to solve your detergent, because with your current solution you linked Ninject and your handler, which is far from necessary.

+15
source

I use this:

  protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var service = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IFooService)) as IFooService; // Other stuff } 
+2
source

I think you need this .

This is the Ninject dependency converter for MVC. Then I believe that you need to use:

 GlobalConfiguration.Configuration.ServiceResolver.SetResolver() 

And pass in the NInject dependency resolver.

+1
source

I had a similar situation and got tips from this publication and related links, I came up with the following solution for constructor injection, which seems to work fine for me:

 using Ninject.Extensions.Logging; 

public static void Register (HttpConfiguration) {

 ILoggerFactory loggerFactory = config.DependencyResolver.GetService(typeof(ILoggerFactory)) as ILoggerFactory; config.MessageHandlers.Add( new HttpsHandler(loggerFactory.GetLogger(typeof(HttpsHandler)))); // Other codes ... 

}

I use Ninject for WebAi 2 / log4net and installed all the relevant nuget packages

0
source

All Articles