SignalR.NET客户端:在打开相同连接的情况下调用后续方法
我有一个.NET客户端应用程序,连接到由.NET mvc5网络应用程序托管的信号器中心。客户端应用程序的主要例程执行以下操作:SignalR.NET客户端:在打开相同连接的情况下调用后续方法,signalr,signalr-hub,signalr.client,Signalr,Signalr Hub,Signalr.client,我有一个.NET客户端应用程序,连接到由.NET mvc5网络应用程序托管的信号器中心。客户端应用程序的主要例程执行以下操作: int num1 = _myService.GetSomeHubMethod(); int num2 = _myService.GetSomeOtherHubMethod(); ... 其中_myService是类的实例: public class MyService { ... private HubConnection connection;
int num1 = _myService.GetSomeHubMethod();
int num2 = _myService.GetSomeOtherHubMethod();
...
其中_myService是类的实例:
public class MyService
{
...
private HubConnection connection;
private IHubProxy hubProxy;
...
public MyService()
{
...
if (BASE_SITE_URL == null)
BASE_SITE_URL = ConfigurationManager.AppSettings["BASE_SITE_URL"].ToString();
if (connection == null)
connection = new HubConnection(BASE_SITE_URL);
if (hubProxy == null)
hubProxy = connection.CreateHubProxy("MyHub");
connection.Start().Wait();
}
...
public int GetSomeHubMethod()
{
//connection.Start().Wait();
var t = hubProxy.Invoke<int>("SomeHubMethod");
int result = t.Result;
//connection.Stop();
return result;
}
public int GetSomeOtherHubMethod()
{
//connection.Start().Wait();
var t = hubProxy.Invoke<int>("SomeOtherHubMethod");
int result = t.Result;
//connection.Stop();
return result;
}
}
问题是:第一个调用的计算结果正确,但第二个调用在
int result=t.result;
线路。特别是,t.状态为“等待激活”
如果我在主例程中切换两个调用的顺序,第一个调用将被执行,第二个调用将“挂起”
注意:如果我在这两个方法中启动和停止连接(请参阅注释行),而不是在MyService的构造函数中调用connection.start().Wait(),那么它工作得很好,但停止和启动需要太多时间
谢谢大家的帮助 好吧,看来这是个僵局。我不得不这样修改方法:
public async Task<int> SomeHubMethod()
{
return await Task.FromResult<int>(1);
}
...
public async Task<int> GetSomeHubMethod()
{
return await chatHubProxy.Invoke<int>("SomeHubMethod");
}
...
int num1 = await _myService.GetSomeHubMethod();
public异步任务方法()
{
返回等待任务。FromResult(1);
}
...
公共异步任务GetSomeHubMethod()
{
返回wait wait chatHubProxy.Invoke(“SomeHubMethod”);
}
...
int num1=wait_myService.GetSomeHubMethod();
好的,我承认我在异步方面还有很多工作要做…我无法在SignalR 2.0.3上重新编程,你使用的是什么版本?@Xiaohong Same,2.0.3。你能调用这两个方法吗?不能“wait_myService.GetSomeHubMethod();”返回任务,而不是int?
public async Task<int> SomeHubMethod()
{
return await Task.FromResult<int>(1);
}
...
public async Task<int> GetSomeHubMethod()
{
return await chatHubProxy.Invoke<int>("SomeHubMethod");
}
...
int num1 = await _myService.GetSomeHubMethod();