Using NCache Extension for SignalR
NCache extends the IDependencyResolver DependencyResolver() method with its UseNCache()
method which requires just
the cache name and event key for the item added. This acts as the registration
point for the clients against the SignalR implementation.
UseNCache()
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. Event key is specific to the application. Each client of the application will use the same event key while calling NCache's extension method. |
configuration |
NCacheScaleoutConfiguration |
An object of NCacheScaleoutConfiguration which extends the ScaleoutConfiguration class and takes in cacheName and eventKey as members. |
To utilize SignalR in your application:
Install NuGet Package
Install the Alachisoft.NCache.SignalR NuGet package to your application by executing the following command in the Package Manager Console:
Install-Package Alachisoft.NCache.SignalR
Include Namespaces
To utilize the extension, include the following namespaces in your application in Startup.cs:
Alachisoft.NCache.SignalR
Microsoft.AspNet.SignalR
Modify Web.config
Define the cacheName and eventKey
in the <appSettings>
tag in Web.config
of your application:
<configuration>
<appSettings>
<add key="cache" value="myPartitionedCache"/>
<add key="eventKey" value="Chat"/>
</appSettings>
</configuration>
Register Clients to use NCache
Register an instance of the UseNCache()
method in Startup.cs
of your
application in either of the following overloads:
Overload 1:
public static IDependencyResolver UseNCache(this IDependencyResolver resolver, string cacheName, string eventKey);
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();
}
}
Overload 2:
public static IDependencyResolver UseNCache(this IDependencyResolver resolver, NCacheScaleoutConfiguration configuration);
public class Startup
{
public void Configuration(IAppBuilder app)
{
string cache, eventKey;
cache = ConfigurationManager.AppSettings["cache"];
eventKey = ConfigurationManager.AppSettings["eventKey"];
NCacheScaleoutConfiguration configuration = new NCacheScaleoutConfiguration(cache, eventKey);
GlobalHost.DependencyResolver.UseNCache(configuration); //using NCache SignalR
app.MapSignalR();
}
}