none
Thread locking when using appconfig file? RRS feed

  • Question

  • In a multithread environment, will a thread cause thread locking in all the other threads when getting a connection string from the configuration file? I have that doubt because this is a resource that any other thread can modify at any given time and so I think it could behave like a global variable. I am creating a Singleton server object and I would like it to be 100% stateless.

    Friday, November 7, 2008 7:35 PM

All replies

  • It should not, but you cannot rely only on post here. What you need to do is to simulate this situation in your environment and see of your application reacts appropriately.

    Monday, November 10, 2008 11:46 AM
    Moderator
  • Thanks, Mazur.

    When I read in the ConfigurationManager class documentation that "any public static (Shared in Visual Basic) members of this type are thread safe.", I think there will be thread locking for this thread safe be implemented. I am a beginner in .NET, so I have no idea if this type of thread locking (if there is any) should be taken as an issue in large scale applications and I don't know how to verify if other threads are being locked when the current thread calls configuration manager members. I have thought to use reflection to create/update an assembly based on an appconfig file at the start of a server launcher. This assembly should have methods returning a hard coded configuration information, but I'm not sure if this will solve or be a better solution for the problem as using reflection is more time consuming then normal code.

    Monday, November 10, 2008 1:48 PM
  • I've tried reflection and I think the code below is a good solution for this problem (if there is a problem). I have a launcher application that reads configuration information at start and calls shared methods from a dynamic assembly that return hard coded informations and compare them. If the assembly methods are returning outdated information, a new assembly is generated with the most recent data being returned by its methods. My server application reference this assembly and call its shared members to obtain configuration information at run time without have to access global shared data what could cause thread locking.

     

    Dim tp As Type = GetType(System.String)

    Dim asmName As New AssemblyName("CfgData")

    Dim domain As AppDomain = AppDomain.CurrentDomain

    Dim demotype As TypeBuilder

    Dim demoAssembly As AssemblyBuilder = _

    domain.DefineDynamicAssembly(asmName, _

    AssemblyBuilderAccess.Save)

    Dim demoModule As ModuleBuilder = _

    demoAssembly.DefineDynamicModule( _

    asmName.Name, _

    asmName.Name & ".dll")

    demotype = demoModule.DefineType("CCfgData", TypeAttributes.Class Or TypeAttributes.Public)

    Dim Metodo As MethodBuilder = demotype.DefineMethod("GetConnectionString", MethodAttributes.Public Or MethodAttributes.Static, tp, Nothing)

    Dim myILGenerator As ILGenerator

    myILGenerator = Metodo.GetILGenerator()

    myILGenerator.Emit(OpCodes.Ldstr, "This is my connection string.")

    myILGenerator.Emit(OpCodes.Ret)

    demotype.CreateType()

    demoAssembly.Save(asmName.Name & ".dll")

    Monday, November 10, 2008 3:33 PM