Hi! It's my first discussion on this forum.
First I have a exe like this below (the object can invoke by console)
public class DynamicLoad
{
public Type AssemblyType = null;
public Assembly TestAssembly1 = null;
public DynamicLoad ()
{
string load_path = "..\OuterAssembly1\OuterAssembly1.dll";
TestAssembly1 = Assembly.LoadFrom(load_path);
AssemblyType = TestAssembly1.GetType
("OuterAssembly1.ThisAssembly");
}
public void InvokeOuterAssembly()
{
OuterAdapter target=(OuterAdapter)
AssemblyType.Assembly.CreateInstance(AssemblyType. FullName);
string response = target.GetConfigValue();
}
}
As you can see it has a "OuterAdapter" (in a seperate dll that referece by exe and "OuterAssembly1.dll" ) that is a base class and a bridge between exe and "OuterAssembly1.ThisAssembly"
"OuterAssembly1.ThisAssembly" is below:
public class ThisAssembly : OuterAdapter
{
private CustomSection ConfigObject;
public override string GetConfigValue()
{
string response = "";
try
{
string AssemblyPath = this.GetType().Assembly.Location;
Configuration ThisConfig =
(ConfigurationManager.OpenExeConfiguration(Assembl yPath));
ConfigObject = new CustomSection();
ConfigObject=(CustomSection)(ThisConfig.GetSection
("TestSection"));
response += ConfigObject.Settings["testkey"].Value + "\r\n";
}
catch (Exception ex)
{
response += ex.ToString() + "\r\n";
}
return response;
}
}
The "ThisAssembly" is under the namespace "OuterAssembly1" that is also assembly name. So I have a dll.config :
<configuration>
<configSections>
<section name="TestSection"
type="OuterAssembly1.CustomSection,OuterAssembly1"/>
</configSections>
<TestSection>
<add key="testkey" value="This is AssemblyOne"/>
</TestSection>
</configuration>
Of cource I have a "OuterAssembly1.CustomSection" as a config read handler
to get "testkey".
But Before i can read the dll.config, I got the Exception from ~~~~~
~~~ ConfigObject=(CustomSection)(ThisConfig.GetSection ("TestSection"));
and it's not exception from my read handler which i test else where ok, however
it seems Configuration object can resolve the "OuterAssembly1" to get my handler for configsection.
The Exception is :
System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for TestSection: Could not load file or assembly 'OuterAssembly1' or one of its dependencies. The system cannot find the file specified. (D:\TestAssemblyLoading\AssemblyLoadingTest\OuterA ssembly1\OuterAssembly1.dll.config line 4) ---> System.IO.FileNotFoundException: Could not load file or assembly 'OuterAssembly1' or one of its dependencies. The system cannot find the file specified.
File name: 'OuterAssembly1'
at System.Configuration.TypeUtil.GetTypeWithReflectio nPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
at System.Configuration.MgmtConfigurationRecord.Creat eSectionFactory(FactoryRecord factoryRecord)
at System.Configuration.BaseConfigurationRecord.FindA ndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
--- End of inner exception stack trace ---
at System.Configuration.BaseConfigurationRecord.FindA ndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
at System.Configuration.BaseConfigurationRecord.GetSe ctionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSe ction(String configKey, Boolean getLkg, Boolean checkPermission)
at System.Configuration.Configuration.GetSection(Stri ng sectionName)
at OuterAssembly1.ThisAssembly.GetConfigValue() in D:\TestAssemblyLoading\AssemblyLoadingTest\OuterAs sembly1\ThisAssembly.cs:line 36
OuterAssembly1 doesn't have any other reference dll except OuterAdapter
As you can see I am imitating a Web ApplicationServer which can load extention dynamiclly and use their own config at runtime then the server.exe that can complete this mission only need to know the assembly path and a name of class which implement an adapter that predefined and know by exe , both extention.
Does any one know that somthing interfere with my ought?
By the way the OuterAssembly1 and its dll.config is outside the exe directory.
And even I add OuterAdapter to GAC it still doesn't work.
Thanks!