日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

.NET6一些常用組件的配置及使用_基礎應用

作者:VictorStar ? 更新時間: 2022-03-10 編程語言

軟件架構

分為模型層,服務層,接口層來做測試使用

0.如何使用IConfiguration、Environment

直接在builder后的主機中使用。

builder.Configuration;
builder.Environment

1.如何使用Swagger

.NET 6 自帶模板已經默認添加Swagger,直接使用即可。

builder.Services.AddSwaggerGen();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

2. 如何添加EFCore到.NET 6中

按照EFCore常規使用方法,申明表的Entity及Dbcontext后,在program.cs文件中添加

builder.Services.AddDbContext<Service.DataContext>(opt => {
    opt.UseSqlServer(builder.Configuration.GetConnectionString("Default"));
});

即可在其他地方注入使用 DataContext

使用Sqlite數據庫,需要引用 Microsoft.EntityFrameworkCore.Sqlite,并在添加服務時,改為

opt.UseSqlite(builder.Configuration.GetConnectionString("Default"));

包管理控制臺數據庫結構生成方法:

  • 使用 add-migration 創建遷移
  • 使用 update-database 更新數據結構

3.如何注入一個服務

builder.Services.AddScoped<UserIdentyService>();

4.如何定義全局的using引用

在根目錄下新建一個 cs文件,比如Globalusing.cs,在里面添加你的全局引用,和常規引用不同的是,在using前面添加 global

global using Service;
global using Entity;
global using Entity.Dto;

5.如何使用Autofac

添加 Nuget 引用

Autofac.Extensions.DependencyInjection

program.cs文件添加autofac的使用和注入配置

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
 {
     Assembly assembly = Assembly.Load("Service.dll");
     builder.RegisterAssemblyTypes(assembly)
            //.AsImplementedInterfaces()// 無接口的注入方式
            .InstancePerDependency();
 });

此時即可構造函數注入使用。

6.如何使用Log4Net

添加引用

Microsoft.Extensions.Logging.Log4Net.AspNetCore

新建配置文件 log4net.config;

添加service配置

//注入Log4Net
builder.Services.AddLogging(cfg =>
{
    //默認的配置文件路徑是在根目錄,且文件名為log4net.config
    //cfg.AddLog4Net();
    //如果文件路徑或名稱有變化,需要重新設置其路徑或名稱
    //比如在項目根目錄下創建一個名為config的文件夾,將log4net.config文件移入其中,并改名為log4net.config
    //則需要使用下面的代碼來進行配置
    cfg.AddLog4Net(new Log4NetProviderOptions()
    {
        Log4NetConfigFileName = "config/log4net.config",
        Watch = true
    });
});

即可在需要的地方定義使用

_logger = LogManager.GetLogger(typeof(UserController));

7.如何使用全局異常過濾器

首先新建 GlobalExceptionFilter 全局異常過濾器,繼承于 ExceptionFilter ,用于接收處理拋出的異常

public class GlobalExceptionFilter : IExceptionFilter
{
    readonly IWebHostEnvironment hostEnvironment;
    readonly ILog logger;
    public GlobalExceptionFilter(IWebHostEnvironment _hostEnvironment)
    {
        this.hostEnvironment = _hostEnvironment;
        this.logger = LogManager.GetLogger(typeof(GlobalExceptionFilter));
    }
    public void OnException(ExceptionContext context)
    {
        if (!context.ExceptionHandled)//如果異常沒有處理
        {
            var result = new ApiResult
            {
                Code = 500,
                IsSuccess = false,
                Message = "服務器發生未處理的異常"
            };

            if (hostEnvironment.IsDevelopment())
            {
                result.Message += "," + context.Exception.Message;
                result.Data = context.Exception.StackTrace;
            }

            logger.Error(result);

            context.Result = new JsonResult(result);
            context.ExceptionHandled = true;//異常已處理
        }
    }
}

然后在Service中添加全局異常過濾器

builder.Services.AddControllers(option =>
    {
        option.Filters.Add<GlobalExceptionFilter>();
    }
);

添加控制器方法完成測試

[HttpGet("exception")]
public ApiResult ExceptionAction()
{
    throw new NotImplementedException();
}

8.如何使用redis做緩存

使用 StackExchange.Redis 作為緩存組件(其他組件類似的使用方式)。nuget 安裝 StackExchange.Redis.Extensions.Core

首先,先建立一個類 RedisClient ,用于管理redis的連接和操作,再建立一個 RedisClientFactory 類,用于創建 redis的連接;

public class RedisClient{...}
public class RedisClientFactory{...}

appsettings.json 中添加redis的配置

"RedisConfig": {
    "Redis_Default": {
      "Connection": "127.0.0.1:6379",
      "InstanceName": "Redis1:"
    },
    "Redis_6": {
      "Connection": "127.0.0.1:6379",
      "DefaultDatabase": 6,
      "InstanceName": "Redis2:"
    }
  }

service中添加 redis客戶端的引用

