Using NCache Extension for SignalR Core
NCache extends the ISignalRServerBuilder interface with its AddNCache
method which requires just the cache name and event key for the item added. If cache security is enabled, user credentials are also required. This acts as the registration point for the clients against the ASP.NET Core SignalR implementation.
Note
This feature is also available in NCache Professional.
The AddNCache
takes in 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 |
connection options |
ConnectionOptions object allows to set the 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 for Using NCache Extension for SignalR Core
- Install the following NuGet packages in your application based on your NCache edition:
- Enterprise: AspNet.SignalR.NCache
- Professional: AspNet.SignalR.NCache.Professional
- OpenSource: AspNet.SignalR.NCache.OpenSource
- To utilize the extension, include the following namespaces in your application in Startup.cs:
- Alachisoft.NCache.AspNet.SignalR
Microsoft.AspNet.SignalR
- The cache must be running.
- Make sure to use the version 1.1.0 of ASP.NET SignalR Core.
- For API details, refer to: AddNCache.
- Make sure that the data being added is serializable.
- To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
Modify AppSettings.json
Add the following configurations in your appsettings.json file. In particular, you can provide a ConnectionOptions
object to set different properties to be used at the time of client connection with 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 parameters provided in the ConnectionOptions
object, refer to CacheConnectionOptions Properties.
"NCacheConfiguration": {
"CacheName": "demoCache",
"EventKey": "chatApplication",
"ConnectionOptions": {
"ClientBindIP": "",
"AppName": "DemoAppName",
"LogLevel": "info",
"UserCredentials": {
"UserID": "john doe",
"Password": "1234"
},
"ServerList": [
{
"Name": "20.200.20.40", "Port": 9800
}
]
}
}
Register Clients to use NCache
Register an instance of 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.