需要在消息处理程序中引用的全局对象

ton*_*ung 2 c# nservicebus signalr

我有一个信号器客户端,我想成为全球性的.

我认为在endpointconfig的Init()中创建signalr客户端是最好的.

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
    public static HubConnection hubConnection;
    public static IHubProxy hubProxy;

    public void Init()
    {
        Configure.With()
            .DefiningEventsAs(t => t.Namespace != null && t.Namespace.Contains(".Events."))
            .DefiningMessagesAs(t => t.Namespace != null && t.Namespace.Contains(".Messages."))
            .StructureMapBuilder(new Container(new DependencyRegistry()));

        Configure.Serialization.Json();

        hubConnection = new HubConnection("http://localhost:58120"); 
        hubProxy = hubConnection.CreateHubProxy("AmsHub");
        hubProxy.On<string>("receiveServerPush", x => System.Diagnostics.Debug.WriteLine(x));
        hubConnection.Start().Wait();
    }

    public class DependencyRegistry : Registry
    {
        public DependencyRegistry()
        {
            Scan(x =>
            {
                x.AssembliesFromApplicationBaseDirectory();
                x.ExcludeNamespace("StructureMap");
                x.WithDefaultConventions();
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我感到困惑的是,我应该如何在消息处理程序中引用hubConnection和hubProxy?我好像很喜欢操纵NServicebus.

public class TestHandler : IHandleMessages<AMS.Infrastructure.Events.IEvent>
{
    public void Handle(AMS.Infrastructure.Events.IEvent message)
    {
        EndpointConfig.hubProxy.Invoke("ServerFunction", "yodle");
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:我需要连接和代理是全局的原因是因为根据信号人员产生新的hubConnection是昂贵的.他们非常不鼓励一遍又一遍地创建和破坏集线器连接.他们发现使hubconnection全局/静态(?)没问题.

Ree*_*sey 5

在这种情况下,您的集线器连接/代理实际上与EndPointConfiguration类无关.他们不使用也不需要此类型的任何数据才能运行.

我建议将它们放在自己的懒惰初始化单例中,并在首次访问时自动启动它们.这看起来像:

public class Hub
{
    private static Lazy<Hub> instance = new Lazy<Hub>(() => new Hub());

    public static Hub Instance { get { return instance.Value; } }

    private Hub()
    {
        this.Connection = new HubConnection("http://localhost:58120"); 
        this.Proxy = Connection.CreateHubProxy("AmsHub");
        this.Proxy.On<string>("receiveServerPush", x => System.Diagnostics.Debug.WriteLine(x));
        this.Connection.Start().Wait(); 
    }

    public HubConnection Connection { get; private set; }
    public IHubProxy Proxy { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

您的消费者只需使用:

public class TestHandler : IHandleMessages<AMS.Infrastructure.Events.IEvent>
{
    public void Handle(AMS.Infrastructure.Events.IEvent message)
    {
        Hub.Instance.Proxy.Invoke("ServerFunction", "yodle");
    }
}
Run Code Online (Sandbox Code Playgroud)

这样做的好处是在第一次使用之前不会创建和启动,并将此类型隔离到它自己的类中.

鉴于您还在内部处理订阅,您还可以选择封装您的方法以简化使用:

public class Hub
{
    private static Lazy<Hub> instance = new Lazy<Hub>(() => new Hub());

    public static Hub Instance { get { return instance.Value; } }

    private Hub()
    {
        this.Connection = new HubConnection("http://localhost:58120"); 
        this.Proxy = Connection.CreateHubProxy("AmsHub");
        this.Proxy.On<string>("receiveServerPush", x => System.Diagnostics.Debug.WriteLine(x));
        this.Connection.Start().Wait(); 
    }

    private HubConnection Connection { get; set; }
    private IHubProxy Proxy { get; set; }

            public static Task Invoke(string method, params Object[] args)
            {
                 return Instance.Proxy.Invoke(method, args);
            }

            public static Task<T> Invoke<T>(string method, params Object[] args)
            {
                 return Instance.Proxy.Invoke<T>(method, args);
            }
}
Run Code Online (Sandbox Code Playgroud)

有了上述内容,您可以使用: Hub.Invoke("ServerFunction", "yodle");