如何在.Net 4.5上存储.Net/ASP.Net应用程序的复杂配置信息
我正在寻找一种简单而优雅的方法来存储我的应用程序设置。这里有一个例子,很好地描述了我正在寻找的如何在.Net 4.5上存储.Net/ASP.Net应用程序的复杂配置信息,.net,configuration,app-config,enterprise-library,configuration-files,.net,Configuration,App Config,Enterprise Library,Configuration Files,我正在寻找一种简单而优雅的方法来存储我的应用程序设置。这里有一个例子,很好地描述了我正在寻找的 public class Office { string location; int numberOfWorkStations; int numberOfServers; string developerNames[]; } 配置如下: <Office> <Location>Mumbai, India</Location>
public class Office
{
string location;
int numberOfWorkStations;
int numberOfServers;
string developerNames[];
}
配置如下:
<Office>
<Location>Mumbai, India</Location>
<NumberOfWorkStations>10</NumberOfWorkStations>
<NumberOfServers>2</NumberOfServers>
<DeveloperNames>
<DeveloperName>GoGo</DeveloperName>
<DeveloperName>MoMo</DeveloperName>
<DeveloperName>JoJo</DeveloperName>
</DeveloperNames>
</Office>
印度孟买
10
2.
果戈
莫莫
乔乔
早在2005/6年,曾经有一个企业库配置块,用于抽象所有XML序列化内容
我正在查看最新版本的企业库,但配置块似乎不再存在
我使用的是.NETFramework4.5,我的想法是,由于该功能已从企业库中删除,它现在应该以本机形式存在于.NETFramework中
我读过这篇文章,但觉得从ConfigurationSection、ConfigurationElement等中派生出来的内容与企业库过去提供的内容相比仍然太多。我所寻找的是非常接近XMLSerialization的东西,但我不想编写代码来进行序列化,因为我觉得这就像重新发明轮子一样
感谢您查看我的帖子。我使用:
这是配置文件(将其另存为OfficeConfig.config,并将其与exe放在同一文件夹中)
印度孟买
10
2.
果戈
莫莫
乔乔
代码如下:
namespace ConsoleApplication1
{
using System;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Installer;
public class Office
{
public string Location { get; set; }
public int NumberOfWorkStations { get; set; }
public int NumberOfServers { get; set; }
public string[] DeveloperNames { get; set; }
}
class Program
{
static void Main(string[] args)
{
var container = new WindsorContainer()
.Install(Configuration.FromXmlFile("OfficeConfig.config"))
.Register(
Component.For<Office>().Named("Office").LifeStyle.Singleton,
Component.For<Program>().LifeStyle.Transient);
var program = container.Resolve<Program>();
}
public Program(Office office)
{
Console.WriteLine(office.Location);
Console.WriteLine(office.NumberOfWorkStations);
Console.WriteLine(office.NumberOfServers);
foreach (var name in office.DeveloperNames)
{
Console.WriteLine(name);
}
}
}
}
命名空间控制台应用程序1
{
使用制度;
使用Castle.MicroKernel.Registration;
使用温莎城堡;
使用Castle.Windsor.Installer;
公课办公室
{
公共字符串位置{get;set;}
公共int NumberOfWorkStations{get;set;}
public int NumberOfServers{get;set;}
公共字符串[]DeveloperNames{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
var容器=新WindsorContainer()
.Install(Configuration.FromXmlFile(“OfficeConfig.config”))
.登记(
Component.For().Named(“Office”).lifety.Singleton,
Component.For().lifesture.Transient);
var program=container.Resolve();
}
公共计划(办公室)
{
控制台写入线(办公室位置);
控制台。写入线(办公室。工作站编号);
Console.WriteLine(office.NumberOfServers);
foreach(var name in office.DeveloperNames)
{
Console.WriteLine(名称);
}
}
}
}
您甚至可以使用列表和字典作为属性我使用:
这是配置文件(将其另存为OfficeConfig.config,并将其与exe放在同一文件夹中)
印度孟买
10
2.
果戈
莫莫
乔乔
代码如下:
namespace ConsoleApplication1
{
using System;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Installer;
public class Office
{
public string Location { get; set; }
public int NumberOfWorkStations { get; set; }
public int NumberOfServers { get; set; }
public string[] DeveloperNames { get; set; }
}
class Program
{
static void Main(string[] args)
{
var container = new WindsorContainer()
.Install(Configuration.FromXmlFile("OfficeConfig.config"))
.Register(
Component.For<Office>().Named("Office").LifeStyle.Singleton,
Component.For<Program>().LifeStyle.Transient);
var program = container.Resolve<Program>();
}
public Program(Office office)
{
Console.WriteLine(office.Location);
Console.WriteLine(office.NumberOfWorkStations);
Console.WriteLine(office.NumberOfServers);
foreach (var name in office.DeveloperNames)
{
Console.WriteLine(name);
}
}
}
}
命名空间控制台应用程序1
{
使用制度;
使用Castle.MicroKernel.Registration;
使用温莎城堡;
使用Castle.Windsor.Installer;
公课办公室
{
公共字符串位置{get;set;}
公共int NumberOfWorkStations{get;set;}
public int NumberOfServers{get;set;}
公共字符串[]DeveloperNames{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
var容器=新WindsorContainer()
.Install(Configuration.FromXmlFile(“OfficeConfig.config”))
.登记(
Component.For().Named(“Office”).lifety.Singleton,
Component.For().lifesture.Transient);
var program=container.Resolve();
}
公共计划(办公室)
{
控制台写入线(办公室位置);
控制台。写入线(办公室。工作站编号);
Console.WriteLine(office.NumberOfServers);
foreach(var name in office.DeveloperNames)
{
Console.WriteLine(名称);
}
}
}
}
您甚至可以使用列表和字典作为属性
我正在查看最新版本的企业库,但配置块似乎不再存在
我使用的是.NETFramework4.5,我的想法是,既然该功能已从企业库中删除,现在就应该使用它了
本机存在于.Net framework中
是的,你的假设是正确的。当该功能进入.NET Framework时,配置块被删除
另一种方法是利用工具创建配置类。例如
我正在查看最新版本的企业库,但配置块似乎不再存在
我使用的是.NETFramework4.5,我的想法是,既然该功能已从企业库中删除,现在就应该使用它了
本机存在于.Net framework中
是的,你的假设是正确的。当该功能进入.NET Framework时,配置块被删除
另一种方法是利用工具创建配置类。例如
非常感谢!正是我想要的。非常感谢!这正是我想要的。