Pergunta Loading Assemblies

  • Saturday, August 18, 2012 6:29 AM
     
     

    I need to manually load some unresolved dlls that are embedded in my code. I am having some issues with that and now reduced the code to the following very simple plugin shown below. When this plugin executes I get the exception:

    Unexpected exception from plug-in (Execute): test.MyPlugin: System.TypeInitializationException: The type initializer for 'test.MyTest' threw an exception. The line that fails is AppDomain... If I replace that line with AppDomain a = AppDomain.CurrentDomain; then there is no issue. So somehow I cannot add a handler. Unfortunately, I can't see any inner exceptions or step through as the plugin runs on a hosted server to which I have no access. Any ideas are appreciated.

    using System;
    using Microsoft.Xrm.Sdk;

    namespace test {
        public class MyPlugin : IPlugin {
            public void Execute(IServiceProvider serviceProvider) {
                MyTest log = new MyTest();
            }
        }
        public class MyTest {
            static MyTest() {
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            }

            static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
                return null;
            }
        }
    }

All Replies

  • Saturday, August 18, 2012 8:42 AM
    Answerer
     
     

    Hi,

    I don't think it is wise to add event handlers to the Plugin Pipeline AppDomain - you would need to ensure that the event is un-registered when the plugin is re-registered etc. and at best this would be an unsupported customisation.

    Options are:

    1) Use ILMerge to combine all the dependancies into a single assembly

    2) Register the dependant assemblies in the gac (if you are using an OnPrem deployment)

    3) Combine all the dependancies into a single code base so that there is a single compiled assembly.

    Option 3 would be the best option, especially if you are using CRM Online.

    hth,

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

  • Saturday, August 18, 2012 9:13 PM
     
     

    Thanks for the response. Since I use CRM Online 2) is out. Regarding 3) are you suggesting that I put all source code into one big project and compile it into one dll? The issue with this is that I don't have access to the source of some dlls. If I misunderstood then can you please clarify some more?

    I did actually use 1) but ran into issues when adding Enterprise Library to the mix. After adding that library I get to different errors (sometimes one and sometimes the other). I guess some dlls are loaded in the background and it just matters what is done first. The errors are:

    [plugin name]: System.TypeLoadException: Inheritance security rules violated by type Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.CommonWmiInstaller. Derived types must either match the security accessibility of the base type or be less accessible.

    [plugin name]: System.ArgumentException: The specified type Xrm.AsyncOperation is not a known entity name.

    The first one is related to to Enterprise Libraries. The second one was never an issue until I added Enterprise Library to the ILMerge. For some reason suddenly AsyncOperation is not visible anymore although I ILMerge it and although I have the [assembly: Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute()].

    If I can continue to use ILMerge that would be preferred.

  • Sunday, August 19, 2012 8:47 AM
    Answerer
     
     

    Hi,

    The issues you are encountering are not un-common with ILMerge unfortunately - the process interferes with type dependancies and refelection. The enterprise library is open-source - so you can compile that in to your plugin assembly.

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"