 |
|
 |
Hi
Do u mean this issue is not there from .net 2.0 onwards??
Iam using microsoft enteprise library for logging application 4.0 2008 version. I have created a class library which has all log information which iam going to use it in other project.But I get an error tat "The configuration section for Logging cannot be found in the configuration source.".
Can u suggest me about it??
Regards
Mailr
|
|
|
|
 |
|
 |
I have a windows service starting with settings from a config file as http://moebiusit.com/blog/pabloromano/2009/07/13/changing-dll-settings-without-re-compiling/[]. A dll used by the service gets the appropriate setting values from the config file BUT after a while (over a day) the dll settings are reset to the default settings provided in the dll itself. (the ones I entered in VS under dll project > properties > settings. Why is this happening? How can I avoid this and keep my original setting value?
modified 12 Oct '11.
|
|
|
|
 |
|
|
 |
|
 |
More straight forward? How do you figure if you have to actually reference the DLL's config file in the main app?! How does this adhere to standard software practices? It doesn't. Your DLL should not rely on the using application to function.
|
|
|
|
 |
|
 |
You reference the config of the dll in the config of the main the same way you have to reference the dll in the main app. This doesn't go against any standard software practice.
The most important thing of my approach is it doesn't rely on a class to get the settings, I keep using the .Net Settings the same way without any change in the code. Having to warn everybody not to use the settings and use this class instead is very difficult, and even more in this case because not doing this will end up in strange runtime errors (not while compiling) because the settings will have sometimes the default values and others the values provided by your class.
I haven't made any benchmark of the two approaches but I would guess the switch between AppDomains isn't cheap, if the dll is called often it will produce a drawback on performance.
|
|
|
|
 |
|
 |
<pre> public class AssemblyConfig : MarshalByRefObject { static Dictionary<string, AssemblyConfig> _instances = null; static public AssemblyConfig GetInstance() { if (_instances == null) { _instances = new Dictionary<string, AssemblyConfig>(); AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); } Assembly callingAssembly = Assembly.GetCallingAssembly(); Assembly thisAssembly = Assembly.GetAssembly(typeof(AssemblyConfig)); string assemblyName = callingAssembly.ManifestModule.Name; if (!_instances.ContainsKey(assemblyName)) { AppDomainSetup ads = new AppDomainSetup(); ads.ConfigurationFile = callingAssembly.Location + ".config"; AppDomain ad = AppDomain.CreateDomain(assemblyName, null, ads); object objProxy = ad.CreateInstanceFromAndUnwrap(thisAssembly.Location, typeof(AssemblyConfig).FullName); AssemblyConfig newInstance = (AssemblyConfig)objProxy; _instances.Add(assemblyName, newInstance); } return _instances[assemblyName]; } static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { return Assembly.GetAssembly(typeof(AssemblyConfig)); } public object GetValue(string name) { AppSettingsReader settingReader = new AppSettingsReader(); return settingReader.GetValue(name, typeof(string)); } }</pre> This will create a new AssemblyConfig instance for each dll you call it from. So in your dll simply use: string myStringVal = (string)AssemblyConfig.GetInstance().Setting("myStringVal"); modified on Wednesday, March 11, 2009 8:52 AM
|
|
|
|
 |
|
 |
Congratulations!!!
Scenary:
I have a Service with WCF and using several .dll including AppBlock and its .config files. So I'd like that every dll will use their own config file.
Question:
Will this solution work fine on this environment, having several clients consuming the Service at the same time?
Thanks for your answer,
Alexeins
|
|
|
|
 |
|
 |
Hi all,
I'm really interest to use this function because I have allready this discribte problem.
Is there anyone how had translate this solution to vb.net or can do this?
I try it but I'm fail to do it.
It would be really nice if somebody help me out.
All the best
Christian
|
|
|
|
 |
|
 |
