# NET6App **Repository Path**: qq356179407/net6-app ## Basic Information - **Project Name**: NET6App - **Description**: .NET 6的CoreApp框架,用来学习.NET6的一些变动和新特性,使用EFCore,等一系列组件的运用,每个用单独的文档篇章记录,如果对你有帮助,请给点个赞,会持续更新文档哦。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 11 - **Created**: 2021-12-16 - **Last Updated**: 2022-05-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # NET6App #### 介绍 .NET 6的CoreApp框架,用来学习.NET6的一些变动和新特性,使用EFCore,等一系列组件的运用,每个用单独的文档篇章记录,持续更新文档哦。 如果对您有帮助,点击右上角⭐Star⭐关注 ,感谢支持开源! #### 软件架构 分为模型层,服务层,接口层来做测试使用 ### 0.如何使用IConfiguration、Environment 直接在builder后的主机中使用。 ```c# builder.Configuration; builder.Environment ``` ### 1.如何使用Swagger .NET 6 自带模板已经默认添加Swagger,直接使用即可。 ```c# builder.Services.AddSwaggerGen(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } ``` ### 2. 如何添加EFCore到.NET 6中 按照EFCore常规使用方法,申明表的Entity及Dbcontext后,在program.cs文件中添加 ```c# builder.Services.AddDbContext(opt => { opt.UseSqlServer(builder.Configuration.GetConnectionString("Default")); }); ``` 即可在其他地方注入使用 DataContext 使用Sqlite数据库,需要引用 Microsoft.EntityFrameworkCore.Sqlite, 并在添加服务时,改为 ```C# opt.UseSqlite(builder.Configuration.GetConnectionString("Default")); ``` ### 3.如何注入一个服务 ```c# builder.Services.AddScoped(); ``` ### 4.如何定义全局的using引用 在根目录下新建一个 cs文件,比如Globalusing.cs,在里面添加你的全局引用,和常规引用不同的是,在using前面添加 global ```c# global using Service; global using Entity; global using Entity.Dto; ``` ### 5.如何使用Autofac 添加 Nuget 引用 ```c# Autofac.Extensions.DependencyInjection ``` program.cs文件添加autofac的使用和注入配置 ```c# builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.ConfigureContainer(builder => { Assembly assembly = Assembly.Load("Service.dll"); builder.RegisterAssemblyTypes(assembly) //.AsImplementedInterfaces()// 无接口的注入方式 .InstancePerDependency(); }); ``` 此时即可构造函数注入使用。 ### 6.如何使用Log4Net 添加引用 ```c# Microsoft.Extensions.Logging.Log4Net.AspNetCore ``` 新建配置文件 log4net.config; 添加service配置 ```c# //注入Log4Net builder.Services.AddLogging(cfg => { //默认的配置文件路径是在根目录,且文件名为log4net.config //cfg.AddLog4Net(); //如果文件路径或名称有变化,需要重新设置其路径或名称 //比如在项目根目录下创建一个名为config的文件夹,将log4net.config文件移入其中,并改名为log4net.config //则需要使用下面的代码来进行配置 cfg.AddLog4Net(new Log4NetProviderOptions() { Log4NetConfigFileName = "config/log4net.config", Watch = true }); }); ``` 即可在需要的地方定义使用 ```C# _logger = LogManager.GetLogger(typeof(UserController)); ``` ### 7.如何使用全局异常过滤器 首先新建 GlobalExceptionFilter 全局异常过滤器,继承于 ExceptionFilter ,用于接收处理抛出的异常 ```C# 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中添加全局异常过滤器 ```C# builder.Services.AddControllers(option => { option.Filters.Add(); } ); ``` 添加控制器方法完成测试 ```C# [HttpGet("exception")] public ApiResult ExceptionAction() { throw new NotImplementedException(); } ``` ### 8.如何使用redis做缓存 使用 StackExchange.Redis 作为缓存组件(其他组件类似的使用方式)。nuget 安装 StackExchange.Redis.Extensions.Core 首先,先建立一个类 RedisClient ,用于管理redis的连接和操作,再建立一个 RedisClientFactory 类,用于创建 redis的连接; ```c# public class RedisClient{...} public class RedisClientFactory{...} ``` appsettings.json 中添加redis的配置 ```Configure "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客户端的引用 ```C# //添加redis的使用 builder.Services.AddSingleton(_=> RedisClientFactory.GetInstance(builder.Configuration)); ``` 一顿操作后,就可以在你想要使用redis的地方引用了 ```c# RedisClient redisClient ... this.redisDb = redisClient.GetDatabase("Redis_Default"); redisDb.StringSet("clientId", "clientId", TimeSpan.FromSeconds(10)); ``` 要使用redis做分布式缓存,先引用 Microsoft.Extensions.Caching.StackExchangeRedis ```c# //将Redis分布式缓存服务添加到服务中 builder.Services.AddStackExchangeRedisCache(options => { //用于连接Redis的配置 Configuration.GetConnectionString("RedisConnectionString")读取配置信息的串 options.Configuration = "Redis_6";// Configuration.GetConnectionString("RedisConnectionString"); //Redis实例名RedisDistributedCache options.InstanceName = "RedisDistributedCache"; }); ``` >引用自 ["分布式 Redis 缓存"](https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/distributed?view=aspnetcore-6.0) ### 9 如何添加使用定时任务组件 此处使用 Hangfire 定时任务组件,轻便,可持久化,还有面板。 引用 Hangfire 后,即可新增定时任务。 ```c# //启用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 如何使用业务锁锁住下单或者支付操作