Alachisoft / NCache

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

Error on InitializeCache, problems getting started #49

Open palmsey opened 5 years ago

palmsey commented 5 years ago

When calling NCache.InitializeCache("myCacheName") in my ASP.NET project I'm getting this exception:

An error occurred while reading client.ncconf. The type initializer for 'Alachisoft.NCache.Common.AppUtil' threw an exception.

Stack trace:

Alachisoft.NCache.Web.RemoteClient.Config.ClientConfiguration.LoadConfiguration() +4707
   Alachisoft.NCache.Web.Communication.Broker.TryNextServer() +1568
   Alachisoft.NCache.Web.Communication.Broker.StartServices(String cacheId, String server, Int32 port) +2212
   Alachisoft.NCache.Web.Caching.RemoteCache..ctor(String cacheId, Cache parent, CacheInitParams initParams, PerfStatsCollector2 perfStatsCol) +476
   Alachisoft.NCache.Web.Caching.NCache.InitializeCacheInternal(String cacheId, CacheInitParams initParams) +2484
   Alachisoft.NCache.Web.Caching.NCache.InitializeCache(String cacheId, CacheInitParams initParams) +63
   Alachisoft.NCache.Web.Caching.NCache.InitializeCache(String cacheId) +85

I've installed NCache 4.6.3 and the Open Source SDK NuGet package. The NCache service is running. listcaches shows I have a local cache running. I've tried editing permissions on client.ncconf to allow anyone to read/modify, both on the config file at the root of my web project and in the NCache installation folder. I've tried using the inproc cache "myCache" that is mentioned in the readme.txt that comes with the NuGet package too, but I keep getting the same error.

What am I missing?

palmsey commented 5 years ago

I was able to resolve this by adding an "InstallDir" app setting. It looks like NCache is supposed to try to read this from System.Environment.CurrentDirectory, which returns the IIS Express path instead of the project path when running in IIS Express. Using System.AppDomain.CurrentDomain.BaseDirectory would be more accurate.

I couldn't find any mention of the "InstallDir" setting in the documentation.

Kal-Alachisoft commented 5 years ago

Hi @palmsey

NCache always looks in the application bin folder first for the "Client.ncconf" file and if it does not find it there then it will go search for it in the NCache installation directory at NCache/Config path in case of installation. The issue you are referring to is specific to web apps deployed in IIS server that always checks for config files from the "insetsrv" folder by default

Here are some options you can try to resolve this in your environment,

Option 1:

You can use the CacheInitParams in your application when initializing the cache. This will take away any dependencies on client.ncconf file and you can control client side configurations programmatically. I am sharing more details on this below. CacheInitParams class allows a developer to configure the cache properties directly by code and NCache will then use the properties specified in code rather than checking the config file. A sample code is as follows:

CacheInitParams params = new CacheInitParams();
params.BindIP = "20.200.21.10"   
params.CommandRetries = 5;

You can specify all client side properties that can be found with the documentation of the CacheInitParams class.

http://www.alachisoft.com/resources/docs/ncache-4-9/dotnet-api-ref/Alachisoft.NCache.Web.Caching.CacheInitParams.html

Option 2:

Here are steps that you can follow to read client.ncconf from a custom path when using Alachisoft Nuget package.

  1. Go to the Windows Registry editor by typing "regedit" in the Windows run command.

  2. Expand HKEY_LOCAL_MACHINE in the left pane of the editor and then right-click on SOFTWARE and add new key titled "Alachisoft".

  3. Right-click on "Alachisoft" that you have just created and add new key titled "NCache".

  4. Right-click on "NCache", click on the "New" tab in the drop down list and select "String Value".

  5. Right-click on the new item you see in the right-hand pane and rename it "InstallDir".

  6. Now right-click on "InstallDir" item and select "Modify".

  7. In the resulting pop-up dialog box, write the custom path on your client machine where you will want your application to read client.ncconf file from. For our purposes, lets say the custom path is "D:\NCache"

  8. Assuming your custom path is "D:\NCache", go to D:, create a new folder NCache and within the NCache folder, create a new folder titled "config". Add your client.ncconf file in this folder

  9. Registry looks like this: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Alachisoft\NCache\InstallDir.

  10. Your path to the client.ncconf file will look like this in the Windows Explorer address bar: D:\NCache\config.

  11. Now, if you restart your server, the web app should be able to read from client.ncconf as expected.

I hope this helps.

palmsey commented 5 years ago

@Kal-Alachisoft thanks. CacheInitParams may work for us, I will try that out. I would rather avoid modifying the registry on our servers and development machines.

palmsey commented 5 years ago

@Kal-Alachisoft I tried using CacheInitParams and removed my "InstallDir" app setting. I'm using the params in the InitializeCache call, like this:

var cache = NCache.InitializeCache(Configuration.NCacheName, cacheParams);

When I run the application, I get an error on that line:

An error occurred while reading client.ncconf. The type initializer for 'Alachisoft.NCache.Common.AppUtil' threw an exception.

It seems like even when you specify CacheInitParams NCache is looking for the config file, and will throw an error if it can't find it.

Kal-Alachisoft commented 4 years ago

Hi @palmsey , the CacheInitParams removes that dependency completely and should not be referring to the config file at all. Please confirm are you still having this issue?

andrevoltolini commented 4 years ago

Hi @Kal-Alachisoft, i'm using CacheInitParams in a ASP.NET MVC 5 project with sucess but in another project with WCF host service WEB i have the problem.

Mark-NCache commented 4 years ago

Hi @palmsey, can you please share with us a working sample with which the issue is reproduced?.

bluejaye commented 4 years ago

@palmsey I met exactly the same issue. Please check your bin folder if "ncregistry.dll" is there.

copy the file there if it isn't there.