SignalR Core Integration for NCache
NCache extends the ISignalRServerBuilder interface with its AddNCache method, which requires just the cache name and an event key for the item being added. If cache security is enabled, user credentials are also required. This acts as a registration point for clients against the ASP.NET Core SignalR implementation.
The AddNCache method takes the following parameters:
| Parameters | Type | Description |
|---|---|---|
CacheName |
string |
Name of the cache in NCache which will store the respective item for the client to trace updates via itemVersion. |
EventKey |
string |
Unique, user-specified key attribute for the item added to NCache on client registration. The event key is specific to the application. Each client of the application will use the same event key while calling NCache's extension method. |
ConnectionOptions |
ConnectionOptions |
ConnectionOptions object allows you to set properties to be used at the time of client connection with the cache. For a detailed description of parameters provided in the ConnectionOptions object, refer to CacheConnectionOptions Properties. |
Prerequisites
Before using the ASP.NET Core SignalR with NCache, ensure that the following prerequisites are fulfilled:
- Install the following NuGet packages in your application based on your NCache edition:
- Enterprise: AspNetCore.SignalR.NCache
- OpenSource: AspNetCore.SignalR.NCache.Opensource
- To utilize the extension, include the following namespaces in your application in Startup.cs:
- Alachisoft.NCache.AspNetCore.SignalR
Microsoft.AspNetCore.SignalR
- The cache must be running.
- Make sure to use version 1.1.0 of ASP.NET SignalR Core.
- For API details, refer to: AddNCache.
- Make sure that the data being added is serializable.
Modify appsettings.json
Add the following configurations to your appsettings.json file. In particular, you can provide a ConnectionOptions object to set different properties used during client connection to the cache. If any property is not found within the ConnectionOptions object or at the root level of NCacheConfiguration, its default value is used from client.ncconf. For a detailed description of the parameters provided in the ConnectionOptions object, refer to CacheConnectionOptions Properties.
"NCacheConfiguration": {
"CacheName": "demoCache",
"EventKey": "chatApplication",
"ConnectionOptions": {
"ClientBindIP": "",
"AppName": "DemoAppName",
"LogLevel": "info",
"UserCredentials": {
"UserID": "john smith",
"Password": "pass1234"
},
"ServerList": [
{
"Name": "20.200.20.40", "Port": 9800
}
]
}
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Register Clients to Use NCache
Register the AddNCache method in Startup.cs of your application using either of the following overloads based on your requirement.
Overload 1:
public static ISignalRServerBuilder AddNCache(this ISignalRServerBuilder signalRBuilder, string cacheName, string eventKey);
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
string cache, eventKey, userId, password;
cache = Configuration["NCacheConfiguration:CacheName"];
eventKey = Configuration["NCacheConfiguration:EventKey"];
services.AddSignalR().AddNCache(cache, eventKey);
}
}
Overload 2:
public static ISignalRServerBuilder AddNCache(this ISignalRServerBuilder signalRBuilder, string cacheName, string eventKey, string userId, string password);
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
string cache, eventKey, userId, password;
cache = Configuration["NCacheConfiguration:CacheName"];
eventKey = Configuration["NCacheConfiguration:EventKey"];
// In case cache security is enabled, specify the security credentials
userId = Configuration["NCacheConfiguration:ConnectionOptions:UserCredentials:UserId"];
password = Configuration["NCacheConfiguration:ConnectionOptions:UserCredentials:Password"];
services.AddSignalR().AddNCache(cache, eventKey, userId, password);
}
}
Overload 3:
public static ISignalRServerBuilder AddNCache(this ISignalRServerBuilder signalrBuilder, Action<NCacheConfiguration> configure);
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<NCacheConfiguration>(Configuration.GetSection("NCacheConfiguration"));
services.AddSignalR().AddNCache(ncacheOptions =>
{
ncacheOptions.CacheName = Configuration["NCacheConfiguration:CacheName"];
ncacheOptions.EventKey = Configuration["NCacheConfiguration:EventKey"];
});
}
}
Configure Method
Here is how you can implement the Configure method for different overloads.
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseSignalR(config =>
{
config.MapHub<MessageHub>("/messages");
});
app.UseMvc();
}
Additional Resources
NCache provides a sample application for SignalR on GitHub.
See Also
.NET: Alachisoft.NCache.AspNet.SignalR namespace