I know it s late, but for people who are interess
ps: i have done some modification for my proper use
enjoy
Imports System.Configuration
Imports System.Reflection
Friend Class MyDllConfig
Implements IDisposable
Private _oldConfig As String
Private _libCompat As Boolean
'Private Const _newConfig As String = "mydll.dll.config"
Private Shared _newConfig As String
Public Shared Property newConfig() As String
Get
Return _newConfig
End Get
Set(ByVal value As String)
_newConfig = value
End Set
End Property
' don't forget to rename this!!
Protected Overrides Sub Finalize()
Dispose()
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
Switch(_oldConfig)
End Sub
Friend Sub New()
_libCompat = Assembly.GetAssembly(GetType(ConfigurationSettings)).GetName().Version.ToString().CompareTo("1.0.5000.0") = 1
_oldConfig = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString()
Switch(_newConfig)
End Sub
Friend Sub New(ByVal a As String)
newConfig = a
_libCompat = Assembly.GetAssembly(GetType(ConfigurationSettings)).GetName().Version.ToString().CompareTo("1.0.5000.0") = 1
_oldConfig = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString()
Switch(_newConfig)
End Sub
Protected Sub Switch(ByVal config As String)
If _libCompat Then
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", config)
Dim fiInit As FieldInfo = GetType(System.Configuration.ConfigurationSettings).GetField("_configurationInitialized", BindingFlags.NonPublic Or BindingFlags.[Static])
Dim fiSystem As FieldInfo = GetType(System.Configuration.ConfigurationSettings).GetField("_configSystem", BindingFlags.NonPublic Or BindingFlags.[Static])
If fiInit IsNot Nothing AndAlso fiSystem IsNot Nothing Then
fiInit.SetValue(Nothing, False)
fiSystem.SetValue(Nothing, Nothing)
End If
End If
End Sub
Public Shared Function ConnectionString() As String
Dim [cstr] As String
Using New MyDllConfig()
'[cstr] = ConfigurationSettings.AppSettings("ConnectionString")
[cstr] = ConfigurationManager.ConnectionStrings("DBConnectionStringForMysqlToDom").ToString() ' AppSettings("ConnectionString").
End Using
Return [cstr]
End Function
End Class
|
|
|
|
 |
|
 |
Hi,
Security Update for Microsoft .NET Framework, Version 1.1 Service Pack 1 (KB928366) has broken this work around.
Any info on a fix would be welcome.
regards,
DAN
|
|
|
|
 |
|
 |
I was able to resolve this by changing the Switch method as such
protected void Switch(string config)
{
if ( _libCompat )
{
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE",config);
FieldInfo[] fiStateValues = null;
Type tInitState = typeof(
System.Configuration.ConfigurationSettings).GetNestedType(
"InitState",
BindingFlags.NonPublic);
if(null != tInitState)
fiStateValues = tInitState.GetFields();
FieldInfo fiInit = typeof(
System.Configuration.ConfigurationSettings).GetField(
"_initState",
BindingFlags.NonPublic|BindingFlags.Static);
FieldInfo fiSystem = typeof(
System.Configuration.ConfigurationSettings).GetField(
"_configSystem",BindingFlags.NonPublic|BindingFlags.Static);
if ( fiInit != null && fiSystem != null && null != fiStateValues)
{
fiInit.SetValue(null,fiStateValues[1].GetValue(null));
fiSystem.SetValue(null,null);
}
}
}
|
|
|
|
 |
|
 |
The original intend of this article (as mentioned in Background section) is to use custom config file for Microsoft Enterprise library. The ideal way to do this is to use Enterprise Library's simple elegant way to take external config files. We just need to provide a custom ConfigurationSource to Enterprise Library as follows:
// Create the database using a custom configuration file
FileConfigurationSource fileConfigSource = new FileConfigurationSource("my.config");
DatabaseProviderFactory dbFactory = new DatabaseProviderFactory(fileConfigSource);
Database db = dbFactory .Create(dbName);
For details, see Tom Hollander's blog [ ^ ]
Regards,
Ann Catherine Jose
http://www.chiramattel.com/ann/blog
|
|
|
|
 |
|
 |
This article is excellent. I have a VB6 service calling a .NET 2 DLL using createobject and I required that the .NET DLL have it's own config file. I set the
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", System.Reflection.Assembly.GetExecutingAssembly().CodeBase + ".Config")
and all i had to do was distribute the Dllname.dll.config file in the same directory where the DLL resides. Now the .NET DLL can have its own settings.
I think Microsoft has totally messed up the way confirguration info is to be stored in .net 2. Again Visual studio seems to overwrite the .CONFIG file everytime i rebuild my application which means removing other appsettings, boolean switches etc and i can't seem to find a way to override this.
S10
|
|
|
|
 |
|
 |
Thanks for the review...and thanks for the little code enhancement for nameing the config file without hard coding it!
|
|
|
|
 |
|
 |
