扩展为 ASP.NET SignalR Backplane
NCache 扩展 ID依赖解析器 与其接口 UseNCache
方法,仅需要添加的项目的缓存名称和事件键。 这充当客户端针对 SignalR 实现的注册点。
备注
此功能也可用于 NCache Professional.
UseNCache
接受以下参数:
参数 | Type | 课程描述 |
---|---|---|
cacheName |
string |
中的缓存名称 NCache 它将存储相应的项目,以便客户端通过以下方式跟踪更新 itemVersion . |
eventKey |
string |
添加到的项目的唯一用户指定键属性 NCache 关于客户注册。 事件键特定于应用程序。 应用程序的每个客户端在调用时将使用相同的事件密钥 NCache的扩展方法。 |
configuration |
NCacheScaleoutConfiguration |
一个对象 NCacheScaleoutConfiguration 这扩展了 ScaleoutConfiguration 上课并参加 cacheName , eventKey , userid 及 password 作为成员。 |
先决条件 NCache 扩展为 ASP.NET SignalR Backplane
- 根据您的应用程序在您的应用程序中安装以下 NuGet 包 NCache 版本:
- 要使用该扩展,请在您的应用程序中包含以下命名空间 启动文件:
- Alachisoft.NCache.AspNet.SignalR
Microsoft.AspNet.SignalR
- 缓存必须正在运行。
- 确保使用 >= 2.4.0 的版本 ASP.NET SignalR.
- 有关 API 详细信息,请参阅: 使用NCache.
- 确保添加的数据是 可序列化.
- 为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
- 要处理任何看不见的异常,请参阅 故障排除 部分。
修改 Web.config
添加 ConnectionOptions
部分 <configSections>
如下所示。
<configuration>
<configSections>
<section name="ConnectionOptions"
type="Alachisoft.NCache.AspNet.SignalR.SignalRConnectionOptions"/>
</configSections>
</configuration>
添加 ConnectionOptions
部分 <configuration>
设置客户端与缓存连接时要使用的不同属性。 有关参数的详细说明,请参阅 ConnectionOptions
对象,参考 CacheConnectionOptions 属性.
<configuration>
<ConnectionOptions ClientBindIp="" AppName="DemoAppName" EnableClientLogs="false" LogLevel="info">
<Server name="20.200.21.39" port="9800"/>
<UserCredentials userID="john doe" password="1234" />
</ConnectionOptions>
</configuration>
地址 appSettings
部分指定 cacheName
和 eventKey
in <configuration>
如下所示。
<appSettings>
<add key="cache" value="demoCache" />
<add key="eventKey" value="chatApplication" />
</appSettings>
注册客户端以使用 NCache
注册一个实例 UseNCache
方法中 启动文件 在以下任一重载中的应用程序:
过载 1:
public static IDependencyResolver UseNCache(this IDependencyResolver resolver, string cacheName, string eventKey, string userID = null, string password = null)
public class Startup
{
public void Configuration(IAppBuilder app)
{
string cache, eventKey;
cache = ConfigurationManager.AppSettings["cache"];
eventKey = ConfigurationManager.AppSettings["eventKey"];
GlobalHost.DependencyResolver.UseNCache(cache, eventKey);
//using NCache SignalR
app.MapSignalR();
}
}
过载 2:
public static IDependencyResolver UseNCache(this IDependencyResolver resolver, NCacheScaleoutConfiguration configuration)
public class Startup
{
public void Configuration(IAppBuilder app)
{
string cache, eventKey, userId ="", password="";
cache = ConfigurationManager.AppSettings["cache"];
eventKey = ConfigurationManager.AppSettings["eventKey"];
var options = (CacheConnectionOptions)ConfigurationManager.GetSection("ConnectionOptions");
if (options!= null && options.UserCredentials != null)
{
userId = options.UserCredentials.UserID;
password = options.UserCredentials.Password;
}
var configuration = new NCacheScaleoutConfiguration(cache, eventKey, userId, password);
GlobalHost.DependencyResolver.UseNCache(configuration); //using NCache SignalR
app.MapSignalR();
}
}
更多资讯
NCache 为 SignalR 提供示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache.AspNet.SignalR 命名空间。