指定.Net配置文件的位置 [英] Specifying the location of .Net configuration files

查看:233
本文介绍了指定.Net配置文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用.Net调试模型来处理加载和合并配置文件的历史的能力,类似于如何将教科书ASP.Net web confgiuration文件合并在一起。



感谢优秀的揭开.NET的奥秘2.0配置系列文章我已经找出了如何做大多数事情,但我有一些麻烦指定要加载的配置文件的确切的底层。正如web.config文件一样,可能有一个无限的配置文件的历史要加载,并且关于应该包含哪些文件的规则需要在运行时确定。



作为一种例行的heirachy我在说 - 如果用户正在处理一个文件c:\User\jblogs\Documents\Projects\MyApp\AppFile12.txt,那么我可能希望以下文件包含在heirachy中:




  • c:\User\jblogs\Documents\Projects\MyApp \myapp.config

  • c:\User\jblogs\Documents\Projects\myapp.config

  • c:\ User\jblogs\myapp.config

  • c:\Program Files\MyApp\config\myapp.config



(免责声明:以上是我想要实现的简化示例,但我认为如果我能找出如何做上述,那么我会破解它)



我甚至尝试在Reflector中查看web.config代码,但是它很难理解正在发生的事情 - 任何人谁能理解这一点更好点我在右边方向?

解决方案

  public class MyConfigObject 
{
public class MyConfigObject(MyConfigObject parent)
{
//复制构造函数 - 不要更改父代。
}
}

public class MyConfigHandler:IConfigurationSectionHandler
{
public object Create(object parent,object context,XmlNode section)
{
var config = new MyConfigObject((MyConfigObject)parent);
//根据需要处理节和mutate配置
return config;
}
}

现在,当您需要应用多层配置时

 

 var currentWorkingDirectory = Path.GetDirectoryName(c:\\User\\jblogs\\Documents\\Projects\\MyApp\\AppFile12.txt); 
var currentDirectory = new DirectoryInfo(currentWorkingDirectory)
var userDataRootDirectory = new DirectoryInfo(c:\\User\\jblogs\\);

var configFilesToProcess = new Stack< string>();

do
{
//在currentDirectory中搜索myapp.config
//如果找到推送路径到configFilesToProcess
currentDirectory = currentDirectory.GetParent() ;
}
while(!currentDirectory.Equals(userDataRootDirectory)

var applicationConfigPath =c:\\Program Files\\MyApp\\config\ \myapp.config;
configFilesToProcess.Push(applicationConfigPath);

var configHandler = new MyConfigHandler();
对象配置= null;
对象configContext = null; //不知道这是什么,但我认为它是整个配置文件
while(configFilesToProcess.Any())
{
var configPath = configFilesToProcess.Pop();
//加载配置文件
var configNode = null; //使用xpath提取配置节点
configuration = configHandler.Create(configuration,configContext,configNode);
}

请注意,上面显示的代码不是最漂亮的,但它显示了意图 - 我建议将其拆分为每个做一件事并做得很好的单独井名方法的数量=)


I'm trying to use the .Net confgiuration model to handle the ability to load and merge together a heirachy of configuration files in a similar way to how the heirachical ASP.Net web confgiuration files are merged together.

Thanks to the excellent Unraveling the Mysteries of .NET 2.0 Configuration series of articles I've figured out how to do most things, but I'm having some trouble specifying the exact heirachy of configuration files to be loaded. Just as with web.config files there can potentially be an infinite heirachy of configuration files to load and the rules about exactly which files should be included needs to be determined at runtime.

As an example of the sort of heirachy I'm talking about - if a user is working on a file "c:\User\jblogs\Documents\Projects\MyApp\AppFile12.txt" then I might want the following files to be included in the heirachy:

  • c:\User\jblogs\Documents\Projects\MyApp\myapp.config
  • c:\User\jblogs\Documents\Projects\myapp.config
  • c:\User\jblogs\myapp.config
  • c:\Program Files\MyApp\config\myapp.config

(Disclaimer: The above is a simplified example of what I want to achieve, but I think that if I can figure out how to do the above then I will have cracked it)

I've even tried looking at the web.config code in Reflector, but its tricky to understand exactly what's going on - can anyone who understands this a bit better points me in the right direction?

解决方案

public class MyConfigObject
{
  public class MyConfigObject( MyConfigObject parent )
  {
    // Copy constructor - don't alter the parent.
  }
}

public class MyConfigHandler : IConfigurationSectionHandler
{
  public object Create( object parent, object context, XmlNode section )
  {
    var config = new MyConfigObject( (MyConfigObject)parent );
    // Process section and mutate config as required
    return config;
  }
}

Now when you need to apply numerous levels of configuration simply collect all the files to process in a stack working up from the directory the file is in and then process them in LIFO order by popping the stack.

var currentWorkingDirectory = Path.GetDirectoryName( "c:\\User\\jblogs\\Documents\\Projects\\MyApp\\AppFile12.txt" );
var currentDirectory = new DirectoryInfo( currentWorkingDirectory )
var userDataRootDirectory = new DirectoryInfo( "c:\\User\\jblogs\\" );

var configFilesToProcess = new Stack<string>();

do
{
  // Search for myapp.config in currentDirectory
  // If found push path onto configFilesToProcess
  currentDirectory = currentDirectory.GetParent();
}
while( !currentDirectory.Equals( userDataRootDirectory )

var applicationConfigPath = "c:\\Program Files\\MyApp\\config\\myapp.config";
configFilesToProcess.Push( applicationConfigPath );

var configHandler = new MyConfigHandler();
object configuration = null;
object configContext = null; // no idea what this is but i think it is the entire config file
while( configFilesToProcess.Any() )
{
  var configPath = configFilesToProcess.Pop();
  // Load config file
  var configNode = null; // Extract your config node using xpath
  configuration = configHandler.Create( configuration, configContext, configNode );
}

Please note that the code shown above is not the prettiest but it shows the intent - i would suggest splitting it out into a number of separate well named methods that each do one thing and do it well =)

这篇关于指定.Net配置文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