标签:
三、程序配置在C# Redis实战(二)中我们安装好了Redis的系统服务,此时Redis服务已经运行。
现在我们需要让我们的程序能正确读取到Redis服务地址等一系列的配置信息,首先,,需要在Web.config文件中添加如下信息:
[html]
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit ?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="RedisConfig" type="RedisDemo.RedisConfigInfo, RedisDemo"/>
</configSections>
<RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" MaxWritePoolSize="60"
MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="180" RecordeLog="false">
</RedisConfig>
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-RedisDemo-20131125110945;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-RedisDemo-20131125110945.mdf" />
</connectionStrings>
</configuration>
有了以上信息还不够,还需要用C#代码来读取并且操作,获取Redis配置的程序如下:
[csharp]
public static RedisConfigInfo GetConfig()
{
RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
return section;
}
public static RedisConfigInfo GetConfig(string sectionName)
{