Alachisoft / NCache

NCache: Highly Scalable Distributed Cache for .NET
http://www.alachisoft.com
Apache License 2.0
647 stars 123 forks source link

NCacheDistributedCache constructor is requesting NCacheSessionConfiguration but only NCacheConfiguration is setup. #38

Closed DamienLaw closed 5 years ago

DamienLaw commented 5 years ago

In order to use NCache as distributed cache in ASP.NET Core, one would have the following code in ConfigureService() of Startup.cs.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddNCacheDistributedCache(Configuration.GetSection("NCache"));
    }
}

Looking at the source codes of AddNCacheDistributedCache(), it's setting up the configuration NCacheConfiguration. image

However, the constructor of NCacheDistributedCache is requesting for NCacheSessionConfiguration, and not NCacheConfiguration. image

Since the configuration NCacheSessionConfiguration was never setup, I always receive the exception A valid Cache ID has not been specified. Is there something that I'm missing?

Brad-NCache commented 5 years ago

Hi Damien,

Thank you for pointing this out to us and helping us narrow down the cause. I have reproduced the issue and have confirmed with Engineering that it is a bug. You are correct in that the constructor of NCacheDistributedCache should have IOptions<NCacheConfiguration> instead of IOptions<NCacheSessionConfiguration> as parameter.

For your work, there are two possible workarounds:

public void ConfigureServices(IServiceCollection services)     
{
            services.AddMvc();

           // The following code adds the NCache IDistributedCache provider
            **services.AddNCacheDistributedCache(Configuration.GetSection("NCacheSettings"));**

            // Alternate extension method
            //services.AddNCacheDistributedCache(configuration =>
            //{
            //    configuration.CacheName = "myPartitionedCache";
            //});

           // The following code adds the NCache ASP.NET Core session provider
            **services.AddNCacheSession(Configuration.GetSection("NCacheSettings"));**

            // Alternate extension method 
            //services.AddNCacheSession(configuration =>
            //{
            //    configuration.CacheName = "myPartitionedCache"; 
            //});
        }  

After this, in the Configure method, also within the Startup.cs file, you can enable NCache functionality into the request pipeline using the UseNCacheSession extension method as shown in the following example:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
      // Some code

       app.UseStaticFiles();

       **app.UseNCacheSession();**

       app.UseMvc(routes =>
           {
                // routes
            });
}
public NCacheDistributedCache(IOptions<**NCacheConfiguration**> options)
{
            // Keep code the same here
}

You can then use the generated DLLs in your project.

By using either of the two workarounds mentioned above, you can then provide the NCache IDistributedCache implementation through dependency injection into any constructor in the app such as a controller class constructor.

The following link contains more information on the different ways NCache can support ASP.NET Core:

http://www.alachisoft.com/resources/docs/ncache/prog-guide/session-storage-aspnet-core.html

I hope this resolves your issues.

DamienLaw commented 5 years ago

This has been resolved in the latest version 5.0. Thank you.