Implementation of Application-like Services in asp.net core

Time:2019-10-3

Intro#

In asp. net, we can use application to save some global variables on the server side, such as the number of people on the server side online at the same time, such as the configuration information of some websites.

In ASP.NET application, the previously developed activity room reservation system loads keyword and Title information from the database and saves them to the application when the website starts. It uses the application directly to get the information where it is needed. After updating the configuration in the background, it updates the application variable so that the website can be updated without restarting the website. Configuration information, using 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, it can’t be used directly. So I realized a service to load the configuration information of the website. It’s simple and crude. It’s right to throw a brick to attract jade, don’t like it, don’t spray it.

Custom Application Service IApplication Setting Service#

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 in the configuration or in your own cache, where the application is temporarily a single application, so it is only placed in the memory object.

Use of Lei

1. Register services in Startup:


services.TryAddSingleton<IApplicationSettingService, ApplicationSettingInMemoryService>();

2. Read configuration information from database and initialize configuration data

3. On Razor pages, examples

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

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.