Custom config section: Could not load file or assembly

前端 未结 7 2221
死守一世寂寞
死守一世寂寞 2020-12-15 05:50

I\'m having a very hard time trying to access a custom configuration section in my config file.

The config file is being read from a .dll that is loaded as a plug-in

相关标签:
7条回答
  • 2020-12-15 06:07

    Unfortunately, you will need to either have the ImportEPDMAddin assembly residing in the same folder as your executable, residing in the .Net framework folder related to the .Net framework you are using (i.e., C:\Windows\Microsoft.NET\Framework\v2.0.50727), or registered in the Global Assembly Cache.

    The only other option is, if you know the path to the assembly that contains the configuration handler's defining class, you can load it without a reference with something like this:

    //Class global
    private Assembly configurationDefiningAssembly;
    
    protected TConfig GetCustomConfig<TConfig>(string configDefiningAssemblyPath, 
        string configFilePath, string sectionName) where TConfig : ConfigurationSection
    {
        AppDomain.CurrentDomain.AssemblyResolve += new 
            ResolveEventHandler(ConfigResolveEventHandler);
        configurationDefiningAssembly = Assembly.LoadFrom(configDefiningAssemblyPath);
        var exeFileMap = new ExeConfigurationFileMap();
        exeFileMap.ExeConfigFilename = configFilePath;
        var customConfig = ConfigurationManager.OpenMappedExeConfiguration(exeFileMap, 
            ConfigurationUserLevel.None);
        var returnConfig = customConfig.GetSection(sectionName) as TConfig;
        AppDomain.CurrentDomain.AssemblyResolve -= ConfigResolveEventHandler;
        return returnConfig;
    }
    
    protected Assembly ConfigResolveEventHandler(object sender, ResolveEventArgs args)
    {
        return configurationDefiningAssembly;
    }
    

    Make sure you handle the AssemblyResolve event, as this will throw an exception without it.

    0 讨论(0)
  • 2020-12-15 06:09

    To expand on AJ's excellent answer, here is a custom class to assist with the overhead of registering and removing the global event.

    public sealed class AddinCustomConfigResolveHelper : IDisposable
    {
        public AddinCustomConfigResolveHelper(
            Assembly addinAssemblyContainingConfigSectionDefinition)
        {
            Contract.Assert(addinAssemblyContainingConfigSectionDefinition != null);
    
            this.AddinAssemblyContainingConfigSectionDefinition =
                addinAssemblyContainingConfigSectionDefinition;
    
            AppDomain.CurrentDomain.AssemblyResolve +=
                this.ConfigResolveEventHandler;
        }
    
        ~AddinCustomConfigResolveHelper()
        {
            this.Dispose(false);
        }
    
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        private void Dispose(bool isDisposing)
        {
            AppDomain.CurrentDomain.AssemblyResolve -= this.ConfigResolveEventHandler;
        }
    
        private Assembly AddinAssemblyContainingConfigSectionDefinition { get; set; }
    
        private Assembly ConfigResolveEventHandler(object sender, ResolveEventArgs args)
        {
            // often the name provided is partial...this will match full or partial naming
            if (this.AddinAssemblyContainingConfigSectionDefinition.FullName.Contains(args.Name))
            {
                return this.AddinAssemblyContainingConfigSectionDefinition;
            }
    
            return null;
        }
    }
    

    I would suggest creating an instance in a using statement, like so:

    // you'll need to populate these two variables
    var configuration = GetConfiguration();
    var assembly = GetAssemblyContainingConfig();
    
    using(new AddinCustomConfigResolveHelper(assembly))
    {
        return (MyConfigSection)configuration.GetSection("myConfigSection");
    }
    
    0 讨论(0)
  • 2020-12-15 06:10

    In your main applications config file, add the following (where plugins is the folder for your assembly to load from. You can use multiple paths semi-colon separated.

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath=".;.\Plugins"/>
        </assemblyBinding>
    </runtime>
    

    Taken from http://msdn.microsoft.com/en-us/library/823z9h8w%28v=vs.90%29.aspx

    0 讨论(0)
  • 2020-12-15 06:11

    Had to use the fully qualified type string of my module/plugin assembly, which is in a probing directory, so it could be located. Using EntityFramework as an example...

    Incorrect:

    type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework"
    

    Correct

    type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    
    0 讨论(0)
  • 2020-12-15 06:12

    Could you verify that the probing paths are setup correctly in your Host application's config file? It is possible that a needed reference is not being loaded in your current application domain.

    Assembly Binding ->Probing

    0 讨论(0)
  • 2020-12-15 06:19

    I tried AJ's answer, with rileywhite's supplement but I found that did not work for me.

    In my scenario, the custom ConfigurationSection class was already in the currently-executing assembly, and trying to load it causes a stack overflow. I also did not want to put it in GAC even though it did resolve the problem as reported by the OP.

    In end, I found this to work well enough for my purpose. Maybe others will find it useful:

    public class CustomConfigurationSection : ConfigurationSection {
      public CustomConfigurationSection()
      {
        var reader = XmlReader.Create(<path to my dll.config>);
        reader.ReadToDescendant("CustomConfigurationSection");
        base.DeserializeElement(reader,false);
      }
    
      // <rest of code>
    }
    
    0 讨论(0)
提交回复
热议问题