This commit is contained in:
Sanchime 2022-11-27 17:23:54 +08:00
parent b4e44d388d
commit 4aa5174881
18 changed files with 139 additions and 54 deletions

View File

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

View File

@ -0,0 +1,6 @@
namespace Dipper.Alioth.Starlets;
public interface IAsterism
{
void Initialize();
}

View File

@ -0,0 +1,6 @@
namespace Dipper.Alioth.Starlets;
public interface IStarlet
{
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
@ -11,4 +11,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Dipper.Alioth.Starlets\Dipper.Alioth.Starlets.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,11 @@
using Dipper.Alioth.Starlets;
namespace Dipper.Alioth;
public class DomainAsterism : IAsterism
{
public void Initialize()
{
Console.WriteLine("已加载主模块");
}
}

View File

@ -0,0 +1,20 @@
using Dipper.Alioth.Starlets;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
namespace Dipper.Alioth;
public class ManagerStarlet : IStarlet
{
private ApplicationPartManager _partManager;
public ManagerStarlet(ApplicationPartManager partManager)
{
_partManager = partManager;
}
public IEnumerable<string> GetList()
{
return _partManager.ApplicationParts.Select(x => x.Name);
}
}

View File

@ -1,6 +0,0 @@
namespace Dipper.Alioth;
public interface IStarModule
{
void Initialize();
}

View File

@ -1,15 +1,16 @@
using Dipper.Alioth.Starlets;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Dipper.Alioth.Options; namespace Dipper.Alioth.Options;
public class StarOption public class StarOption
{ {
public IList<IStarModule> Modules { get; } public IList<IAsterism> Asterisms { get; }
public Action<MvcOptions>? MvcOption { get; set; } public Action<MvcOptions>? MvcOption { get; set; }
public Action<JsonOptions>? JsonOption { get; set; } public Action<JsonOptions>? JsonOption { get; set; }
public StarOption() public StarOption()
{ {
Modules = new List<IStarModule>(); Asterisms = new List<IAsterism>();
} }
} }

View File

@ -0,0 +1,19 @@
using System.Reflection;
using Dipper.Alioth.Starlets;
using Microsoft.AspNetCore.Mvc.Controllers;
namespace Dipper.Alioth.Providers;
public class ApiFeatureProvider : ControllerFeatureProvider
{
protected override bool IsController(TypeInfo typeInfo)
{
if (!typeof(IStarlet).IsAssignableFrom(typeInfo) ||
!typeInfo.IsPublic ||
typeInfo.IsAbstract ||
typeInfo.IsGenericType)
return false;
return true;
}
}

View File

@ -1,6 +0,0 @@
namespace Dipper.Alioth;
public interface IService
{
}

View File

@ -1,4 +1,5 @@
using System.Text; using System.Text;
using Dipper.Alioth.Starlets;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ActionConstraints; using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.ApplicationModels;
@ -12,7 +13,7 @@ public class ServiceConvention : IApplicationModelConvention
foreach (var controller in application.Controllers) foreach (var controller in application.Controllers)
{ {
var type = controller.ControllerType; var type = controller.ControllerType;
if (typeof(IService).IsAssignableFrom(type)) if (typeof(IStarlet).IsAssignableFrom(type))
{ {
ConfigureServiceExplorer(controller); ConfigureServiceExplorer(controller);
ConfigureSelector(controller); ConfigureSelector(controller);

View File

@ -1,4 +1,6 @@
using Dipper.Alioth.Options; using Dipper.Alioth.Options;
using Dipper.Alioth.Providers;
using Dipper.Alioth.Starlets;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.ApplicationParts;
@ -7,9 +9,9 @@ using Microsoft.Extensions.Hosting;
namespace Dipper.Alioth.Web; namespace Dipper.Alioth.Web;
internal static class ServiceExtension public static class ServiceExtension
{ {
internal static WebApplicationBuilder AddStarApp(this WebApplicationBuilder builder, Action<StarOption> action) public static WebApplicationBuilder AddStarApp(this WebApplicationBuilder builder, Action<StarOption>? action = null)
{ {
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
@ -17,7 +19,7 @@ internal static class ServiceExtension
action?.Invoke(option); action?.Invoke(option);
var mvcBuilder = builder.Services.AddControllers(options => builder.Services.AddControllers(options =>
{ {
options.EnableEndpointRouting = false; options.EnableEndpointRouting = false;
option.MvcOption?.Invoke(options); option.MvcOption?.Invoke(options);
@ -26,38 +28,18 @@ internal static class ServiceExtension
{ {
options.JsonSerializerOptions.PropertyNamingPolicy = null; options.JsonSerializerOptions.PropertyNamingPolicy = null;
option.JsonOption?.Invoke(options); option.JsonOption?.Invoke(options);
}); })
.AddFlexibleApi(option);
builder.Services.AddSwaggerGen(options =>
{
});
AddFlexibleApi(mvcBuilder, option);
builder.Services.AddSession();
return builder; return builder;
} }
internal static WebApplication UserStarApp(this WebApplication app) public static WebApplication UseStarApp(this WebApplication app)
{ {
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
app.UseExceptionHandler("/?m=Error500");
}
app.UseStaticFiles(); app.UseStaticFiles();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseRouting(); app.UseRouting();
app.UseSession();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers(); app.MapControllers();
return app; return app;
@ -68,20 +50,20 @@ internal static class ServiceExtension
/// </summary> /// </summary>
/// <param name="builder"></param> /// <param name="builder"></param>
/// <param name="option"></param> /// <param name="option"></param>
private static void AddFlexibleApi(IMvcBuilder builder, StarOption option) private static void AddFlexibleApi(this IMvcBuilder builder, StarOption option)
{ {
builder.ConfigureApplicationPartManager(manager => builder.ConfigureApplicationPartManager(manager =>
{ {
manager.ApplicationParts.Add(new AssemblyPart(typeof(IService).Assembly)); manager.ApplicationParts.Add(new AssemblyPart(typeof(IStarlet).Assembly));
foreach (var module in option.Modules) foreach (var module in option.Asterisms)
{ {
module.Initialize(); module.Initialize();
var assembly = module.GetType().Assembly; var assembly = module.GetType().Assembly;
manager.ApplicationParts.Add(new AssemblyPart(assembly)); manager.ApplicationParts.Add(new AssemblyPart(assembly));
} }
//manager.FeatureProviders.Add(new ApiFeatureProvider()); manager.FeatureProviders.Add(new ApiFeatureProvider());
}); });
} }
} }

View File

@ -1,4 +1,5 @@
using System.Reflection; using System.Reflection;
using Dipper.Alioth.Starlets;
using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Controllers;
namespace Dipper.Alioth.Web; namespace Dipper.Alioth.Web;
@ -7,7 +8,7 @@ public class ServiceFeatureProvider : ControllerFeatureProvider
{ {
protected override bool IsController(TypeInfo typeInfo) protected override bool IsController(TypeInfo typeInfo)
{ {
return typeof(IService).IsAssignableFrom(typeInfo) && typeInfo.IsPublic return typeof(IStarlet).IsAssignableFrom(typeInfo) && typeInfo.IsPublic
&& !typeInfo.IsAbstract && !typeInfo.IsGenericType; && !typeInfo.IsAbstract && !typeInfo.IsGenericType;
} }
} }

View File

@ -4,6 +4,10 @@ using Microsoft.Extensions.DependencyInjection;
namespace Dipper.Alioth.Web; namespace Dipper.Alioth.Web;
/// <summary>
/// 封装统一主机
/// 暂时不使用
/// </summary>
public class WebStarHost : StarHost public class WebStarHost : StarHost
{ {
private WebStarHost() private WebStarHost()
@ -36,7 +40,7 @@ public class WebStarHost : StarHost
builder.AddStarApp(action); builder.AddStarApp(action);
var app = builder.Build(); var app = builder.Build();
app.UserStarApp(); app.UseStarApp();
return app; return app;
} }

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>

View File

@ -1,8 +1,28 @@
using Dipper.Alioth; using Dipper.Alioth;
using Dipper.Alioth.Web; using Dipper.Alioth.Web;
WebStarHost.CreateWebHost() var builder = WebApplication.CreateBuilder(args);
.Run(args, options =>
{
}); builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.AddStarApp(option =>
{
option.Asterisms.Add(new DomainAsterism());
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseStarApp();
app.Run();

View File

@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dipper.Alioth", "Dipper.Ali
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dipper.Demo", "Dipper.Demo\Dipper.Demo.csproj", "{F075C721-EA36-4B54-B90B-0CC4AA1D641C}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dipper.Demo", "Dipper.Demo\Dipper.Demo.csproj", "{F075C721-EA36-4B54-B90B-0CC4AA1D641C}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dipper.Alioth.Starlets", "Dipper.Alioth.Starlets\Dipper.Alioth.Starlets.csproj", "{2282EA3D-7F55-40AE-84F6-33D5099BC078}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -18,5 +20,9 @@ Global
{F075C721-EA36-4B54-B90B-0CC4AA1D641C}.Debug|Any CPU.Build.0 = Debug|Any CPU {F075C721-EA36-4B54-B90B-0CC4AA1D641C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F075C721-EA36-4B54-B90B-0CC4AA1D641C}.Release|Any CPU.ActiveCfg = Release|Any CPU {F075C721-EA36-4B54-B90B-0CC4AA1D641C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F075C721-EA36-4B54-B90B-0CC4AA1D641C}.Release|Any CPU.Build.0 = Release|Any CPU {F075C721-EA36-4B54-B90B-0CC4AA1D641C}.Release|Any CPU.Build.0 = Release|Any CPU
{2282EA3D-7F55-40AE-84F6-33D5099BC078}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2282EA3D-7F55-40AE-84F6-33D5099BC078}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2282EA3D-7F55-40AE-84F6-33D5099BC078}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2282EA3D-7F55-40AE-84F6-33D5099BC078}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

7
global.json Normal file
View File

@ -0,0 +1,7 @@
{
"sdk": {
"version": "7.0.0",
"rollForward": "latestMajor",
"allowPrerelease": true
}
}