Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #495

Merged
merged 2 commits into from
Jul 30, 2024
Merged

Dev #495

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions EasilyNET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.Test.Unit", "sample\
EndProject
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{771BAFD8-69AB-4F2E-9FBF-0280E83953BF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasilyNET.AutoDependencyInjection.SourceGenerator", "src\EasilyNET.AutoDependencyInjection.SourceGenerator\EasilyNET.AutoDependencyInjection.SourceGenerator.csproj", "{F8F0840A-001A-4621-80C5-B3735E1337E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi.SourceGenerator.Test", "sample\WebApi.SourceGenerator.Test\WebApi.SourceGenerator.Test.csproj", "{4A011ECD-3C24-4818-8C42-A9361BF4119A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -143,6 +147,14 @@ Global
{771BAFD8-69AB-4F2E-9FBF-0280E83953BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{771BAFD8-69AB-4F2E-9FBF-0280E83953BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{771BAFD8-69AB-4F2E-9FBF-0280E83953BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8F0840A-001A-4621-80C5-B3735E1337E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8F0840A-001A-4621-80C5-B3735E1337E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8F0840A-001A-4621-80C5-B3735E1337E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8F0840A-001A-4621-80C5-B3735E1337E4}.Release|Any CPU.Build.0 = Release|Any CPU
{4A011ECD-3C24-4818-8C42-A9361BF4119A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A011ECD-3C24-4818-8C42-A9361BF4119A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A011ECD-3C24-4818-8C42-A9361BF4119A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A011ECD-3C24-4818-8C42-A9361BF4119A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -168,6 +180,8 @@ Global
{7E493CA2-CB0C-77E0-B442-5ED204493F13} = {8D626EA8-CB54-BC41-363A-217881BEBA6E}
{8F9C18F9-A526-DCA0-597F-90D64AE5C6C2} = {8D626EA8-CB54-BC41-363A-217881BEBA6E}
{9B426136-DC85-603B-94FB-F3C0B2E72713} = {4F9DEAE5-078F-E77A-2E4A-FEB6FFE226FF}
{F8F0840A-001A-4621-80C5-B3735E1337E4} = {E0AD1809-C64F-3D39-45E0-E424261CE16D}
{4A011ECD-3C24-4818-8C42-A9361BF4119A} = {4F9DEAE5-078F-E77A-2E4A-FEB6FFE226FF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BF2C0069-ED43-44A1-A66C-2CC1B62E3EA3}
Expand Down
43 changes: 43 additions & 0 deletions sample/WebApi.SourceGenerator.Test/AppWebModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using EasilyNET.AutoDependencyInjection.Attributes;
using EasilyNET.AutoDependencyInjection.Contexts;
using EasilyNET.AutoDependencyInjection.Modules;
using EasilyNET.WebCore.Handlers;
using WebApi.SourceGenerator.Test.ServiceModules;

// ReSharper disable ClassNeverInstantiated.Global

namespace WebApi.SourceGenerator.Test;

/// <summary>
/// </summary>
[DependsOn(typeof(DependencyAppModule),
typeof(CorsModule),
typeof(ControllersModule),
typeof(SwaggerModule))]
public sealed class AppWebModule : AppModule
{
/// <inheritdoc />
public override void ConfigureServices(ConfigureServicesContext context)
{
base.ConfigureServices(context);
// 添加 ProblemDetails 服务
context.Services.AddProblemDetails();
context.Services.AddExceptionHandler<BusinessExceptionHandler>();
// 添加HttpContextAccessor
context.Services.AddHttpContextAccessor();
}

/// <inheritdoc />
public override void ApplicationInitialization(ApplicationContext context)
{
base.ApplicationInitialization(context);
var app = context.GetApplicationHost() as IApplicationBuilder;
// 全局异常处理中间件
app?.UseExceptionHandler();
app?.UseResponseTime();
// 先认证
app?.UseAuthentication();
// 再授权
app?.UseAuthorization();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;

namespace WebApi.SourceGenerator.Test.Controllers;

/// <summary>
/// </summary>
/// <param name="wfs"></param>
[ApiController, Route("[controller]")]
public class WeatherForecastController(WeatherForecastService wfs) : ControllerBase
{
/// <summary>
/// </summary>
/// <returns></returns>
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get() => wfs.Get();
}
15 changes: 15 additions & 0 deletions sample/WebApi.SourceGenerator.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using WebApi.SourceGenerator.Test;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
// ×Ô¶¯×¢Èë·þÎñÄ£¿é
builder.Services.AddApplicationModules<AppWebModule>();
var app = builder.Build();
if (app.Environment.IsDevelopment()) app.UseDeveloperExceptionPage();

// Ìí¼Ó×Ô¶¯»¯×¢ÈëµÄһЩÖмä¼þ.
app.InitializeApplication();
app.MapControllers();
app.Run();
15 changes: 15 additions & 0 deletions sample/WebApi.SourceGenerator.Test/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5143",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
2 changes: 2 additions & 0 deletions sample/WebApi.SourceGenerator.Test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#### 测试SG

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Text.Json.Serialization;
using EasilyNET.AutoDependencyInjection.Contexts;
using EasilyNET.AutoDependencyInjection.Modules;
using EasilyNET.WebCore.JsonConverters;

// ReSharper disable UnusedType.Local
// ReSharper disable ClassNeverInstantiated.Global

namespace WebApi.SourceGenerator.Test.ServiceModules;

/// <summary>
/// 注册一些控制器的基本内容
/// </summary>
public sealed class ControllersModule : AppModule
{
/// <inheritdoc />
public override void ConfigureServices(ConfigureServicesContext context)
{
context.Services.AddControllers()
.AddJsonOptions(c =>
{
c.JsonSerializerOptions.Converters.Add(new DecimalNullConverter());
c.JsonSerializerOptions.Converters.Add(new IntNullConverter());
c.JsonSerializerOptions.Converters.Add(new BoolNullConverter());
c.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
c.JsonSerializerOptions.Converters.Add(new DateTimeNullConverter());
c.JsonSerializerOptions.Converters.Add(new TimeOnlyJsonConverter());
c.JsonSerializerOptions.Converters.Add(new TimeOnlyNullJsonConverter());
c.JsonSerializerOptions.Converters.Add(new DateOnlyJsonConverter());
c.JsonSerializerOptions.Converters.Add(new DateOnlyNullJsonConverter());
c.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
context.Services.AddEndpointsApiExplorer();
}
}
25 changes: 25 additions & 0 deletions sample/WebApi.SourceGenerator.Test/ServiceModules/CorsModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using EasilyNET.AutoDependencyInjection.Contexts;
using EasilyNET.AutoDependencyInjection.Modules;

namespace WebApi.SourceGenerator.Test.ServiceModules;

/// <summary>
/// 配置跨域服务及中间件
/// </summary>
public sealed class CorsModule : AppModule
{
/// <inheritdoc />
public override void ConfigureServices(ConfigureServicesContext context)
{
var config = context.Services.GetConfiguration();
var allow = config["AllowedHosts"] ?? "*";
context.Services.AddCors(c => c.AddPolicy("AllowedHosts", s => s.WithOrigins(allow.Split(",")).AllowAnyMethod().AllowAnyHeader()));
}

/// <inheritdoc />
public override void ApplicationInitialization(ApplicationContext context)
{
var app = context.GetApplicationHost() as IApplicationBuilder;
app?.UseCors("AllowedHosts");
}
}
65 changes: 65 additions & 0 deletions sample/WebApi.SourceGenerator.Test/ServiceModules/SwaggerModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using EasilyNET.AutoDependencyInjection.Contexts;
using EasilyNET.AutoDependencyInjection.Modules;
using Microsoft.OpenApi.Models;

namespace WebApi.SourceGenerator.Test.ServiceModules;

/// <summary>
/// Swagger文档的配置
/// </summary>
public sealed class SwaggerModule : AppModule
{
/**
* https://github.com/domaindrivendev/Swashbuckle.AspNetCore
*/
private const string name = $"{title}-{version}";

private const string version = "v1";
private const string title = "WebApi.Test";

/// <inheritdoc />
public SwaggerModule()
{
Enable = true;
}

/// <inheritdoc />
public override void ConfigureServices(ConfigureServicesContext context)
{
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
context.Services.AddSwaggerGen(c =>
{
// 配置默认的文档信息
c.SwaggerDoc(name, new()
{
Title = title,
Version = version,
Description = "Console.WriteLine(\"🐂🍺\")"
});
// 这里使用EasilyNET提供的扩展配置.
c.EasilySwaggerGenOptions(name);
// 配置认证方式
c.AddSecurityDefinition("Bearer", new()
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
});
}

/// <inheritdoc />
public override void ApplicationInitialization(ApplicationContext context)
{
var app = context.GetApplicationHost() as IApplicationBuilder;
app?.UseSwagger().UseSwaggerUI(c =>
{
// 配置默认文档
c.SwaggerEndpoint($"/swagger/{name}/swagger.json", $"{title} {version}");
// 使用EasilyNET提供的扩展配置
c.EasilySwaggerUIOptions();
});
}
}
13 changes: 13 additions & 0 deletions sample/WebApi.SourceGenerator.Test/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace WebApi.SourceGenerator.Test;

#pragma warning disable CS1591 // ȱÉÙ¶Ô¹«¹²¿É¼ûÀàÐÍ»ò³ÉÔ±µÄ XML ×¢ÊÍ
public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
32 changes: 32 additions & 0 deletions sample/WebApi.SourceGenerator.Test/WeatherForecastService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using EasilyNET.AutoDependencyInjection.Core.Attributes;

// ReSharper disable ClassNeverInstantiated.Global

namespace WebApi.SourceGenerator.Test;

/// <summary>
/// 天气服务
/// </summary>
[DependencyInjection(ServiceLifetime.Transient)]
public class WeatherForecastService
{
private static readonly string[] Summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];

/// <summary>
/// 获取天气服务
/// </summary>
/// <returns></returns>
// ReSharper disable once MemberCanBeMadeStatic.Global
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EasilyNET.AutoDependencyInjection\EasilyNET.AutoDependencyInjection.csproj" />
<ProjectReference Include="..\..\src\EasilyNET.WebCore.Swagger\EasilyNET.WebCore.Swagger.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions sample/WebApi.SourceGenerator.Test/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
2 changes: 1 addition & 1 deletion sample/WebApi.Test.Unit/WebApi.Test.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="Serilog.Sinks.EventLog" Version="4.0.1-dev-00087" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.Map" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.0.0-dev-00322" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageVersion Include="MessagePack" Version="3.0.111-alpha" />
<PackageVersion Include="MessagePack" Version="3.0.134-beta" />
<PackageVersion Include="MongoDB.Bson" Version="2.28.0" />
<PackageVersion Include="MongoDB.Driver" Version="2.28.0" />
<PackageVersion Include="MongoDB.Driver.Core" Version="2.28.0" />
<PackageVersion Include="MongoDB.Driver.GridFS" Version="2.28.0" />
<PackageVersion Include="RabbitMQ.Client" Version="7.0.0-rc.6" />
<PackageVersion Include="Serilog" Version="4.0.1-dev-02215" />
<PackageVersion Include="Spectre.Console.Json" Version="0.49.2-preview.0.11" />
<PackageVersion Include="Serilog" Version="4.0.2-dev-02220" />
<PackageVersion Include="Spectre.Console.Json" Version="0.49.2-preview.0.13" />
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.6.2" />
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.6.2" />
<!--microsoft asp.net core -->
Expand Down
Loading