ASP.NETコア多層サイトアーキテクチャを構築[5.1 WebCoreサイトコア構成]

2020年1月29日、ASP.NETコア3.1、VS2019

要約:ASP.NETコア3.1 WEBAPIビルドバックエンド多層アーキテクチャのウェブサイト[5.1-WebCoreサイトコア構成]に基づいて
統一パッケージのサイトのコア構成、クロスドメインポリシー登録、インスタンス化アルゴリズムの雪追加する遅スケーラブルなマルチ言語サポート

記事のディレクトリ

このブランチのプロジェクトコード

このセクションでは、統一されたパッケージのサイトのコア構成を説明し、クロスドメインポリシーは、多言語サポートを追加する後半スケーラブルなインスタンス化アルゴリズムの雪を登録しました

ウェブサイトのプロファイルとクロスドメインの設定を追加

ではMS.WebApiアプリケーションappsettings.json、次のノードを追加します。

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

完了後、次のように:

説明:

  • WorkerId、DataCenterId 2雪のアルゴリズムのパラメータ
  • LoginFailedCountLimitsユーザーがログイン失敗の数を制限します
  • LoginLockedTimeoutユーザーがロックされ、どのくらい再度ログインすることができます
  • AllowOrigins源は、クロスドメインはコンマで区切られた許可され

SiteSettingサイト構成エンティティクラス

MS.WebCore追加クラスライブラリのSiteSetting.csクラス:

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

フィールドは前記とappsettings.json対応する、クラスJSONを貼り付けるために使用することができるペースト

サービス登録を追加

MS.WebCore追加クラスライブラリのWebCoreExtensions.csクラス:

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

説明:

  • クロスドメインポリシーAddCorsPolicyをカプセル化
  • SiteSettingでバインディングのAppSetting
  • 単一のレジスタの実施形態としてのアルゴリズムIdWorkerと雪の例(すなわち、単一のグローバルな実施形態を確保するため)

使用

ではMS.WebApiアプリケーションStartup.csクラス、ConfigureServicesは、次のコードでメソッドを追加します。

//using MS.WebCore;
//添加以上代码至using引用
//注册跨域策略
services.AddCorsPolicy(Configuration);
//注册webcore服务(网站主要配置)
services.AddWebCoreService(Configuration);

コードの完了を以下に示す後

これまでのところ、WebCoreコアの設定が完了し、プロジェクトはクロスドメイン構成をサポートするために、この時点でのサイトとなっている、IOC解決SiteSettingサイト構成によって読み取られ、IdWorkerは雪のIDを生成してきました

プロジェクトの完了後、などに示します。

おすすめ

転載: www.cnblogs.com/kasnti/p/12239496.html