NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Loading app.config stuff from .net assembly

Solved!
Go to solution

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

0 Kudos
Message 11 of 18
(2,598 Views)

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

--Signature--
Sessions NI-Week 2017 2016
Feedback or kudos are welcome
0 Kudos
Message 12 of 18
(2,412 Views)

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

0 Kudos
Message 13 of 18
(2,401 Views)

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

 

 

 

--Signature--
Sessions NI-Week 2017 2016
Feedback or kudos are welcome
0 Kudos
Message 14 of 18
(2,376 Views)
Solution
Accepted by j_dodek

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

 

Message 15 of 18
(2,367 Views)

Hi Doug,

 

Unless you wrote it more than twice. Now i have understood!!

I have implemented it in my code. Now i am able to consume GetSection.

 

Thanks

 

Juergen

--Signature--
Sessions NI-Week 2017 2016
Feedback or kudos are welcome
0 Kudos
Message 16 of 18
(2,329 Views)

Look like this stille helps with .Net 3.5 (Visual Studio 2010) and TestStand 2010. Thangs

0 Kudos
Message 17 of 18
(1,927 Views)

10 years later, still relevant, worked like a charm!

 

Thank you very much 🙂

0 Kudos
Message 18 of 18
(802 Views)