SignalR 循序渐进(四) Hub的生命周期以及IoC

王朝学院·作者佚名  2016-05-17
窄屏简体版  字體: 小  |  中  |  大  |  超大  

SignalR 循序渐进(四) Hub的生命周期以及IoC有阵子没更新这个系列了,最近太忙了。本篇带来的是Hub的生命周期以及IoC。

首先,Hub的生命周期,我们用一个Demo来看看:

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方法。我们来看看实际运行情况:

可以看到,客户端每请求一次服务端,都会创建一个新的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的实例进行注入:

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); }); } }

这样改造后,我们再来看看实际运行情况:

好,目前来说一切都ok,我们能通过全局注册实例。现在有一个这样的需求,TestHub的构造函数需要注入一个仓储接口,例如:

public class TestHub : Hub { int count; public TestHub(IRepository repository) { this.repository = repository; } IRepository repository; public void Hello() { count++; repository.Save(count); } }

这样改完以后,势必需要在Startup里也做改动:

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了,如图:

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); } }

看一下最终的效果:

转载请注明出处:http://www.cnblogs.com/royding/p/3875915.html

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航