减少program.cs代码冗余问题

master
黄欣 10 months ago
parent 4b9c0cd025
commit 57d83f0189

@ -1,5 +1,5 @@
using Ds.WMS.WebCore; using Ds.WMS.WebCore.appextend;
using DS.Module.Swagger; using Ds.WMS.WebCore.appstartup;
var builder = WebApplication.CreateBuilder(args).UseMyConfiguration(); var builder = WebApplication.CreateBuilder(args).UseMyConfiguration();
@ -15,22 +15,7 @@ builder.Services.AddControllers(options =>
builder.UseDynamicStartups(); builder.UseDynamicStartups();
var app = builder.Build(); var app = builder.Build();
// 中间件
app.UseMyMiddlewares();
app.UseMiddleware<RequestLoggingMiddleware>();
app.UseMiddleware<GlobalExceptionHandler>();
// 启用中间件服务生成 Swagger 作为 JSON 终结点
app.UseSwagger();
// 启用中间件服务对 swagger-ui指定 Swagger JSON 终结点
app.UseSwaggerDocumentation();
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// 顺序放在最后即可
app.UseDynamicStartups();
app.Run(); app.Run();

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#region << 版 本 注 释 >>
/*
* c2024
* CLR 4.0.30319.42000
*
*
*
*
*
*
*/
#endregion << 版 本 注 释 >>
namespace Ds.Module.AppMiddlewareLibrary
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseMyMiddlewares(this IApplicationBuilder app)
{
app.UseMiddleware<RequestLoggingMiddleware>();
app.UseMiddleware<GlobalExceptionHandler>();
app.UseSwagger();
app.UseSwaggerDocumentation();
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseDynamicStartups();
return app;
}
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,48 @@
using Ds.WMS.WebCore.appfilter;
using Ds.WMS.WebCore.applog;
using Ds.WMS.WebCore.appstartup;
using DS.Module.Swagger;
using Microsoft.AspNetCore.Builder;
#region << 版 本 注 释 >>
/*
* c2024
* CLR 4.0.30319.42000
*
*
*
*
*
*
*/
#endregion << 版 本 注 释 >>
namespace Ds.WMS.WebCore.appextend
{
public static class ApplicationBuilderExtensions
{
/// <summary>
///为了让主Progarm.cs文件更加简洁我们将一些中间件的配置放到这里
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static WebApplication UseMyMiddlewares(this WebApplication app)
{
app.UseMiddleware<RequestLoggingMiddleware>();
app.UseMiddleware<GlobalExceptionHandler>();
app.UseSwagger();
app.UseSwaggerDocumentation();
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseDynamicStartups();
return app;
}
}
}

@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
namespace Ds.WMS.WebCore namespace Ds.WMS.WebCore.appfilter
{ {
public static class ActionResultHelper public static class ActionResultHelper
{ {

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Ds.WMS.WebCore namespace Ds.WMS.WebCore.appfilter
{ {
public class CustomObjectResult : ObjectResult public class CustomObjectResult : ObjectResult
{ {

@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.WebUtilities; using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace Ds.WMS.WebCore namespace Ds.WMS.WebCore.appfilter
{ {
public class DsAppActionFilter : ActionFilterAttribute public class DsAppActionFilter : ActionFilterAttribute
{ {

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
namespace Ds.WMS.WebCore namespace Ds.WMS.WebCore.appfilter
{ {
/// <summary> /// <summary>
/// 请求中间件 用于记录请求信息 /// 请求中间件 用于记录请求信息

@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Http;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net; using System.Net;
namespace Ds.WMS.WebCore namespace Ds.WMS.WebCore.applog
{ {
public class GlobalExceptionHandler public class GlobalExceptionHandler
{ {

@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System.Reflection; using System.Reflection;
namespace Ds.WMS.WebCore namespace Ds.WMS.WebCore.appstartup
{ {
/// <summary> /// <summary>
/// ///

@ -1,12 +1,10 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
namespace Ds.WMS.WebCore namespace Ds.WMS.WebCore.appstartup
{ {
public static class WebApplicationFactoryExtensions public static class WebApplicationFactoryExtensions
{ {
public static WebApplicationBuilder UseMyConfiguration(this WebApplicationBuilder builder) public static WebApplicationBuilder UseMyConfiguration(this WebApplicationBuilder builder)
{ {
// 获取应用程序的工作目录 // 获取应用程序的工作目录
@ -27,6 +25,4 @@ namespace Ds.WMS.WebCore
return builder; return builder;
} }
} }
} }

@ -59,7 +59,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ds.WMS.Finance.MediatR", "D
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ds.WMS.WebCore", "Ds.WMS.WebCore\Ds.WMS.WebCore.csproj", "{9FB4A2AF-2414-4258-8B16-287EA3EB098D}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ds.WMS.WebCore", "Ds.WMS.WebCore\Ds.WMS.WebCore.csproj", "{9FB4A2AF-2414-4258-8B16-287EA3EB098D}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ds.WMS.MogonDb", "Ds.WMS.MogonDb\Ds.WMS.MogonDb.csproj", "{47702656-3E62-4477-B72C-ADA39034B5A7}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ds.WMS.MogonDb", "Ds.WMS.MogonDb\Ds.WMS.MogonDb.csproj", "{47702656-3E62-4477-B72C-ADA39034B5A7}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

Loading…
Cancel
Save