在 asp.net core 中使用类似 Application 的服务

Intro

在 asp.net 中,我们可以借助 Application 来保存一些服务器端全局变量,比如说服务器端同时在线的人数计数,比如一些网站的配置信息。

在 ASP.NET 应用中,之前开发的活动室预约系统把网站的 keyword 以及 Title 等信息,在网站启动的时候会从数据库加载配置并保存到 Application 中,在需要的地方直接使用 Application 来获取,后台更新配置之后,更新 Application 变量,这样就不需要重启网站就可以更新网站配置信息了,在 Razor 页面上使用示例

<title>@ViewBag.Title - @HttpContext.Current.Application["SystemTitle"]</title>
<meta name="keywords" content="@HttpContext.Current.Application["SystemKeywords"]" />

迁移到 asp.net core 后,就不能直接这样用了,于是自己实现了一个服务,去加载网站配置信息,比较简单简陋,权当是抛砖引玉,不喜勿喷

自定义 Application 服务 IApplicationSettingService

服务接口定义:

IApplicationSettingService

    public interface IApplicationSettingService
{
string GetSettingValue(string settingKey); string SetSettingValue(string settingKey, string settingValue); int AddSettings(Dictionary<string, string> dictionary);
}

基于一个字典对象的简单实现:

ApplicationSettingInMemoryService

    public class ApplicationSettingInMemoryService : IApplicationSettingService
{
private readonly ConcurrentDictionary<string, string> _settingDictionary = new ConcurrentDictionary<string, string>(); public int AddSettings(Dictionary<string, string> dictionary)
{
if (dictionary != null && dictionary.Count > 0)
{
foreach (var item in dictionary)
{
_settingDictionary[item.Key] = item.Value;
}
}
return _settingDictionary.Count;
} public string GetSettingValue(string settingKey)
{
_settingDictionary.TryGetValue(settingKey, out var val);
return val;
} public string SetSettingValue(string settingKey, string settingValue)
{
_settingDictionary[settingKey] = settingValue;
return settingValue;
}
}

也可以根据自己的需要写不同的实现,比如放在配置中或者自己的缓存中,这里应用暂时是单体应用,所以只是放在了内存对象中。

使用

  1. 在 Startup 中注册服务:

    services.TryAddSingleton<IApplicationSettingService, ApplicationSettingInMemoryService>();
  2. 从数据库中读取配置信息,初始化配置数据

  3. 在 Razor 页面上使用,示例

    @using ActivityReservation.Services
    @inject IApplicationSettingService applicationSettings
    /* 此处省略 N 行代码 ... */
    <title>@($"{ViewBag.Title} -- {applicationSettings.GetSettingValue("SystemTitle")}") </title>
    <meta name="keywords" content="@(applicationSettings.GetSettingValue("SystemKeywords"))" />

Reference

在 asp.net core 中使用类似 Application 的服务的更多相关文章

  1. 在ASP.NET Core中使用Apworks开发数据服务:对HAL的支持

    HAL,全称为Hypertext Application Language,它是一种简单的数据格式,它能以一种简单.统一的形式,在API中引入超链接特性,使得API的可发现性(discoverable ...

  2. 【半译】在ASP.NET Core中创建内部使用作用域服务的Quartz.NET宿主服务

    在我的上一篇文章中,我展示了如何使用ASP.NET Core创建Quartz.NET托管服务并使用它来按计划运行后台任务.不幸的是,由于Quartz.NET API的工作方式,在Quartz作业中使用 ...

  3. 如何在 ASP.NET Core 中构建轻量级服务

    在 ASP.NET Core 中处理 Web 应用程序时,我们可能经常希望构建轻量级服务,也就是没有模板或控制器类的服务. 轻量级服务可以降低资源消耗,而且能够提高性能.我们可以在 Startup 或 ...

  4. ASP.NET Core 中文文档 第三章 原理(6)全球化与本地化

    原文:Globalization and localization 作者:Rick Anderson.Damien Bowden.Bart Calixto.Nadeem Afana 翻译:谢炀(Kil ...

  5. ASP.NET Core 中文文档 第三章 原理(13)管理应用程序状态

    原文:Managing Application State 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:高嵩 在 ASP.NET Core 中,有多种途径可以对应用程序的状态进行 ...

  6. [译]在Asp.Net Core 中使用外部登陆(google、微博...)

    原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET Core> 摘要:本文主要介绍了使用外部登陆提供程序登陆的流程,以及身份 ...

  7. 在Asp.Net Core中集成Kafka

    在我们的业务中,我们通常需要在自己的业务子系统之间相互发送消息,一端去发送消息另一端去消费当前消息,这就涉及到使用消息队列MQ的一些内容,消息队列成熟的框架有多种,这里你可以读这篇文章来了解这些MQ的 ...

  8. ASP.NET Core中如何针对一个使用HttpClient对象的类编写单元测试

    原文地址: How to unit test a class that consumes an HttpClient with IHttpClientFactory in ASP.NET Core? ...

  9. 【译】在Asp.Net Core 中使用外部登陆(google、微博...)

    原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET Core> (本文很长) 摘要:本文主要介绍了使用外部登陆提供程序登陆的 ...

随机推荐

  1. SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

    前言 本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识. SpringCloud Hystrix Hystrix 介绍 Netfl ...

  2. .net core 杂记:日记记录

    ASP.NET Core 有内置的log组件,遗憾的是看了微软官方文档,貌似无法直接将日志存于文件或数据库,只能由自己实现或引用第三方日志组件. 以下为Nlog和log4net的使用记录 Nlog使用 ...

  3. 用css画一个哆啦A梦

    原图: 效果图: 虽然说没用啥什么高级的技巧,但这让我感受到了CSS的乐趣! 好好学习,天天向上! <!DOCTYPE html> <html> <head> &l ...

  4. Web学习的第四天

    今天通过前面的学习,自己写了段代码. 今天还学习了列表,列表的分类有:无序列表.菜单列表.目录列表.有序列表.定义列表. 列表类型                     标记符号          ...

  5. Percona XtraBackup 关于 MySQL备份还原的详细测试

    一. Percona XtraBackup 的优点. (1)无需停止数据库进行InnoDB热备: (2)增量备份MySQL: (3)流压缩传输到其它服务器: (4)在线移动表: (5)能够比较容易地创 ...

  6. redis cluster + sentinel详细过程和错误处理三主三备三哨兵

    redis cluster + sentinel详细过程和错误处理三主三备三哨兵1.基本架构192.168.70.215 7001 Master + sentinel 27001192.168.70. ...

  7. The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "SDSSDFCC" was unable to begin a distributed transaction.

    Question: SQL SERVER 通过Linkserver连接A和B 2台,A对B执行单条的增删改查没有异常(没有配置DTC) 但是开启事务后就会出现报错 Solution: 在A和B上配置D ...

  8. libaio.so.1()(64bit) is needed by MySQL-server 问题解决办法

    [root@localhost upload]# rpm -ivh MySQL-server-5.5.25a-1.rhel5.x86_64.rpmerror: Failed dependencies: ...

  9. Go基础(3)

    demo1: package main import "fmt" func print() { for i := 1; i < 10; i++ { for j := 1; j ...

  10. ASP.NET Core MVC应用程序中的后台工作任务

    在应用程序的内存中缓存常见数据(如查找)可以显着提高您的MVC Web应用程序性能和响应时间.当然,这些数据必须定期刷新. 当然你可以使用任何方法来更新数据,例如Redis中就提供了设定缓存对象的生命 ...