zoukankan      html  css  js  c++  java
  • 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方法。我们来看看实际运行情况:

    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的实例进行注入:

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

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

    image

    好,目前来说一切都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了,如图:

    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 

  • 相关阅读:
    Voice over IP
    [转】:TCP/IP详解学习笔记(5)
    windows phone 7 version: ObservableCollectionEx (1)
    MA0003 移动智能网原理
    TCP 网络书籍
    windows Phone 7如何实现background的情况下不丢失数据
    最近想要学习和了解的东东
    Windows phone 7 开发注意事项
    android Tab标签下得按钮
    新浪微博教程(一)
  • 原文地址:https://www.cnblogs.com/royding/p/3875915.html
Copyright © 2011-2022 走看看