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">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@ -11,4 +11,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Dipper.Alioth.Starlets\Dipper.Alioth.Starlets.csproj" />
</ItemGroup>
</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;
namespace Dipper.Alioth.Options;
public class StarOption
{
public IList<IStarModule> Modules { get; }
public IList<IAsterism> Asterisms { get; }
public Action<MvcOptions>? MvcOption { get; set; }
public Action<JsonOptions>? JsonOption { get; set; }
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 Dipper.Alioth.Starlets;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
@ -12,7 +13,7 @@ public class ServiceConvention : IApplicationModelConvention
foreach (var controller in application.Controllers)
{
var type = controller.ControllerType;
if (typeof(IService).IsAssignableFrom(type))
if (typeof(IStarlet).IsAssignableFrom(type))
{
ConfigureServiceExplorer(controller);
ConfigureSelector(controller);

View File

@ -1,4 +1,6 @@
using Dipper.Alioth.Options;
using Dipper.Alioth.Providers;
using Dipper.Alioth.Starlets;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
@ -7,9 +9,9 @@ using Microsoft.Extensions.Hosting;
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>();
@ -17,7 +19,7 @@ internal static class ServiceExtension
action?.Invoke(option);
var mvcBuilder = builder.Services.AddControllers(options =>
builder.Services.AddControllers(options =>
{
options.EnableEndpointRouting = false;
option.MvcOption?.Invoke(options);
@ -26,38 +28,18 @@ internal static class ServiceExtension
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
option.JsonOption?.Invoke(options);
});
builder.Services.AddSwaggerGen(options =>
{
});
AddFlexibleApi(mvcBuilder, option);
builder.Services.AddSession();
})
.AddFlexibleApi(option);
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.UseHttpsRedirection();
app.UseRouting();
app.UseSession();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
return app;
@ -68,20 +50,20 @@ internal static class ServiceExtension
/// </summary>
/// <param name="builder"></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 =>
{
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();
var assembly = module.GetType().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 Dipper.Alioth.Starlets;
using Microsoft.AspNetCore.Mvc.Controllers;
namespace Dipper.Alioth.Web;
@ -7,7 +8,7 @@ public class ServiceFeatureProvider : ControllerFeatureProvider
{
protected override bool IsController(TypeInfo typeInfo)
{
return typeof(IService).IsAssignableFrom(typeInfo) && typeInfo.IsPublic
return typeof(IStarlet).IsAssignableFrom(typeInfo) && typeInfo.IsPublic
&& !typeInfo.IsAbstract && !typeInfo.IsGenericType;
}
}

View File

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

View File

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

View File

@ -1,8 +1,28 @@
using Dipper.Alioth;
using Dipper.Alioth.Web;
WebStarHost.CreateWebHost()
.Run(args, options =>
{
var builder = WebApplication.CreateBuilder(args);
});
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
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dipper.Demo", "Dipper.Demo\Dipper.Demo.csproj", "{F075C721-EA36-4B54-B90B-0CC4AA1D641C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dipper.Alioth.Starlets", "Dipper.Alioth.Starlets\Dipper.Alioth.Starlets.csproj", "{2282EA3D-7F55-40AE-84F6-33D5099BC078}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
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}.Release|Any CPU.ActiveCfg = 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
EndGlobal

7
global.json Normal file
View File

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