简体   繁体   English

在SignalR Project外部调用Client方法

[英]Call Client method outside SignalR Project

I am having concerns about how to use SgnalR in the following scenario: 我担心在以下情况下如何使用SgnalR:

There is a non-hub service project that runs a time-consuming task periodically. 有一个非中心服务项目,该项目定期运行耗时的任务。 The clients should be notified about the progress of the running task. 应将运行任务的进度通知客户端。 After making some research, SignalR seemed to be the right choice for this purpose. 经过研究后,SignalR似乎是实现此目的的正确选择。

The problem is, I want the Service-Hub-Clients system to be as loosely-coupled as possible. 问题是,我希望Service-Hub-Clients系统尽可能宽松地耦合。 So, I hosted the Hub in IIS and as a SignalR documentation suggests, added a reference to the Hub context in the outside project and called the client method: 因此,我将Hub托管在IIS中,并且正如SignalR文档建议的那样,在外部项目中添加了对Hub上下文的引用,并称为客户端方法:

    hubContext = GlobalHost.ConnectionManager.GetHubContext<TheHub>()
    hubContext.Clients.All.progress(n, i);

Client side: 客户端:

    private void InitHub()
    {
        hubConnection = new HubConnection(ConfigurationManager.AppSettings["hubConnection"]);

        hubProxy = hubConnection.CreateHubProxy("TheHub");
        hubConnection.Start().Wait();
    }

    hubProxy.On<int, int>("progress", (total, done) =>
        {
            task1Bar.Invoke(t => t.Maximum = total);
            task1Bar.Invoke(t => t.Value = done);
        });

On the client side the method isn't being invoked and after two days of research I can't get it working, although when making a call from the Hub itself, it works fine. 在客户端,该方法没有被调用,经过两天的研究,我无法使其工作,尽管从集线器本身进行调用时,它仍然可以正常工作。 I suspect I'm missing some configuration 我怀疑我缺少一些配置

You can't use the GlobalHost.Connection manager in your Hub class or service, if the caller is going to be any project other than the Web project. 如果调用者将是Web项目以外的任何项目,则不能在Hub类或服务中使用GlobalHost.Connection管理器。

GlobalHost.ConnectionManager.GetHubContext<TheHub>()

You should instead create a service class that would abstract the hub from the callers. 相反,您应该创建一个服务类,该服务类将从调用方中提取集线器。 The service class should have something like: 服务类应具有以下内容:

// This method is what the caller sees, and abstracts the communication with the Hub
public void NotifyGroup(string groupName, string message)
{
  Execute("NotifyGroup", groupName, message);
}

// This is the method that calls the Hub
private void Execute(string methodName, params object[] parameters)
{
  using (var connection = new HubConnection("http://localhost/"))
  {
    _myHub = connection.CreateHubProxy("TheHub");
    connection.Start().Wait();
    _myHub.Invoke(methodName, parameters);
    connection.Stop();
  }
}

The last bit which is the hub itself, should be something like: 最后一点是集线器本身,应类似于:

public void NotifyGroup(string groupName, string message)
{
  var group = Clients.Group(groupName);
  if (group == null)
  {
    Log.IfWarn(() => $"Group '{groupName}' is not registered");
    return;
  }
  group.NotifyGroup(message);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM