The solution is to implement a class that changes the configuration file, associated with the project. Here is some working code doing that:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace OmeksOfficeTools
{
public class ConfigurationFileChanger : IDisposable
{
private string currentConfigFilePath;
private string newConfigFilePath = @"YourLibraryPathHere";
public ConfigurationFileChanger()
{
currentConfigFilePath = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();
ChangeConfigFile(newConfigFilePath);
}
protected void ChangeConfigFile(string newConfigFilePath)
{
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", newConfigFilePath);
FieldInfo[] fiStateValues = null;
Type initStateType = typeof(System.Configuration.ConfigurationManager).GetNestedType("InitState", BindingFlags.NonPublic);
if (initStateType != null)
{
fiStateValues = initStateType.GetFields();
}
FieldInfo fiInit = typeof(System.Configuration.ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static);
FieldInfo fiSystem = typeof(System.Configuration.ConfigurationManager).GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static);
if (fiInit != null && fiSystem != null && null != fiStateValues)
{
fiInit.SetValue(null, fiStateValues[1].GetValue(null));
fiSystem.SetValue(null, null);
}
}
public void Dispose()
{
ChangeConfigFile(currentConfigFilePath);
}
}
}
This really worked for me. I use .Net Framework 3.5 sp1 but I think this will work for .Net Framework 2.0 and above.
The idea is that this code sets the value for app.config path that the framework is going to use and then forces it to reload its configuration by setting 's_initState' to false and 's_configSystem' to null.
The class implements IDisposable to be able to revert to the old configuration file.
So it is good to use the using pattern as follows:
using (ConfigurationFileChanger cfch = new ConfigurationFileChanger())
{
SecurityServiceClient client = new SecurityServiceClient();
//Do your service operation calls here...
}
Where SecurityServiceClient is my service client proxy.
Enjoy :)
P.S. I of course didn't came to that solution while dreaming tonight, so I should give my thanks to the author of THIS article (see also comments under the article).