How can I prevent CompileAssemblyFromSource from leaking memory?

I think I have a working solution. Thanks to everyone for pointing me in the right direction (I hope). Assemblies can’t be unloaded directly, but AppDomains can. I created a helper library that gets loaded in a new AppDomain and is able to compile a new assembly from code. Here’s what the class in that … Read more

How to avoid SerializationException: Type is not resolved for member XXX when testing a component that uses the LogicalCallContext

I think this is good explaination why you get this error. Is it possible to use the logical call context within a unit test in VS 2010? I searched what is good option this. I never find any answer except MarshalByRefObject. So why you should inherit your object with it . It is good explanation … Read more

Static Fields in AppDomain

It looks like you are loading a type from another appDomain into the current appDomain. Thus the code that calls the static methods are calling from the current appDomain. I’m unaware of any other way to call a static method in another domain without creating an instance of an object in another domain, and having … Read more

Loading DLLs into a separate AppDomain

More specifically AppDomain domain = AppDomain.CreateDomain(“New domain name”); //Do other things to the domain like set the security policy string pathToDll = @”C:\myDll.dll”; //Full path to dll you want to load Type t = typeof(TypeIWantToLoad); TypeIWantToLoad myObject = (TypeIWantToLoad)domain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName); If all that goes properly (no exceptions thrown) you now have an instance of TypeIWantToLoad … Read more

How best to communicate between AppDomains?

I have had good success using WCF with a named pipes binding. Using named pipes creates no network traffic and uses binary encoding, so it should be pretty fast without sacrificing the ability to distribute in future scaling scenarios. EDIT: Refer here for more detailed information including a link to an implementation example.

How to load a .NET assembly for reflection operations and subsequently unload it?

From the MSDN documentation of System.Reflection.Assembly.ReflectionOnlyLoad (String) : The reflection-only context is no different from other contexts. Assemblies that are loaded into the context can be unloaded only by unloading the application domain. So, I am afraid the only way to unload an assembly is unloading the application domain. To create a new AppDomain and … Read more

In .NET 4.0, how do I ‘sandbox’ an in-memory assembly and execute a method?

OK, first things first: there’s no actual way to use the CSharpCodeProvider to do dynamic compilation of C# source entirely in memory. There are methods that seem to support that functionality, but since the C# compiler is a native executable that cannot run in-process, the source string is saved to a temporary file, the compiler … Read more

“Object has been disconnected or does not exist at the server” exception

That is probably because the local garbage collector at the server side collects the object. You can prevent that by renewing the leasing. You can read more about that in these articles: Managing the Lifetime of Remote .NET Objects with Leasing and Sponsorship CLR Inside Out: Managing Object Lifetime Update: Unfortunately, the MSDN Magazine issues … Read more

Running NUnit through Resharper 8 tests fail when crossing between projects due to AppDomain

The Workaround: Have you tried in Visual Studio going to ReSharper -> Options -> Tools -> Unit Testing Change the setting “Run up to 1 assemblies in parallel” to a higher number. I tried one for each test project. Max is number of cores, I think. Counterintuitive I know, but it worked for me and … Read more