CacheFactory.GetCache() throws an exception

I get the following exception when I call CacheFactory.GetCache()
System.TypeInitializationException was caught
  Message=The type initializer for '<Module>' threw an exception.
       at Soss.Client.CacheFactory.GetCache()
       at ScaleoutCacheProvider.ClearAll(String bankUri, Nullable`1 userIdentifier) in c:\src\FORB\Common\ScaleoutCacheProvider.cs:line 240
  InnerException: <CrtImplementationDetails>.ModuleLoadException
       Message=The C++ module failed to load while attempting to initialize the default appdomain.

            at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
            at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
            at .cctor()
       InnerException: System.InvalidOperationException
            Message=Cannot serialize a Session while connected
              Server stack trace:
                 at NHibernate.Impl.SessionImpl.System.Runtime.Serialization.ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
                 at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
                 at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
                 at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
                 at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
                 at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeMessageParts(ArrayList argsToSerialize)
                 at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage..ctor(IMethodCallMessage mcm)
                 at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage.SmuggleIfPossible(IMessage msg)
                 at System.Runtime.Remoting.Channels.CrossAppDomainSink.SyncProcessMessage(IMessage reqMsg)
              Exception rethrown at [0]:
                 at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
                 at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
                 at System.AppDomain.get_Id()
                 at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
                 at <CrtImplementationDetails>.DefaultDomain.Initialize()
                 at <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* )
                 at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
                 at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
The actual inner exception is
System.InvalidOperationException: Cannot serialize a Session while connected
I am not sure why I am getting an nhibernate error. I am using nhibernate in this project, although I am not sure why it causes scaleout to not load.

I tried the sample project, and that works fine. I tried adding nhibernate to the sample project, and it still works.


This is an unusual exception--we've have a number of people using SOSS in conjunction with NHibernate for years (even integrating SOSS as a second-level cache provider), but this hasn't been reported before.

Without being able to reproduce this we'll need to look for other people reporting similar NHibernate problems. Here are the first two things I'd look at:

1. If you're using JSON.NET with NH then I'd suggest reviewing this StackOverflow question. ...your "Cannot serialize a Session while connected" exception can occur when serializing an entity to JSON, so you'll want to plug in the contract resolver that's described in that discussion.

2. There's also a report of a similar conflict between NHibernate and a managed C++ library here. This is relevant because one of the ScaleOut assemblies that your project references (soss_svcdotnet.dll) is also mixed-mode, so it may be a similar issue since you're also seeing a ModuleLoadException.

...while a good solution wasn't provided in that google discussion, we'd recommend verifying that the correct version of soss_svcdotnet.dll is referenced so as to avoid any loading problems (that is, make sure that you're loading the soss_svcdotnet build that best matches your .NET runtime). The first table in our API docs (under "Configuring and Deploying your .NET Project") describes which assemblies you should be referencing.

If you continue to have problems then please let us know and we'll work with you to resolve this exception.
I have identified the root cause of the issue, It's failing because I am currently inside an nHibernate session when I attempt to get the first instance of the cache. For the purposes of my evaluation I can work around this, not sure I like the workaround of having to instantiate the factory during application startup everywhere though.