2020/01/29, ASP.NET Core 3.1, VS2019
Abstract: Based on ASP.NET Core 3.1 WebApi build back-end multi-layer architecture website [5.1-WebCore site core configuration]
unified package site core configuration, cross-domain policy registered, instantiate algorithm snow late scalable to add multi-language support
This section describes the unified package site core configuration, cross-domain policy registered, instantiate algorithm snow late scalable to add multi-language support
Add Website Profile and cross-domain configuration
In the MS.WebApi
application appsettings.json
, add the following nodes:
"SiteSetting": {
"WorkerId": 1, //for snowflake workerid
"DataCenterId": 1, //for snowflake datacenterid
"LoginFailedCountLimits": 3, //the number of login failed
"LoginLockedTimeout": 3 //(minutes) account locked timeout
},
"Startup": {
"Cors": {
"AllowOrigins": "http://localhost:8080,http://localhost:8081"
}
}
After completion, as shown below:
Description:
- WorkerId, DataCenterId two snowflakes algorithm parameters
- LoginFailedCountLimits user limit the number of login failures
- LoginLockedTimeout user is locked, how long can log in again
- AllowOrigins source is allowed cross-domain separated by a comma
SiteSetting site configuration entity class
In the MS.WebCore
Add Class Library SiteSetting.cs
class:
namespace MS.WebCore
{
public class SiteSetting
{
public long WorkerId { get; set; }
public long DataCenterId { get; set; }
public int LoginFailedCountLimits { get; set; }
public int LoginLockedTimeout { get; set; }
}
}
And wherein the field appsettings.json
corresponding, Paste may be used to paste class json
Add Service Registration
In the MS.WebCore
Add Class Library WebCoreExtensions.cs
class:
using MS.Common.IDCode;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace MS.WebCore
{
public static class WebCoreExtensions
{
public const string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
/// <summary>
/// 添加跨域策略,从appsetting中读取配置
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
/// <returns></returns>
public static IServiceCollection AddCorsPolicy(this IServiceCollection services, IConfiguration configuration)
{
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder =>
{
builder
.WithOrigins(configuration.GetSection("Startup:Cors:AllowOrigins").Value.Split(','))
.AllowAnyHeader()
.AllowAnyMethod();
});
});
return services;
}
/// <summary>
/// 注册WebCore服务,配置网站
/// do other things
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
/// <returns></returns>
public static IServiceCollection AddWebCoreService(this IServiceCollection services, IConfiguration configuration)
{
//绑定appsetting中的SiteSetting
services.Configure<SiteSetting>(configuration.GetSection(nameof(SiteSetting)));
#region 单例化雪花算法
string workIdStr = configuration.GetSection("SiteSetting:WorkerId").Value;
string datacenterIdStr = configuration.GetSection("SiteSetting:DataCenterId").Value;
long workId;
long datacenterId;
try
{
workId = long.Parse(workIdStr);
datacenterId = long.Parse(datacenterIdStr);
}
catch (Exception)
{
throw;
}
IdWorker idWorker = new IdWorker(workId, datacenterId);
services.AddSingleton(idWorker);
#endregion
return services;
}
}
}
Description:
- Encapsulates the cross-domain policy AddCorsPolicy
- Binding appsetting in SiteSetting
- Examples of algorithms IdWorker and snow as a single register embodiment (to ensure that only a single global embodiment)
use
In MS.WebApi
the application Startup.cs
class, ConfigureServices add method in the following code:
//using MS.WebCore;
//添加以上代码至using引用
//注册跨域策略
services.AddCorsPolicy(Configuration);
//注册webcore服务(网站主要配置)
services.AddWebCoreService(Configuration);
After completion of the code is shown below
So far, WebCore core configuration is complete, the project has been the site at this time to support cross-domain configuration, has been read by ioc resolve SiteSetting site configuration and IdWorker generated snowflakes ID
After completion of the project, as shown in FIG.