none
CLR Hosting: Voodoo required. RRS feed

  • Question

  • Hello all,

    i've got some questions regarding CLR hosting:

    1. Is it possible to host multiple CLR's from within one single managed application? Sort of AppDomains on steroids?
    2. If so, what steps are required to create a completely indestructible application that is able to compile and execute code on the fly?

    There are lots of pieces of information about this topic, but none of them holds the holy grail on how to achieve numer two functionality like is implemented in ASP.NET and SQL Server CLR hosting.

    Many, many thanks in advance,

    Peter Vrenken - MCPD: Enterprise Applications Developer - Sigmax Mobile Solutions
    Saturday, February 27, 2010 11:44 PM

Answers

All replies

  • 1. Since 4.0 it is possible to host multiple CLRs in the same process. However versions 1.0, 1.1 and 2.0 (also included in .NET Framework 3.0 and 3.5) cannot be in the same proces. So currently you can load only 4.0 and one of the older versions. In the future when next major CLR release will be released, you will be able to host 3 CLR versions in the same process (4.0, one older version and the future version). The new 4.0 hosting APIs are here: http://msdn.microsoft.com/en-us/library/dd380850(VS.100).aspx
    Also check out these links:
        http://msdn.microsoft.com/en-us/magazine/ee819091.aspx
        http://channel9.msdn.com/shows/Going+Deep/CLR-4-Side-by-Side-In-Process-What-How-Why/
        http://blogs.msdn.com/jasonz/archive/2007/05/10/side-by-side-in-process-clrs-start-with-silverlight.aspx

    2. What do you mean by completly indestructible? For compilation and execution on the fly you should be able to use Reflection.Emit.
    ASP.NET and SQL Server both use just the published hosting APIs, so whatever it is you like on them, you should be able to achieve too. They use more advanced hosting APIs e.g. for controlling locking, so they can avoid deadlocks (SQL), or control amount of consumed memory by CLR application.

    -Karel

    Sunday, February 28, 2010 12:57 AM
    Moderator
  • Hello Karel,

    First of al thanks for the response.

    1. Do you mean that there can only be one CLR within a process, or that no different versions can exist in the same project?
    2. With indestructible i mean a managed application that can compile and execute managed code, and that this code cannot terminate the main application (for example by throwing an unhandled exception).
    If i am correct, ASP.NET and SQL Server are both native applications that use CLR hosting to execute custom managed code. I am in search for a managed application that allows for managed custom code execution.

    Maybe i should introduce my problem with a bit more detail.

    I am developing a research application in which the user can write pieces of C# source code. All of these sources are then compiled into an application and executed. As of now the execution is done using secondary appdomains.This is required because the the compiled application has to be executed in parrallel using a number of variations. Because of this the appdomains are a must.

    Thus:

    - I am developing the research application (as a managed application).
    - Users can create experiments using C# source code (and other goodies), which in turn are compiled into experiment assemblies.
    - These experiment assemblies are mutated and executed next to each other into secondary appdomains.

    I've created the research application without any great problems until i tried to write some experiment source code that uses threading. When the experiment creates a unhandled exception on a thread it created, the whole research application crashes also. Until now i do not know of a way to maintain the crash within the experiment itselve. Even using the CLR hosting components from within a managed application (by adding a reference to the mscoree library) does not seem to work correctly.
    I guess the problem goes for all threading, being Threads, ThreadPools and all Begin/EndInvoke.

    Any thoughts?
    Peter Vrenken - MCPD: Enterprise Applications Developer - Sigmax Mobile Solutions
    Sunday, February 28, 2010 1:27 PM
  • 1. There can be multiple CLRs in the process. Read 'Figure 2' in this link: http://msdn.microsoft.com/en-us/magazine/ee819091.aspx. However there cannot be 2 instances of the same CLR version in the process.

    2. Start each 'secondary' AppDomain with your code which will either do a try-catch around call into the 'experiment assembly' main method, or will set up UnhandledException handler for that AppDomain.
    In the handler, you can just report the exception to the user (or to the main AppDomain) and exit AppDomain execution.

    Hosting APIs go much further than this. They allow the host to control locking (and therefore break deadlocks) or limit amount of used resources like memory. I don't think that you need any of this functionality for your project.
    AppDomains seem to be the right isolation solution for your need - you just need to handle unhandled exceptions.

    -Karel
    Sunday, February 28, 2010 7:56 PM
    Moderator
  • Have you used AppDomain unloading in a UnhandledException method in one of you're own applications?
     
    If so, please tell me how to use it correctly. I would really like to, but as far as i know and see in my application the UnhandledException event is a complete no-go. It's purpose is solely to let sanitize functions execute, but not to let developers decide if the application keeps living or dies.

    As far as i understand, if an unhandled exception occurs in any of the secondary appdomains, first the unhandled exception in the faulty AppDomain is raised. Right after that the unhandled exception event in the default AppDomain also gets called, and the application terminates no matter what happens.

    This is by design of the 2.0 Framework, but frustrated to allot of developers because we cannot use appdomains to execute dynamic code.

    Snif... :-(

    Peter Vrenken - MCPD: Enterprise Applications Developer - Sigmax Mobile Solutions
    Sunday, February 28, 2010 10:26 PM
  • You're right UnhandledExceptionHandler doesn't really seem to fully work for your scenario.

    BTW here's a related article: http://www.codeproject.com/KB/dotnet/unhandledexceptions.aspx
    And here's explanation that it won't work on additional threads in CLR 2.0+: http://msdn.microsoft.com/en-us/library/ms228965.aspx
    However there's link to ICLRPolicyManager and ICLRPolicyManager::SetUnhandledExceptionPolicy at the end of that article. It can probably be used as this post explains (OK, you already found it ... still the solution seems to be applicable to your case, you will just not get the notifications).

    Another option is to start all your threads executing dynamic code with try-catch wrapper, or compile your dynamic code with try-catch around your 'Main' method. Although that will not help if the dynamic code creates new threads itself.
    Also I don't know how 'clean' such solution is.

    Maybe someone else will have better idea.

    -Karel

    Monday, March 1, 2010 1:28 AM
    Moderator