Repository: MarimerLLC/csla

Issue #2095: Add support for unloading assemblies from memory (re: caching)
Created: 2021-02-08T20:02:41Z
There is no support for the concept of unloading an assembly and its types from memory during the lifetime of a process or AppDomain. It appears that in .NET Core it is now possible to unload an assembly and reload it, but when reloaded, that assembly no longer maps to the same types as the previous load.
This seems to imply that all locations in CSLA where type information is cached need to be updated to support a scenario where the "same type" can be loaded multiple times during the lifetime of an app.
At a high level, this seems to require:

Identify all locations where type info is cached
Allow flushing or removing info from the cache (all or partial?), when an assembly is unloaded
Allow reloading of new info into the cache (which should be automatic if step 2 removed the old info)

Some areas where type info is almost certainly cached include:

Property registration (types in Csla.Core.FieldManager and PropertyManager)
Numerous helpers and classes in Csla.Reflection
Numerous configured types in Csla.ApplicationContext and related Csla.Configuration
Rules and rule sets in various base classes and in Csla.Rules
Maybe some types related to the factory model data portal in Csla.Server

There may be other areas where caching is implemented. I suspect searching for the lock keyword will reveal most or all of them, given that caching type info needs to be threadsafe.
Original post:
DataPortal operation on repeatedly loaded assembly (using AssemblyLoadContext) causing cast exception - Can not cast [A]MyClass from MyAssembly.dll to [B]MyClass from MyAssembly.dll
Probably i'm doing something wrong, but will try to explain :\
Have main application with installed CSLA library. On request - loading dynamic assembly using AssemblyLoadContext and calling DataPortal.Fetch method from class within this dynamically loaded assembly.
On first call everything works fine, but after some time of inactivity flushing this not used anymore dynamic assembly, calling AssemblyLoadContext.Unload(), GC.Collect() and GC.WaitForPendingFinalizers().
After period of time trying to access same dynamic assembly via same scenario: loading in AssemblyLoadContext and calling DataPortal.Fetch, but this time getting error:
DataPortal.Create failed ([A]MyClass cannot be cast to [B]MyClass. Type A originates from 'MyAssembly.dll, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' in a byte array. Type B originates from 'MyAssembly.dll, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' in a byte array.)

That is call stack:
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.ServiceProviderMethodCaller.CallMethodTryAsync(Object obj, ServiceProviderMethodInfo method, Object[] parameters)
   at Csla.Reflection.ServiceProviderMethodCaller.CallMethodTryAsync(Object obj, ServiceProviderMethodInfo method, Object[] parameters)
   at Csla.Reflection.LateBoundObject.CallMethodTryAsyncDI[T](Boolean isSync, Object[] parameters)
   at Csla.Server.DataPortalTarget.InvokeOperationAsync[T](Object criteria, Boolean isSync)
   at Csla.Server.DataPortalTarget.FetchAsync(Object criteria, Boolean isSync)
   at Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context, Boolean isSync)
   at Csla.DataPortal'1.Fetch(Type objectType, Object criteria)
   at Csla.DataPortal'1.Fetch(Object[] criteria)
   at Csla.DataPortal.Fetch[T](Object[] criteria)
   at MyClass.Fetch(MyFetchCriteria criteria)

Any  suggestion what am i doing wrong? As i understand, static fetch method from MyClass was cached somewhere in CSLA and CSLA trying to reuse it.
Could anyone guide me a little?)
Version and Platform
CSLA version: 5.2.000
OS: Linux
Platform: ASP.NET Core 3.1

Duplicate issue #2356: Add support for unloading assemblies from memory (re: caching)
Created: 2021-07-05T19:36:19Z
Implement changes from #2095 into CSLA 6
There is no support for the concept of unloading an assembly and its types from memory during the lifetime of a process or AppDomain. It appears that in .NET Core it is now possible to unload an assembly and reload it, but when reloaded, that assembly no longer maps to the same types as the previous load.
This seems to imply that all locations in CSLA where type information is cached need to be updated to support a scenario where the "same type" can be loaded multiple times during the lifetime of an app.

Closing comment (2021-08-03T19:39:34Z): I'm closing this as it is a duplicate of #2095