SignalR 按部就班(四) Hub的生命週期以及IoC

有陣子沒更新這個系列了,最近太忙了。本篇帶來的是Hub的生命週期以及IoC。html

首先,Hub的生命週期,咱們用一個Demo來看看:app

public class TestHub : Hub
    {
        public TestHub()
        {
            Console.WriteLine(Guid.NewGuid().ToString());
        }

        public void Hello() { }
    }
static HubConnection hubConnection;
        static IHubProxy hubProxy;
        static List<IDisposable> clientHandlers = new List<IDisposable>();

        static void Main(string[] args)
        {
            hubConnection = new HubConnection("http://127.0.0.1:10086/");
            hubProxy = hubConnection.CreateHubProxy("TestHub");

            hubConnection.Start().ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    Console.WriteLine(t.Exception.Message);
                }
                else
                {
                    Console.WriteLine("Connectioned");
                }
            }).Wait();

            while (true)
            {
                hubProxy.Invoke("Hello");
                Thread.Sleep(1000);
            }
        }

給測試Hub增長構造函數,在裏面輸出一個Guid。而後客戶端調用一個空的Hello方法。咱們來看看實際運行狀況:ide

image

能夠看到,客戶端每請求一次服務端,都會建立一個新的Hub實例來進行操做。函數

好,這是基本的應用場景。若是Hub裏有一些須要持久的東西,好比一個訪問計數器,咱們把Hub改一下:測試

public class TestHub : Hub
    {
        int count;

        public TestHub()
        {
            Console.WriteLine(Guid.NewGuid().ToString());
        }

        public void Hello()
        {
            count++;
            Console.WriteLine(count);
        }
    }

這時候要怎麼辦呢?這時候就須要對Hub進行實例管理。在Startup裏能夠經過Ioc的方式將Hub的實例進行注入:ui

public class Startup
    {
        TestHub testHub = new TestHub();

        public void Configuration(IAppBuilder app)
        {
            GlobalHost.DependencyResolver.Register(typeof(TestHub), () => testHub);

            app.Map("/signalr", map =>
            {
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration
                {
                    EnableDetailedErrors = true,
                    EnableJSONP = true
                };
                map.RunSignalR(hubConfiguration);
            });
        }
    }

這樣改造後,咱們再來看看實際運行狀況:this

image

好,目前來講一切都ok,咱們能經過全局註冊實例。如今有一個這樣的需求,TestHub的構造函數須要注入一個倉儲接口,例如:url

public class TestHub : Hub
    {
        int count;

        public TestHub(IRepository repository)
        {
            this.repository = repository;
        }

        IRepository repository;

        public void Hello()
        {
            count++;
            repository.Save(count);
        }
    }

這樣改完之後,勢必須要在Startup裏也作改動:spa

public class Startup
    {
        public Startup(IRepository repository)
        {
            testHub = new TestHub(repository);
        }

        TestHub testHub;

        public void Configuration(IAppBuilder app)
        {
            GlobalHost.DependencyResolver.Register(typeof(TestHub), () => testHub);

            app.Map("/signalr", map =>
            {
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration
                {
                    EnableDetailedErrors = true,
                    EnableJSONP = true
                };
                map.RunSignalR(hubConfiguration);
            });
        }
    }

好,到這步位置,一切都在掌控之中,只要咱們在入口的地方用本身熟悉的IoC組件把實例注入進來就ok了,如圖:code

image

WebApp.Start徹底沒有地方給予依賴注入,這條路走不通,看來得另尋方法。

Owin提供了第二種方式來啓動,經過服務工廠來解決IoC的問題,並且是原生支持:

static void Main(string[] args)
        {
            var url = "http://*:10086/";
            var serviceProviders = (ServiceProvider)ServicesFactory.Create();
            var startOptions = new StartOptions(url);
            serviceProviders.Add<IRepository, Repository>();
            var hostingStarter = serviceProviders.GetService<IHostingStarter>();

            hostingStarter.Start(startOptions);
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }

咱們將輸出計數的位置挪到倉儲實例裏:

public class Repository : IRepository
    {
        public void Save(int count)
        {
            Console.WriteLine(count);
        }
    }

看一下最終的效果:

image

 

 

轉載請註明出處:http://www.cnblogs.com/royding/p/3875915.html 

相關文章
相關標籤/搜索