SignalR + Dependency Injection Questions

前端 未结 4 1269
醉酒成梦
醉酒成梦 2020-12-29 23:47

I am using SignalR in my MVC3 application, and since I have implemented StructureMap Dependency Injection on my controllers I would like to do the same in my hub, but I can\

4条回答
  •  Happy的楠姐
    2020-12-30 00:32

    Add something like this to a file in your App_Start folder. This code snippet is for Ninject, so just replace AspNetHost.SetResolver()

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    using Ninject;
    using SignalR.Hosting.AspNet;
    using SignalR.Infrastructure;
    using SignalR.Ninject;
    using Web.Models;
    
    [assembly: WebActivator.PreApplicationStartMethod(typeof(Web.App_Start.NinjectSignalR), "Start")]
    
    namespace Web.App_Start
    {
        public static class NinjectSignalR
        {
            public static void Start()
            {
                IKernel kernel = CreateKernel();
                // switch this line to the structuremap resolver
                AspNetHost.SetResolver(new NinjectDependencyResolver(kernel));
            }
    
            private static IKernel CreateKernel()
            {
                var kernel = new StandardKernel();
                RegisterServices(kernel);
                return kernel;
            }
    
            private static void RegisterServices(IKernel kernel)
            {
                // add your services here
                //kernel.Bind().To();
            }
        }
    }
    

提交回复
热议问题