我正在构建一个允许我在Mongo数据库中缓存常见请求的解决方案。
我想尽可能使它成为通用的,因此,我创建了包含以下属性的ConfigurationElement
:
在配置文件中编写时,如下所示:
<add name="mongoCachingProvider" provider="GeniDev.ApplicationBlocks.Data.Caching.Mongo.Configuration.MongoCachingBackingStoreElement, GeniDev.ApplicationBlocks.Data.Caching.Mongo"
maxItems="10" />
此配置部分的.NET类如下所示:
public class CachingBackingStoreElement : ConfigurationElement
{
#region Properties
// Provide some constants that are used by this class.
private const string CachingBackingStoreNameProperty = "name";
private const string CachingBackingStoreProviderProperty = "provider";
private const string CachingBackingStoreMaxItemsProperty = "maxItems";
/// <summary>
/// Gets or sets the name of the provider.
/// </summary>
[ConfigurationProperty(CachingBackingStoreNameProperty, IsRequired = true)]
public string Name
{
get { return (string)this[CachingBackingStoreNameProperty]; }
set { this[CachingBackingStoreNameProperty] = value; }
}
/// <summary>
/// Gets or sets the provider.
/// </summary>
[ConfigurationProperty(CachingBackingStoreProviderProperty, IsRequired = true)]
[TypeConverter(typeof(TypeNameConverter))]
public Type Provider
{
get { return (Type)this[CachingBackingStoreProviderProperty]; }
set { this[CachingBackingStoreProviderProperty] = value; }
}
/// <summary>
/// Gets or sets the maximum items that should be stored in the cache.
/// </summary>
[ConfigurationProperty(CachingBackingStoreMaxItemsProperty, IsRequired = true)]
public int Port
{
get { return (int)this[CachingBackingStoreMaxItemsProperty]; }
set { this[CachingBackingStoreMaxItemsProperty] = value; }
}
#endregion
}
现在,我需要一个Mongo配置部分,需要其他属性,例如dbServer
,dbName
,dbCacheBucket
和maxSizeInMB
。
因此,我创建了一个新的ConfigurationElement
,它继承自上面的CachingBackingStoreElement
。
代码导致以下结果:
public class MongoCachingBackingStoreElement : CachingBackingStoreElement
{
#region Properties
// Provide some constants that are used by this class.
private const string MongoCachingBackingStoreServerProperty = "dbServer";
private const string MongoCachingBackingStoreProperty = "dbPort";
private const string MongoCachingBackingStoreNameProperty = "dbName";
private const string MongoCachingBackingStoreBucketNameProperty = "dbCacheBucket";
private const string MongoCachingBackingStoreMaxSizeInMb = "maxSizeInMB";
/// <summary>
/// Gets or sets the server.
/// </summary>
[ConfigurationProperty(MongoCachingBackingStoreServerProperty, IsRequired = true)]
public string Server
{
get { return (string)this[MongoCachingBackingStoreServerProperty]; }
set { this[MongoCachingBackingStoreServerProperty] = value; }
}
/// <summary>
/// Gets or sets the port.
/// </summary>
[ConfigurationProperty(MongoCachingBackingStoreProperty, IsRequired = true)]
public int Port
{
get { return (int)this[MongoCachingBackingStoreProperty]; }
set { this[MongoCachingBackingStoreProperty] = value; }
}
/// <summary>
/// Gets or sets the database name.
/// </summary>
[ConfigurationProperty(MongoCachingBackingStoreNameProperty, IsRequired = true)]
public string DbName
{
get { return (string)this[MongoCachingBackingStoreNameProperty]; }
set { this[MongoCachingBackingStoreNameProperty] = value; }
}
/// <summary>
/// Gets or sets the bucket name.
/// </summary>
[ConfigurationProperty(MongoCachingBackingStoreBucketNameProperty, IsRequired = true)]
public string CacheBucketName
{
get { return (string)this[MongoCachingBackingStoreBucketNameProperty]; }
set { this[MongoCachingBackingStoreBucketNameProperty] = value; }
}
/// <summary>
/// Gets or sets the maximum size that the cache should take (in MegaBytes).
/// </summary>
[ConfigurationProperty(MongoCachingBackingStoreMaxSizeInMb, IsRequired = true)]
public int MaxSize
{
get { return (int)this[MongoCachingBackingStoreMaxSizeInMb]; }
set { this[MongoCachingBackingStoreMaxSizeInMb] = value; }
}
#endregion
}
这允许我在配置文件中编写以下内容:
<add name="mongoCachingProvider" provider="GeniDev.ApplicationBlocks.Data.Caching.Mongo.Configuration.MongoCachingBackingStoreElement, GeniDev.ApplicationBlocks.Data.Caching.Mongo"
maxItems="10" dbServer="127.0.0.1" dbPort="27017" dbName="AppServer-Cache" dbCacheBucket="ODataRequestsCache" maxSizeInMB="500" />
当我立即请求配置部分时,我收到以下错误:
System.Configuration.ConfigurationErrorsExceptionUnrecognized attribute 'maxSizeInMB'. Note that attribute names are case-sensitive.
任何人都知道如何解决这个问题?