locked
Assemblies search path RRS feed

  • Question

  • User305165808 posted

    HI, I'm having some troubles with the assemblies search path of an asp.net (webform) application.

    I'm trying to make some sort of plugin system using MEF.

    The purpose is to be able the throw some "modules" in a plugin subfolder of the main asp.net app and then load them without having any references the host project. A module consits of a folder with some assemblies and webforms and other various resources like images, translations, ...

    The problem I'm having is that the assemblies associated with a certain plugin page aren't loaded and so I can't access them. For exemple if I try to access the page "/plugins/MyModule/Default.apsx". I'll get the following error : 

    Parser Error Message: Could not load file or assembly 'MyModule' or one of its dependencies. The system cannot find the file specified.

    I know that one solution for this problem would be to simply copy all the plugins assemblies in the main "bin" folder but I don't want to do this. I want all the files related to a certain plugins to reside in the same folder.

    I've tried using "probing privatePath" option of the web.config but with no luck. It doesn't seems to work if it's defined in a "sub web.config" file and I don't want to manually add all the plugins path to the main web.config file.

    So how could I tell asp.net to load the assemblies contained in the plugin subfolders ?

    Thanks for your help.

    Thursday, October 3, 2013 10:49 AM

Answers

All replies

  • User-166373564 posted

    Hi cpaulus

    Parser Error Message: Could not load file or assembly 'MyModule' or one of its dependencies. The system cannot find the file specified.

    From your description above, I think you need add Project Dependencies for your application. Right click your project->Project Dependencies, and choose plugin folder.

    Please check whether it helps you, if I misunderstand you or anything wrong, please let me know.

    Regards 

    Tuesday, October 8, 2013 11:13 PM
  • User305165808 posted

    Hi,

    Firstly, thanks for taking the time to answer me.

    I know that adding the dependency will solve the error. But I can't do that in my case. As I'm working with plugins, I don't know which one will be used at design time so I can't add the dependency.

    In the meantime I found a bit of a solution by using the class BuildManager in the PreApplicationStart methods, here is my code :

    string pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins");
    foreach (string f in Directory.GetDirectories(pluginPath))
    {
        string binPath = Path.Combine(f, "bin");
        if (Directory.Exists(binPath))
        {
            foreach (String file in Directory.GetFiles(binPath, "*.dll"))
            {
                Assembly a = Assembly.LoadFrom(file);
                BuildManager.AddReferencedAssembly(a);
            }
        }
    }

    The code above search all the plugin subfolder and check if they have a bin subfolder. If it's the case it'll reference all the assemblies contained in this folder at runtime with the help of the BuildManager.

    Now if I navigate to a plugin Url (eg : plugins/MyModule/Page.aspx) the previous error is gone, but the page is blank ! The page's source is empty; no html, nothing.

    I don't have any errors and when I try to debug, the method Page_Load of MyModule/Page.aspx.cs is never called.

    Any more ideas ?

    Wednesday, October 9, 2013 5:53 AM
  • User305165808 posted

    So after a bit more of search, I found this on stackoverflow : 

    http://stackoverflow.com/questions/1199237/buildmanager-resolving-page-inheritance

    And it solved my problem.

    The solution is to save a reference to each module assemblies when you load them. Then add an event handler to AppDomain.CurrentDomain.AssemblyResolve where you return the correct assembly.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 9, 2013 8:18 AM