0

考虑下面的类:

using Microsoft.AspNet.SignalR;
public class TwitterStream
    {
        // Hub Context
        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<GeoFeedHub>();

        public void ChangeStreamBounds(double latitude, double longitude)
        {
            Debug.WriteLine(latitude + "-" + longitude);
        }

        // Lots of other interesting code redacted
   }

ChangeStreamBounds即使它在 Hub 类之外,是否可以从客户端调用该方法?可以从服务器(以及从 Hub 类之外)调用客户端函数,但是否可以反过来呢?

不幸的是,我让自己陷入了困境,并且必须从我编写的类中执行代码(而不是从集线器本身 - 除非您当然可以将 SignalR 集线器作为任务工厂运行)

4

1 回答 1

2

您的问题可能有一个涉及HubConnections 和IHubProxys (允许您绑定到集线器方法调用)或较低级别 API 的答案,但我认为您可能会以错误的方式解决这个问题。

从概念上讲,您希望GeoFeedHub处理客户端请求,以及TwitterStream处理与 Twitter API 交互的类。因此,您的GeoFeedHub课程依赖于TwitterStream.

你的TwitterStream类有async方法很好,这在 SignalR 中得到完全支持。您可以拥有调用 的异步 Hub 方法TwitterStream,这样就无需TaskFactoryGlobal.asax.

与其TwitterStream在应用程序启动时创建您的应用程序并尝试找到一种将 Hub 调用绑定到它的方法(向后依赖并违反单一责任原则),不如让您的 Hub 作为实时客户端之间的联系点会更干净,并将 的实例TwitterStream注入 ,GeoFeedHub以便 Hub 可以访问 Twitter API。


这里有一些示例代码可以说明这个想法:

public class GeoFeedHub : Hub
{
    // Declare dependency on TwitterStream class
    private readonly TwitterStream _twitterStream;

    // Use constructor injection to get an instance of TwitterStream
    public GeoFeedHub(TwitterStream _twitterStream)
    {
        _twitterStream = _twitterStream;
    }

    // Clients can call this method, which uses the instance of TwitterStream
    public async Task SetStreamBounds(double latitude, double longitude)
    {
        await _twitterStream.SetStreamBoundsAsync(latitude, longitude);
    }
}


public class TwitterStream
{
    public TwitterStream() 
    {
    }

    public async Task SetStreamBoundsAsync(double latitude, double longitude)
    {
        // Do something with Twitter here maybe?
        await SomeComponent.SetStreamBoundsAsync(latitude, longitude);
    }

    // More awesome code here
}

我在示例中使用了 DI,因此这里有一些您需要将其连接起来的胶水代码。此代码将进入您的App_Start文件夹:

// Configure Unity as our DI container
public class UnityConfig
{

    private static readonly Lazy<IUnityContainer> Container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        RegisterTypes(container);
        return container;
    });

    public static IUnityContainer GetConfiguredContainer()
    {
        return Container.Value;
    }


    private static void RegisterTypes(IUnityContainer container)
    {
        var twitterService = new TwitterService();
        container.RegisterInstance(twitterService);

        /*
         * Using RegisterInstance effectively makes a Singleton instance of 
         * the object accessible throughout the application.  If you don't need 
         * (or want) the class to be shared among all clients, then use 
         * container.RegisterType<TwitterService, TwitterService>();
         * which will create a new instance for each client (i.e. each time a Hub
         * is created, you'll get a brand new TwitterService object)
         */
    }
}


// If you're using ASP.NET, this can be used to set the DependencyResolver for SignalR 
// so it uses your configured container

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(UnitySignalRActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(UnitySignalRActivator), "Shutdown")]

public static class UnitySignalRActivator
{
    public static void Start() 
    {
        var container = UnityConfig.GetConfiguredContainer();
        GlobalHost.DependencyResolver = new SignalRUnityDependencyResolver(container);
    }

    public static void Shutdown()
    {
        var container = UnityConfig.GetConfiguredContainer();
        container.Dispose();
    }
}

public class SignalRUnityDependencyResolver : DefaultDependencyResolver
{
    private readonly IUnityContainer _container;

    public SignalRUnityDependencyResolver(IUnityContainer container)
    {
        _container = container;
    }

    public override object GetService(Type serviceType)
    {
        return _container.IsRegistered(serviceType) 
            ? _container.Resolve(serviceType) 
            : base.GetService(serviceType);
    }

    public override IEnumerable<object> GetServices(Type serviceType)
    {
        return _container.IsRegistered(serviceType) 
            ? _container.ResolveAll(serviceType) 
            : base.GetServices(serviceType);
    }
}
于 2014-07-21T21:02:33.020 回答