01-18-2010 03:44 PM
The solution that worked for me with your test case is something similar to the following:
static System.Reflection.Assembly AssemblyResolveEventHandler(System.Object sender, System.ResolveEventArgs args)
{
if (args.Name.StartsWith("MyAssemblyName"))
return System.Reflection.Assembly.GetExecutingAssembly();
else
return null;
}
You need to register this event handler once as follows before executing your other code:
static bool assemblyResolveRegistered = false;
if (!assemblyResolveRegistered)
{
AppDomain.CurrentDomain.AssemblyResolve += new System.ResolveEventHandler(AssemblyResolveEventHandler);
assemblyResolveRegistered = true
}
Once I had done this, I was successfully able to use both OpenExeConfiguration and the Sections collection. Basically what assemblyResolve does is when the .NET framework tries to load your assembly in the Load context (and normally fails because it can't find it in the GAC or base application directory, and gives you the error you were seeing), your AssemblyResolve callback instead gets called and returns back a reference to your already loaded assembly. This effectively puts your assembly in the Load context too and allows the .NET framework to match up the type it was looking for in your assembly to the serialized data. Also for data that was serialized with a different version of your assembly, the AssemblyResolve handler can redirect the .NET code to use the currently loaded, newer version (there might be some issues if the data type has changed drastically from when it was serialized though). You can make it more robust and safer by checking more explicitly the args.Name property of the AssemblyResolve event handler to more exactly only redirect references that you want it to, but the above is just an example. But again, I don't know the .NET Configuration API that well so it's possible this is not a good idea for some reason that I am not aware of.
-Doug
09-10-2010 07:37 AM
Hi Doug,
Still reopened this old issue
I am not able any more to get this stuff running
OpenExeConfiguration is working but mySel = (MySelection)config.Sections["MySelection"] is crashing !
Could you please provied a small example that uses a config file.
Regards
Juergen
09-11-2010 01:16 PM
What workaround are you using to get the assembly into the Load context? Are you using AssemblyResolve? Or are you putting assemblies in the application base path or GAC? The workaround that I recommend was using AssemblyResolve to cause the assemblies loaded by path (i.e. in the LoadFrom context) to be accessible in the Load context.
-Doug
09-15-2010 02:25 PM
Hi Doug,
i would like to try AssemlyResolve. But i dont know who to register the event handler.
I do i need a second assembly at all ?
BTW I did a workaround, just to save time. The biggest advantage of using config.GetSection("xxx")
is that i can map the configfile data into a class based architecture which is very suitable
especially if you have some collections classes.
Now i open the hole conifg file as a xml domdocument and put its data to classes architecture.
But i think it could be useful for me (for future projects) and other members
to provide a small example that shows how it should be done.
Have a nice day
Juergen
09-16-2010 02:44 PM
The assembly resolve event handler just needs to be registered before you execute the code that does the serialization or deserialization. To register it, you need to do something like the following:
// Call this from your sequence before anything else. It only needs to be done once. That's why a static bool variable is used to check to see if it's already been done.
static void RegisterAssemblyResolveEventHandler()
{
static bool assemblyResolveRegistered = false;
if (!assemblyResolveRegistered)
{
AppDomain.CurrentDomain.AssemblyResolve += new System.ResolveEventHandler(AssemblyResolveEventHandler);
assemblyResolveRegistered = true
}
}
// This just needs to be in the same assembly as the RegisterAssemblyResolveEventHandler() method. This is the event handler that gets registered.
static System.Reflection.Assembly AssemblyResolveEventHandler(System.Object sender, System.ResolveEventArgs args)
{
if (args.Name.StartsWith("MyAssemblyName,")) // Change this to the name of your assembly
return System.Reflection.Assembly.GetExecutingAssembly();
else
return null;
}
-Doug
09-21-2010 07:56 AM
09-20-2012 03:28 AM
Look like this stille helps with .Net 3.5 (Visual Studio 2010) and TestStand 2010. Thangs
07-29-2020 06:51 AM
10 years later, still relevant, worked like a charm!
Thank you very much 🙂