Implementation of Application like services in asp. net core

  • 2021-11-01 02:57:30
  • OfStack

Intro#

In asp. net, we can use Application to save 1 server-side global variables, such as the number of people online at the same time on the server side, such as 1 website configuration information.

In ASP. NET application, The activity room reservation system developed before loads the information such as keyword and Title from the database and saves it to Application when the website starts, and directly uses Application to obtain it where necessary. After updating the configuration in the background, update Application variables, so that the website configuration information can be updated without restarting the website. Use examples on Razor pages:


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

After migrating to asp. net core, you can't use it directly, so you realized a service to load the website configuration information, which is relatively simple and humble, and you should throw bricks to attract jade. Don't spray if you don't like it

Custom Application service IApplicationSettingService #

Service interface definition:

IApplicationSettingService


 public interface IApplicationSettingService
 {
  string GetSettingValue(string settingKey);

  string SetSettingValue(string settingKey, string settingValue);

  int AddSettings(Dictionary<string, string> dictionary);
 }

Simple implementation based on a dictionary object:

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;
  }
 }

You can also write different implementations according to your own needs, such as putting them in the configuration or in your own cache. Here, the application is a single application for the time being, so it is only put in the memory object.

Use #

1. Register the service in Startup:


services.TryAddSingleton<IApplicationSettingService, ApplicationSettingInMemoryService>();

2. Read the configuration information from the database and initialize the configuration data

3. Use on an Razor page, example


@using ActivityReservation.Services
@inject IApplicationSettingService applicationSettings
/*  Omitted here  N  Line code  ... */
<title>@($"{ViewBag.Title} -- {applicationSettings.GetSettingValue("SystemTitle")}") </title>
<meta name="keywords" content="@(applicationSettings.GetSettingValue("SystemKeywords"))" />

Related articles: