robyle / NetPro

🌈An enhanced version of asp.netcore,Support for netcore3.1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NetPro

.NET Core NuGet

🕰️ 项目请参照

简要

NetPro项目封装常用组件和初始配置,为快速开发webapi,守护进程,windwos服务提供基础模板,支持.NetCore3.1,支持.Net5 Preview

主要组件:

FreeSql,Autofac , Automapper,apollo,App.Metrics,

CsRedisCore,StackExchange.Redis,Serilog,

MiniProfiler,FluentValidation,IdGen,

MongoDb,Dapper,RedLock.Net,

Sentry,RabbitMQ.Client,SkyAPM,

Swagger,WebApiClient.Core,

TimeZoneConverter,healthcheck

exceptionless

使用

NetPro.Web.Api组件打包封装了其他所有组件,"开箱即用",各组件已发布Nuget包,也可单独使用,建议直接使用NetPro.Web.Api省去各种初始化与避免配置有误导致的问题
包含的内置组件

具体参考sample/Leon.XXX.Api(分层)项目

  • webapi项目引用 NetPro.Web.Api NuGet 引用最新nuget即可

Package Manager方式: Install-Package NetPro.Web.Api -Version 3.1.2

.NET CLI 方式: dotnet add package NetPro.Web.Api --version 3.1.2

PackageReference:<PackageReference Include="NetPro.Web.Api" Version="3.1.2" />

.NET CLI 方式: paket add NetPro.Web.Api --version 3.1.2

  • 修改Program.cs
public class Program
{
/// <summary>
/// 
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
	CreateHostBuilder(args).Build().Run();
}

/// <summary>
/// 
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public static IHostBuilder CreateHostBuilder(string[] args) =>
	Host.CreateDefaultBuilder(args)
	.UseServiceProviderFactory(new AutofacServiceProviderFactory())
	.ConfigureAppConfiguration((hostingContext, config) => ApolloClientHelper.ApolloConfi	(hostingContext, config, args))
	.ConfigureWebHostDefaults(webBuilder =>
	{
		webBuilder.UseStartup<Startup>();
	}).UseSerilog();
	}
  • 修改 Startup.cs
public class Startup
{
 #region Fields

 private readonly IConfiguration _configuration;
 private readonly IWebHostEnvironment _webHostEnvironment;
 private IEngine _engine;
 private NetProOtion _NetProOtion;

 #endregion

 #region Ctor

 /// <summary>
 /// 
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="webHostEnvironment"></param>
 public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
 {
 	_configuration = configuration;
 	_webHostEnvironment = webHostEnvironment;
 }

 #endregion

 // This method gets called by the runtime. Use this method to add services to the  container.
 /// <summary>
 /// 
 /// </summary>
 /// <param name="services"></param>
 public void ConfigureServices(IServiceCollection services)
 {
 	(_engine, _NetProOtion) = services.ConfigureApplicationServices(_configuration, _webHostEnvironment);
 }

 /// <summary>
 /// 
 /// </summary>
 /// <param name="builder"></param>
 public void ConfigureContainer(ContainerBuilder builder)
 {
 	_engine.RegisterDependencies(builder, _NetProOtion);
 }
 
 // This method gets called by the runtime. Use this method to configure the HTTP request  pipeline.
 /// <summary>
 /// 
 /// </summary>
 /// <param name="app"></param>
 /// <param name="env"></param>
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 {
 	app.ConfigureRequestPipeline();
 }
}
  • 为了Startup文件干净清爽,建议创建ApiStartup.cs文件

