I'm not very experienced with Ninject, so I may have a concept completely wrong here, but this is what I want to do. I have a multi-tenant web application and would like to inject a different class object depending on what URL was used to come to my site.

Something along the lines of this, although maybe I can use .When() in the binding, but you get the idea:

    private static void RegisterServices(IKernel kernel)
    {
        var currentTenant = TenantLookup.LookupByDomain(HttpContext.Current.Request.Url.Host.ToLower());
        if (currentTenant.Foldername == "insideeu")
        { kernel.Bind<ICustomerRepository>().To<AXCustomerRepository>(); }
        else
        { kernel.Bind<ICustomerRepository>().To<CustomerRepository>(); }
...

The problem is that HttpContext.Current is null at this point. So my question is how can I get the HttpContext data in NinjectWebCommon.RegisterServices. Any direction on where I might be going wrong with Ninject would be much appreciated as well.

Thank you

有帮助吗?

解决方案

The problem is that your binding here resolves at compile time; whereas you need it to resolve at runtime, for every request. To do this, use ToMethod:

Bind<ICustomerRepository>().ToMethod(context => 
    TenantLookup.LookupByDomain(HttpContext.Current.Request.Url.Host.ToLower()).Foldername == "insideeu" 
    ? new AXCustomerRepository() : new CustomerRepository());

This means that, every time the ICustomerRepository is called for, NInject will run the method using the current HttpContext, and instantiate the appropriate implementation.

Note that you can also use Get to resolve to the type rather than to the specific constructor:

Bind<ICustomerRepository>().ToMethod(context => 
    TenantLookup.LookupByDomain(HttpContext.Current.Request.Url.Host.ToLower())
        .Foldername == "insideeu" ?  
            context.Kernel.Get<AXCustomerRepository>() : context.Kernel.Get<CustomerRepository>()
    as ICustomerRepository);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top