c# - 如何删除应用程序代码中的 app.config

标签 c# caching .net-core enterprise-library

我正在编写一个 Dll 项目,其中我正在使用 EnterpriseLibrary.Caching.NetCore用于内存缓存。这个包需要一个 app.config。现在,当我在我的控制台应用程序中调用此 DLL 时,我也必须在我的控制台应用程序中添加 app.config。所以我需要从我的控制台应用程序中删除 app.config。

最佳答案

我认为您可以通过从自定义配置中手动设置企业库容器 (IServiceLoccator),以编程方式初始化企业库缓存 block 。因此,不应读取应用程序配置文件。例如:

var configSource = new DictionaryConfigurationSource();
var cacheSettings = new CacheManagerSettings();

configSource.Add(CacheManagerSettings.SectionName, cacheSettings);
var storageConfig = new CacheStorageData("NullBackingStore", typeof(NullBackingStore));
cacheSettings.BackingStores.Add(storageConfig);

var cacheManagerData = new CacheManagerData(
    "Cache Manager",
    60,
    1000,
    10,
    storageConfig.Name);
cacheSettings.CacheManagers.Add(cacheManagerData);
cacheSettings.DefaultCacheManager = "Cache Manager";
cacheSettings.DefaultCacheManager = cacheManagerData.Name;

EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

编辑:注意我已经在 .NET Framework 上为企业库使用了这种代码,但我想它在 .Net 核心上应该是相同的。

关于c# - 如何删除应用程序代码中的 app.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57864677/

相关文章:

mongodb - 使用 mongodb 和 redis 的缓存失效策略

rest - 如何将 IFormFile 作为 ViewModel 对象的一部分发送到 Web API

asp.net-core - ASP.NET Core launchSettings.json 无法启动浏览器

javascript - 如何将文件连同数据从 Angular 上传到 .NET Core

C# 到 VB.Net 转换 - 具有初始化的类对象数组

c# - 非常慢的性能结构

c# - 如何使用 Visual Studio 2015 注册 .NET Core 1.1 RTM 的自定义项模板?

c# - 覆盖字符串的方括号运算符?

c# - 基础提供者在打开时失败/该操作对交易状态无效

c# - CacheItemUpdateCallback 中的 HttpContext.Current Null