此文件继承INetProStartup接口,提供了microsoft原生依赖注入能力,所有组件注入放于此 ,Startup.cs将不接受组件注入

  • 修改appsettings.json 文件
{	
	//数据库ORM建议使用FreeSql,为了便于灵活选择使用适合自己的ORM,框架已剔除内置的NetPro.Dapper
	//apollo配置
	"Apollo": {
		"Enabled": false,
		"AppId": "Leon",
		"MetaServer": "http://192.168.56.98:7078",
		"Cluster": "default",
		"Namespaces": "AppSetting,MicroServicesEndpoint",
		"RefreshInterval": 300000,
		"LocalCacheDir": "apollo/data"
	},
	//响应缓存配置,建议不大于3秒
	"ResponseCacheOption": {
		"Enabled": true,
		"Duration": 3,
		"IgnoreVaryQuery": [ "sign", "timestamp" ]
	},
	//日志配置
	"Serilog": {
		"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Async", "Serilog.Sinks.File" ],
		"MinimumLevel": {
			"Default": "Information",
			"Override": {
				"Microsoft": "Debug",
				"System": "Debug",
				"System.Net.Http.HttpClient": "Debug"
			}
		},
		"WriteTo:Async": {
			"Name": "Async",
			"Args": {
				"configure": [
					{ "Name": "Console" }
				]
			}
		},
		"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
		"Properties": {
			"Application": "Netpro"
		}
	},

	"AllowedHosts": "*",
	//框架核心配置
	"NetProOption": {
		"ProjectPrefix": "Leon",
		"ProjectSuffix": "",
		"UseResponseCompression": false,
		"ThreadMinCount": 5,
		"ApplicationName": "",
		"RequestWarningThreshold": 5
	},
	//接口签名防篡改配置
	"VerifySignOption": {		
		"Enable": true,
		"IsDarkTheme":true,
		"IsDebug": false,
		"IsForce": false, //是否强制签名
		"Scheme": "attribute", //attribute;global
		"ExpireSeconds": 60,
		"CommonParameters": {
			"TimestampName": "timestamp",
			"AppIdName": "appid",
			"SignName": "sign"
		},
		"AppSecret": {
			"AppId": {
				"sadfsdf": "sdfsfd"
			}
		},
		"IgnoreRoute": [ "api/ignore/", "" ]
	},
	//swagger配置
	"SwaggerOption": {
		"Enable": true,
		"IsDarkTheme":true,//Swagger黑色主题
		"MiniProfilerEnabled": false,
		"XmlComments": [ "", "" ],
		"RoutePrefix": "swagger",
		"Description": "this is swagger for netcore",
		"Title": "Demo swagger",
		"Version": "first version",
		"TermsOfService": "netcore.com",
		"Contact": {
			"Email": "swagger@netcore.com",
			"Name": "swagger",
			"Url": "swagger@netcore.com"
		},
		"License": {
			"Name": "",
			"Url": ""
		},
		"Headers": [ //swagger默认公共头参数
			{
				"Name": "User",
				"Description": "用户"
			}
		], 
		"Query": [ //swagger默认url公共参数
			{
				"Name": "sign",
				"Description": "签名"
			},
			{
				"Name": "timestamp",
				"Description": "客户端时间戳"
			}
		]
	},
	//中间件健康检查配置
	"HealthChecksUI": {
		"HealthChecks": [
			{
				"Name": "HealthList",
				"Uri": "/health"
			}
		],
		"Webhooks": [],
		"EvaluationTimeOnSeconds": 3600, //检查周期,单位秒
		"MinimumSecondsBetweenFailureNotifications": 60
	},

	"Hosting": {
		"ForwardedHttpHeader": "",
		"UseHttpClusterHttps": false,
		"UseHttpXForwardedProto": false
	},
	//redis配置
	"RedisCacheOption": {
		"Enabled": true,
		"RedisComponent": 1,
		"Password": "netpro",
		"IsSsl": false,
		"Preheat": 20,
		"Cluster": true, //集群模式
		"ConnectionTimeout": 20,
		"Endpoints": [
			{
				"Port": 6379,
				"Host": "192.168.7.66"
			}
		],
		"Database": 0,
		"DefaultCustomKey": "NetPro:",//key前缀
		"PoolSize": 50
	},
	//跨服务访问配置
	"MicroServicesEndpoint": {
		"Example": "http://localhost:5000",
		"Baidu": ""
	},
	//mongodb配置
	"MongoDbOptions": {
		"Enabled": false,
		"ConnectionString": null,
		"Database": -1
	},
	//rabbitmq配置
	"RabbitMq": {
		"HostName": "127.0.0.1",
		"Port": "5672",
		"UserName": "guest",
		"Password": "guest"
	},
	"RabbitMqExchange": {
		"Type": "direct",
		"Durable": true,
		"AutoDelete": false,
		"DeadLetterExchange": "default.dlx.exchange",
		"RequeueFailedMessages": true,
		"Queues": [
			{
				"Name": "myqueue",
				"RoutingKeys": [ "routing.key" ]
			}
		]
	}
}

  • Controller使用

Controller继承ApiControllerBase抽象类提供统一响应和简化其他操作,如果不需要默认提供的响应格式也可直接继承ControllerBase

	/// <summary>
	///
	/// </summary>
	[Route("api/v1/[controller]")]
	public class WeatherForecastController : ApiControllerBase
	{
		private readonly ILogger _logger;
		private IExampleProxy _userApi { get; set; }

		public WeatherForecastController(ILogger logger
			 ,IExampleProxy userApi)
		{
			_logger = logger;
			_userApi = userApi;
		}
	}

