0

I am using my custom virtual provider in my ASP.Net MVC4 project. Let's call the provider MyVirtualPathProvider. The provider is registered in OnApplicationStarted method like this:

HostingEnvironment.RegisterVirtualPathProvider(new MyVirtualPathProvider());

The method is part of Global.asax.cs. On the top of which I have the following:

public class MvcApplication : NinjectHttpApplication

Now, I would like to use [Inject] attribute to inject one of my services, just like this:

    [Inject]
    public ILogger Logger { get; set; }

In the custom virtual path provider I have overridden GetFile method where I would like to log something using the injected Logger:

     public override VirtualFile GetFile(string virtualPath)
     {
     ...
         Logger.Log(...);

Unfortunately, the Logger is null therefore it does not work.

I have tried to pass the Logger as a parameter in the custom virtual path provider constructor, like this:

HostingEnvironment.RegisterVirtualPathProvider(new MyVirtualPathProvider(Kernel.Get<ILogger>()));

Unfortunately, it is not the way to go, because I have the warning: "warning CS0618: 'Ninject.Web.Common.NinjectHttpApplication.Kernel' is obsolete: 'Do not use Ninject as Service Locator'"

What have I done wrong? Please advise.

Antipod
  • 1,593
  • 3
  • 23
  • 43

1 Answers1

0

You may want to view logging as a cross-cutting concern and make use of the Ambient Context design pattern. It can help in cases where you don't want to pollute the constructors with a generally used type (in the same way you may want DateTime providers). If you went with this approach you would side-step the warning your getting because you would not need to inject types into your path provider.

Jeremy F
  • 417
  • 2
  • 16