Удалить привязку в одном приложении с помощью mvc5 / WebApi / Signalr - PullRequest
0 голосов
/ 04 сентября 2018

Я использую Ninject в своем веб-приложении.

Коды:

public interface ICustomerService
{}

public class CustomerService : ICustomerService
{
    public CustomerService(IContext context)
    {
        /*Code*/
    }
}

public interface IContext
{}

public class Context : IContext
{
    public Context(IApplicationContext applicationContext)
    {
        /*Code*/
    }
}

public class TestController : Controller
{
    public TestController(IContext context, ICustomerService customerService) 
    {
        /*Code*/
    }
}

public class TestApiController : ApiController
{
    public TestController(IContext context, ICustomerService customerService) 
    {
        /*Code*/
    }
}

public partial class MyHub : Hub
{
    public MyHub(IContext context, ICustomerService customerService)
    {
        /*Code*/
    }
}

Ninject bindings:

kernel.Bind<IApplicationContext>().ToMethod(context => { return HttpContext.Current.GetOwinContext().Get<IApplicationContext>();}).InRequestScope();
kernel.Bind<IContext>().To<Context>().InRequestScope();
kernel.Bind<ICustomerService>().To<CustomerService>().InRequestScope();

Мои сервисы правильно связаны и вводятся в майские контроллеры и ApiControllers с помощью InRequestScope ()

Но для концентратора-сигнализатора InRequestScope действует только в случае подключения, а не в случае отключения (HttpContext.Current = null).

Я внес некоторые изменения в область привязки

kernel.Bind<IHermesApplicationContext>().ToMethod(context => {
    IApplicationContext applicationContext =  HttpContext.Current?.GetOwinContext().Get<IApplicationContext>();
                return applicationContext ?? new UnknownApplicationContext();
            }).InRequestScope();

В этом случае (отключение концентратора) мой контекст не является единичным экземпляром.

Какая хорошая практика для реализации этого?

Спасибо

...