.NET6一些常用元件的配置及使用
軟體架構
分為模型層,服務層,介面層來做測試使用
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<Containerhttp://www.cppcns.comBuilder>(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 onResult(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)
{
owww.cppcns.comptions.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"); });
到此這篇關於.NET6一些常用元件的配置及使用的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援我們。