有阵子没更新这个系列了,最近太忙了。本篇带来的是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/WebEnh/
如果想下次快速找到我,记得点下面的关注哦!
| 本博客Android APP 下载 |
![]() |
| 支持我们就给我们点打赏 |
![]() |
| 支付宝打赏 支付宝扫一扫二维码 |
![]() |
| 微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!









浙公网安备 33010602011771号