有阵子没更新这个系列了,最近太忙了。本篇带来的是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();
}
}

给测试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 

最新文章

  1. Win8.1 安装Express 框架
  2. 发布和运行HOLOLENS程序注意这里要勾上,不然就成普通的UWP程序了!
  3. 【C】二级指针探秘 &amp; 星号的两种用法(1.与基本类型结合形成另一种类型,比如与int结合形成int* 2.取值操作)
  4. Web 在线文件管理器学习笔记与总结(13)重命名文件夹(14)复制文件夹
  5. linux终端io笔记
  6. Java设计模式---工厂方法模式(Factory-Method)
  7. 【剑指offer】求逆序对的个数
  8. freemarker跳出循环
  9. Base64工具类
  10. NSTemporaryDirectory 临时文件
  11. Eclipse 修改maven 仓储Repository位置
  12. IOS中单例NSUserDefaults的使用(转)
  13. ASP.NET MVC5 Forms登陆+权限控制(控制到Action)
  14. 简单谈谈数据库DML、DDL和DCL的区别
  15. CSS div 高度满屏
  16. python 前后端分离 简单的数据库返回接口
  17. map &amp;&amp; multimap
  18. swoole 定时器
  19. php curl 传输是url中带有空格的处理方法
  20. 2019.01.22 zoj3583 Simple Path(并查集+枚举)

热门文章

  1. Google 商店:您的应用静态链接到的 OpenSSL 版本有多个安全漏洞。建议您尽快更新 OpenSSL
  2. python 使用urllib.urlopen超时问题的解决方法
  3. HDU 1863 畅通工程 克鲁斯卡尔算法
  4. 新建一个用户,让他只能看到某一个视图(View),如何设置
  5. hdu3948(后缀数组)
  6. IIS短文件名泄露漏洞危害及防范方法(转)
  7. PHP——0128练习相关2——js点击button按钮跳转到另一个新页面
  8. Gogs http和ssh地址显示localhost的问题
  9. cs108 03 ( 调试, java通用性)
  10. JavaScript处理JSON