2012-02-08 50 views
2

我知道在SO上的类似问题很多,但我有一个警告。在.NET中各种组件之间共享配置设置

基本前提是可预测的:我继承了一个由多个组件组成的产品,所有这些组件都共享一些配置设置,其中包括连接字符串。目前,这些组件使用哈希密码调用Web服务以检索连接字符串(blegh),但这有时会导致Windows启动时Web服务和需要配置值的NT服务之间的竞争条件。

我想创建一个优雅的解决方案,允许我从单个安全位置(即注册表或machine.config)共享这些设置。这些组件中的任何一个都可以在单个部署环境中轻松实现,但(这是问题)其中一个组件是一次点击应用程序。

所以简而言之,我的问题是:我怎样才能创建一个集中化的配置设置机制,并将其传播到一次性部署?

选项我已考虑:

据我所知,这两个解决方案依赖于共享配置文件的本地副本的可用性,这不适用于点击一次。

有两点需要注意有关我们的点击一次应用程序部署环境:

  • 部署始终是一个企业局域网网络中,从而配置设置,如连接字符串是普遍适用的。
  • 安装时与点击一次应用程序打包在一起的配置设置可安全地在随后的部署中被覆盖。
+0

我的假设是正确的,你现在使用的webservice是否也托管在本地机器上?你不能包含集中选项? – Polity 2012-02-13 07:40:51

+0

正确,客户端无法保证集中式选项可用的部署环境。 – staterium 2012-02-13 09:12:24

+0

在这种情况下,我会建议你当前的实现并不是那么糟糕,它可以在大多数信任环境中工作,并且易于扩展。通过使用互斥体来确保启动顺序,可以很容易地克服竞争条件。 – Polity 2012-02-13 09:21:40

回答

0

正如评论中指出的那样,我认为目前的解决方案是一种体面的方式,因为webservices对安全问题不敏感,并且它确保集中式解决方案。为了克服竞争条件,可以使用互斥锁来强制客户端等待服务器启动。示例代码:

string mutexName = "C01F6FBB-50E9-4BFA-AFBA-209C316AE9FB"; 
TimeSpan waitInterval = TimeSpan.FromSeconds(1d); 

// Server sample 
System.Threading.Tasks.Task.Factory.StartNew(() => 
{ 
    // similate some startup delay for the server 
    Thread.Sleep(TimeSpan.FromSeconds(5)); 

    using (Mutex mutex = new Mutex(true, mutexName)) 
    { 
     Console.WriteLine("Server: Good morning!"); 

     // Do server business, ensure that the mutex is kept alive throughout the server lifetime 
     // this ensures that the application can always check whether the server is available or not 
     Thread.Sleep(TimeSpan.FromSeconds(5));  
    } 
}); 

// Application sample 
System.Threading.Tasks.Task.Factory.StartNew(() => 
{ 
    Console.WriteLine("Application: Checking the server..."); 

    bool mutexOpened = false; 

    while (!mutexOpened) 
    { 
     try 
     { 
      using (Mutex mutex = Mutex.OpenExisting(mutexName)) 
      { 
       mutexOpened = true; 
      } 
     } 
     catch (WaitHandleCannotBeOpenedException) 
     { 
      Console.WriteLine("Application: Server is not yet ready...."); 

      // mutex does not exist yet, wait for the server to boot up 
      Thread.Sleep(waitInterval); 
     } 
    } 

    // Server is ready, we can do our application business now 
    // note that we dont need to preserve the mutex anymore. 
    // we only used it to ensure that the server is available. 
    Console.WriteLine("Application: Good morning to you!"); 
}); 

Console.ReadLine(); 

希望这有助于!