约定

以下后缀结尾的类将自动DI注入
  • Service 业务相关
  • Repository 数据仓储相关(需要直接在Service或Controller中直接操作数据库可无需使用此后缀)
  • Proxy 代理请求相关(请求远程接口使用)
  • Aggregate 聚合相关,当Service 或者Controller 业务逻辑繁琐复杂可在此聚合后再调用

发布

发布自包含应用
dotnet publish -r linux-x64 -c release /p:PublishSingleFile=true /p:PublishTrimmed=true
依赖CLR运行时应用
dotnet publish -r linux-x64 -c release

运行

开发环境运行后效果如下:

          ____  _____        _   _______
        |_   \|_   _|      / |_|_   __ \
          |   \ | |  .---.`| |-' | |__) |_ .--.   .--.
          | |\ \| | / /__\\| |   |  ___/[ `/'`\]/ .'`\ \
         _| |_\   |_| \__.,| |, _| |_    | |    | \__. |
        |_____|\____|'.__.'\__/|_____|  [___]    '.__.'


[20:20:34 Development] dotnet process id:349824
服务注入顺序:
+-------+------------------------+-------------------------------------------------------+
| Order | StartUpName            | Path                                                  |
+-------+------------------------+-------------------------------------------------------+
| 0     | ErrorHandlerStartup    | NetPro.Web.Core.Infrastructure.ErrorHandlerStartup    |
+-------+------------------------+-------------------------------------------------------+
| 100   | NetProCommonStartup    | NetPro.Web.Core.Infrastructure.NetProCommonStartup    |
+-------+------------------------+-------------------------------------------------------+
| 100   | NetProRateLimitStartup | NetPro.Web.Core.Infrastructure.NetProRateLimitStartup |
+-------+------------------------+-------------------------------------------------------+
| 105   | ShareBodyStartup101    | NetPro.Web.Core.Infrastructure.ShareBodyStartup101    |
+-------+------------------------+-------------------------------------------------------+
| 110   | SignStartup            | NetPro.Web.Core.Infrastructure.SignStartup            |
+-------+------------------------+-------------------------------------------------------+
| 115   | ResponseCacheStartup   | NetPro.Web.Core.Infrastructure.ResponseCacheStartup   |
+-------+------------------------+-------------------------------------------------------+
| 120   | NetProApiStartup       | NetPro.Web.Api.NetProApiStartup                       |
+-------+------------------------+-------------------------------------------------------+
| 500   | AuthenticationStartup  | NetPro.Web.Core.Infrastructure.AuthenticationStartup  |
+-------+------------------------+-------------------------------------------------------+
| 900   | ApiStartup             | Leon.XXX.Api.ApiStartup                               |
+-------+------------------------+-------------------------------------------------------+
| 900   | XXXApiProxyStartup     | Leon.XXX.Proxy.XXXApiProxyStartup                     |
+-------+------------------------+-------------------------------------------------------+
| 1000  | NetProCoreStartup      | NetPro.Web.Core.Infrastructure.NetProCoreStartup      |
+-------+------------------------+-------------------------------------------------------+
| 2000  | ApiProxyStartup        | NetPro.Web.Api.ApiProxyStartup                        |
+-------+------------------------+-------------------------------------------------------+

核心数为:8--默认线程最小为:40--Available:32767
[20:20:51 DBG] Hosting starting
[20:20:51 DBG] Failed to locate the development https certificate at 'null'.
[20:20:51 INF] Now listening on: http://localhost:5001
[20:20:51 DBG] Loaded hosting startup assembly Leon.XXX.Api
[20:20:51 INF] Application started. Press Ctrl+C to shut down.
[20:20:51 INF] Hosting environment: Development
[20:20:51 INF] Content root path: G:\vsFile\netproFile\NetPro\src\sample\Leon.XXX.Api
[20:20:51 DBG] Hosting started

Swagger地址:/swagger/index.html

健康检查地址 /health

健康检查面板/ui

应用信息 /info

环境信息 /env

问题汇总

1. 如何覆盖系统异常处理

var mvcBuilder = services.AddControllers(config =>
   {
    config.Filters.Add(typeof(CustomerExceptionFilter),2);//自定义全局异常过滤器//100是order值,越大越靠后加载
});

...

Target

...

About

🌈An enhanced version of asp.netcore,Support for netcore3.1


Languages

Language:C# 99.0%Language:HTML 0.8%Language:Dockerfile 0.1%Language:CSS 0.1%Language:PowerShell 0.1%