Native Client .NET Application Error: "Cannot pass a GCHandle across AppDomains"
search cancel

Native Client .NET Application Error: "Cannot pass a GCHandle across AppDomains"

book

Article ID: 294212

calendar_today

Updated On:

Products

VMware Tanzu Gemfire

Issue/Introduction

Symptoms:
This article provides a solution for when the Native Client .NET application fails with error "Cannot pass a GCHandle across AppDomains" on Windows environment. 

In the case of creating multiple AppDomains in the single Native Client .NET application process, it fails with the log message shown below. This error tends to happen on IIS as the ASP.NET application creates individual AppDomains automatically for each deployed Web Application even if the actual application developer does not intend to create AppDomains explicitly.

Client Side Log Message:

gemfire::Exception: GFCLI_EXCEPTION:System.ArgumentException: Cannot pass a GCHandle across AppDomains.

 

Environment


Cause

The Native Client cache instance remains a singleton in the process even if the target .NET application process has multiple AppDomains. Because of this, you can run into conflicts with the different AppDomains. In other words, Native Client module tries to execute each single task for all the AppDomains in the process, although, they should be executed only on the specific AppDomain which calls the Native Client API.

Resolution

Set appdomain-enabled=true via the property file or API like the following:

Property file (gfcpp.properties by default):

appdomain-enabled=true
API (C#):

Properties<string, string> prp = Properties<string, string>.Create<string, string>();
prp.Insert("appdomain-enabled", "true");
  :
CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(prp);
  :
Cache cache = cacheFactory.Create();