您好我有一个WCF服务,它接收来自Web前端的请求。然后,服务连接到SignalR集线器并向订户发送消息。这一切都适用于单个请求。但是当我用多个请求加载服务时(通常不超过8个)。 signalR连接似乎丢失,Web客户端无法连接到Hub。一段时间后,这最终会释放,客户再次开始连接。
我的假设是它与WCF服务中的HubConnection的生命周期有关。我尝试了以下方法来解决这个问题,但似乎都没有。我确定我错过了一些非常明显的东西。
方法1 - 创建连接,不要处置并让它超出GC的范围。
// Establish connection to the SignalRHub.
var hubConnection = new HubConnection(this.signalRHost + "/signalr", useDefaultUrl: false);
hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
IHubProxy hubProxy = hubConnection.CreateHubProxy(this.dataCollectionServiceHubName);
// Start connection to signalR hub.
hubConnection.Start().ContinueWith((result) =>
{
hubProxy.Invoke("SendAll", message);
});
方法2 - 在发送消息后呼叫停止。
// Establish connection to the SignalRHub.
var hubConnection = new HubConnection(this.signalRHost + "/signalr", useDefaultUrl: false);
hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
IHubProxy hubProxy = hubConnection.CreateHubProxy(this.dataCollectionServiceHubName);
// Start connection to signalR hub.
hubConnection.Start().ContinueWith((result) =>
{
hubProxy.Invoke("SendAll", message).ContinueWith((r) => { hubConnection.Stop(); });
});
}
方法3 - 使用块中的crate连接,以便在发送后调用Dispose。
using (var hubConnection = this.connectionFactory.Create())
{
// Establish connection to the SignalRHub.
IHubProxy hubProxy = hubConnection.CreateHubProxy(this.dataCollectionServiceHubName);
// Start connection to signalR hub.
hubConnection.Start().ContinueWith((result) =>
{
hubProxy.Invoke("SendAll", message);
});
}
方法4创建单例集线器
public sealed class SignalRConnectionFactory : ISignalRConnectionFactory
{
/// <summary>
/// The instance.
/// </summary>
private static volatile SignalRConnectionFactory instance;
/// <summary>
/// The sync root.
/// </summary>
private static object syncRoot = new Object();
/// <summary>
/// The signal r host.
/// </summary>
private string signalRHost = ConfigurationManager.AppSettings["SignalRHost"];
/// <summary>
/// The hub connection.
/// </summary>
private HubConnection hubConnection;
/// <summary>
/// Prevents a default instance of the <see cref="SignalRConnectionFactory"/> class from being created.
/// </summary>
private SignalRConnectionFactory()
{
this.hubConnection = new HubConnection(this.signalRHost + "/signalr", useDefaultUrl: false);
this.hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
}
/// <summary>
/// Gets the instance.
/// </summary>
public static SignalRConnectionFactory Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new SignalRConnectionFactory();
}
}
}
return instance;
}
}
/// <summary>
/// The create.
/// </summary>
/// <returns>
/// The <see cref="HubConnection"/>.
/// </returns>
public HubConnection Create()
{
return this.hubConnection;
}
}
using (var hubConnection = this.connectionFactory.Create())
{
// Establish connection to the SignalRHub.
IHubProxy hubProxy = hubConnection.CreateHubProxy(this.dataCollectionServiceHubName);
// Start connection to signalR hub.
hubConnection.Start().ContinueWith((result) =>
{
hubProxy.Invoke("SendAll", message);
});
}