//添加redis的使用
builder.Services.AddSingleton<RedisClient>(_=> RedisClientFactory.GetInstance(builder.Configuration));

一頓操作后,就可以在你想要使用redis的地方引用了

RedisClient redisClient
...
this.redisDb = redisClient.GetDatabase("Redis_Default");
redisDb.StringSet("clientId", "clientId", TimeSpan.FromSeconds(10));

要使用redis做分布式緩存,先引用 Microsoft.Extensions.Caching.StackExchangeRedis

//將Redis分布式緩存服務添加到服務中
builder.Services.AddStackExchangeRedisCache(options =>
    {
        //用于連接Redis的配置  Configuration.GetConnectionString("RedisConnectionString")讀取配置信息的串
        options.Configuration = "Redis_6";// Configuration.GetConnectionString("RedisConnectionString");
        //Redis實例名RedisDistributedCache
        options.InstanceName = "RedisDistributedCache";
    });

引用自 "分布式 Redis 緩存"

9 如何添加使用定時任務組件

此處使用 Hangfire 定時任務組件,輕便,可持久化,還有面板。

引用 Hangfire 后,即可新增定時任務。

//啟用Hangfire服務.
builder.Services.AddHangfire(x => x.UseStorage(new MemoryStorage()));
builder.Services.AddHangfireServer();

...

//啟用Hangfire面板
app.UseHangfireDashboard();
//開啟一個定時任務
RecurringJob.AddOrUpdate("test",() => Console.WriteLine("Recurring!"), Cron.Minutely());

訪問 https://localhost:7219/hangfire 即可看到任務面板

10. 如何使用業務鎖鎖住下單或者支付操作

首先,做這個事需要能先構建出一個鎖出來,這個鎖有個鎖的標識key,可以根據這個key判定key對應的鎖是否存在,這樣的話,在某個用戶支付或者下單減庫存啥的時候,就可以按照這個key先上鎖,后面有用戶走其他渠道進行同樣的操作的時候,就可以根據是否上鎖了,來判斷操作能否繼續。

比如一個支付訂單的業務,可以在手機上操作,也可以在電腦上操作,這個時候就可以給支付接口上鎖,只要一個支付過程存在著,并且沒有超時,那就不能在其他渠道進行操作。

我們上面已經使用了redis,下面就用redis構建個鎖來模擬這個操作,具體看代碼:

/// <summary>
    /// 測試業務鎖
    /// </summary>
    /// <returns></returns>
    [HttpGet("lockhandle")]
    public async Task<ApiResult> LockHandle(int userId)
    {
        var key = "user";
        var token = $"ID:{userId}";
        try
        {
            if (redisDb.LockTake(key, token, TimeSpan.FromSeconds(50)))
            {
                await Task.Delay(30 * 1000);
                return await Task.FromResult(ApiResult.Success($"ID:{userId} 獲取到鎖了,操作正常,connectId:{Request.HttpContext.Connection.Id}"));
            }
            else
            {
                return await Task.FromResult(ApiResult.Fail($"有正在操作的鎖,connectId:{Request.HttpContext.Connection.Id}"));
            }
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            redisDb.LockRelease(key, token);
        }
    }

11. 如何配置跨域

此處主要記錄全局跨域,不包括指定api跨域。先增加一個配置 "Cors": "http:127.0.0.1:5001",配置可以跨域的url,也可以使用默認跨域配置。
host配置以下服務,按需使用:

builder.Services.AddCors(delegate (CorsOptions options)
{
    options.AddPolicy("CorsPolicy", delegate (CorsPolicyBuilder corsBuilder)
    {
        //指定url跨域
        corsBuilder.WithOrigins(builder.Configuration.GetValue<string>("Cors").Split(','));
        //默認跨域
        corsBuilder.SetIsOriginAllowed((string _) => true).AllowAnyMethod().AllowAnyHeader()
            .AllowCredentials();
    });
});

12. 如何使用NewtonsoftJson

.NET6 默認的系列化庫是內置的 System.Text.Json,使用中如果有諸多不熟悉的地方,那肯定是想換回 NewtonsoftJson,需要nuget 引用 Microsoft.AspNetCore.Mvc.NewtonsoftJson 來配置使用,常用配置包括日期格式、大小寫規則、循環引用配置。。。等,下面是一個配置

builder.Services.AddControllers(option =>
    {
        option.Filters.Add<GlobalExceptionFilter>();
    }
).AddNewtonsoftJson(options =>
{
    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); //序列化時key為駝峰樣式
    options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
    options.SerializerSettings.ReferenceLoopHandling =  ReferenceLoopHandling.Ignore;//忽略循環引用
});

13. 如何使用SignalR

首先添加一個 ChatHub 作為 交互中心處理器

public class ChatHub : Hub
    {
        public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }
    }

在主機中使用服務

builder.Services.AddSignalR();
...
app.UseEndpoints(endpoints =>
{
    endpoints.MapHub<ChatHub>("/chatHub");
});

原文鏈接:https://www.cnblogs.com/Start201505/p/15713345.html

欄目分類
最近更新