|
|
using Microsoft.OpenApi.Models;
|
|
|
using System.Reflection;
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
// Add services to the container.
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
|
//// 注册 SqlSugarClient
|
|
|
builder.Services.AddScoped<SqlSugar.ISqlSugarClient>(provider =>
|
|
|
{
|
|
|
var connectionConfig = new SqlSugar.ConnectionConfig
|
|
|
{
|
|
|
ConnectionString = builder.Configuration.GetConnectionString("YourConnectionStringName"), // 这里替换为你的连接字符串
|
|
|
DbType = SqlSugar.DbType.MySql, // 数据库类型
|
|
|
IsAutoCloseConnection = true, // 自动释放数据务,如果存在事务,在事务结束后释放
|
|
|
InitKeyType = SqlSugar.InitKeyType.Attribute // 从实体特性中读取主键自增列信息
|
|
|
};
|
|
|
var sqlSugarClient = new SqlSugar.SqlSugarClient(connectionConfig);
|
|
|
return sqlSugarClient;
|
|
|
});
|
|
|
// 注册 MediatR 和请求处理器
|
|
|
var assembly = AppDomain.CurrentDomain.Load("Ds.WMS.Finance.MediatR");
|
|
|
builder.Services.AddMediatR(c =>
|
|
|
{
|
|
|
c.RegisterServicesFromAssembly(assembly);
|
|
|
});
|
|
|
builder.Services.AddSwaggerGen(c =>
|
|
|
{
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
|
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
|
c.IncludeXmlComments(xmlPath);
|
|
|
});
|
|
|
|
|
|
//builder.Services.AddMediatR(Assembly.GetExecutingAssembly());
|
|
|
var app = builder.Build();
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
//if (app.Environment.IsDevelopment())
|
|
|
//{
|
|
|
// app.UseSwagger();
|
|
|
// app.UseSwaggerUI();
|
|
|
//}
|
|
|
// 启用中间件服务生成 Swagger 作为 JSON 终结点
|
|
|
app.UseSwagger();
|
|
|
|
|
|
// 启用中间件服务对 swagger-ui,指定 Swagger JSON 终结点
|
|
|
app.UseSwaggerUI(c =>
|
|
|
{
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
|
|
|
});
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
app.UseRouting();
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
{
|
|
|
endpoints.MapControllers();
|
|
|
});
|
|
|
|
|
|
app.Run();
|
|
|
|