Hi, n10sive.
I looked through your article for some time, feel it intererst.
But I can't get its means entirely.
This is my understand:
I have a Dll, with a MyDll.config file:
="1.0" ="utf-8"
<configuration>
<appSettings>
<add key ="AlgorithmConfigFile" value="CashAcceptType.xml"/>
</appSettings>
<connectionStrings/>
</configuration>
And I have an App project, I put your Class into the project.
Using this line to get the file path:
string str = MyDllConfig.ConnectionString();
but, when I debug into
public static string ConnectionString()
{
string cstr;
using (new MyDllConfig())
{
string str = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();
cstr = ConfigurationManager.AppSettings["AlgorithmConfigFile"];
}
return cstr;
}
"str" is MyDll.config which is under my execute app path.
but "cstr" is null!
I don't know why
ConfigurationManager.AppSettings["AlgorithmConfigFile"];
cant' get the value "CashAcceptType.xml"
Could you give me a help? Thanks!
|
|
|
|
 |
|
 |
Supose this scenario:
1. A dll was created with an App.Config file
2. The dll has a strong name
3. The dll resides in the GAC
Is it possible to retrieve the dll App.Config information?
Is there any other alternative?
|
|
|
|
 |
|
 |
Has anyone found a similar solution for .NET framework 2.0.?
Thanks.
|
|
|
|
 |
|
 |
Hope this is helpful for your case.
internal class MyDllConfig : IDisposable
{
private string _oldConfig;
private bool _libCompat;
private const string _newConfig = @"MyDll.config";
internal MyDllConfig()
{
_libCompat = Assembly.GetAssembly(typeof(
ConfigurationManager)).GetName().Version.ToString().
CompareTo("2.0.0.0") == 0;
_oldConfig = AppDomain.CurrentDomain.GetData(
"APP_CONFIG_FILE").ToString();
Switch(_newConfig);
}
protected void Switch(string config)
{
if (_libCompat)
{
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", config);
FieldInfo[] fiStateValues = null;
Type tInitState = typeof(
System.Configuration.ConfigurationManager).GetNestedType(
"InitState",
BindingFlags.NonPublic);
if (null != tInitState)
fiStateValues = tInitState.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()
{
Switch(_oldConfig);
}
public static string ConnectionString()
{
string cstr;
using (new MyDllConfig())
{
cstr = ConfigurationManager.AppSettings["ConnectionString"];
}
return cstr;
}
}
|
|
|
|
 |
|
 |
Your approach works fine for me on XP with SP2, and also on 2003 Server without SP1, but as soon as SP1 is installed, it fails to switch configuration files.
Any idea why?
-bjørn
|
|
|
|
 |
|
 |
This aproach only works with specific builds of .NET (clr 1.1).
There is a line in the code:
_libCompat = Assembly.GetAssembly(typeof(ConfigurationSettings)).GetName().Version.ToString().CompareTo("1.0.5000.0") == 0;
What is that returning? I do not run 20003 sp1 server so I can't help or troubleshoot this for you but checking the version of System.Configuration before and after SP1 might help me out.
Thanks
|
|
|
|
 |
|
 |
I am seeing the same problem on Windows 2003...the following line returns Nothing:
Dim fiInit As FieldInfo = GetType(System.Configuration.ConfigurationSettings).GetField("_configurationInitialized", BindingFlags.NonPublic Or BindingFlags.Static)
Has anyone been able to get around this?
Edward Rakovsky
|
|
|
|
 |
|
 |
Win 2003 SP1 updated the 1.1 framework to version 1.1.4322.2300. In this version, the internals of the class have changed. I am trying to figure out how to make similar code work around the new class.
The version check in this code does not work because the version number reported by the call has not changed.
Mike Cerutti
|
|
|
|
 |
|
 |
I´ve tried using this strategy in Framework .NET 2.0 but "_configurationInitialized" and "_configSystem" don't exist.
Any ideas ?!?
|
|
|
|
 |
|
 |
I do not currently have CLR 2.0. But...as stated, this was for 1.1 only. As far as I know, the problem of having a .config file for a DLL was solved in 2.0 and therefore, this hack is not required (this may also explain why the code base has changed and these variables don't exist anymore). Maybe someone can weigh in on this that has 2.0
|
|
|
|
 |
|
 |
In CLR 2.0 for this purpose is used class Configuration.
|
|
|
|
 |