From a30c3b20be2d8287f60cb86f4cb7590adbba8ac2 Mon Sep 17 00:00:00 2001 From: Nice <2017875139@qq.com> Date: Fri, 24 Sep 2021 14:14:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0TestApi=20=E4=BF=AE=E5=A4=8Da?= =?UTF-8?q?ssemblies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WeatherForecastController.cs | 34 +++++++++++ Gear.Test.Api/Gear.Test.Api.csproj | 15 +++++ Gear.Test.Api/Program.cs | 26 +++++++++ Gear.Test.Api/Properties/launchSettings.json | 31 ++++++++++ Gear.Test.Api/Startup.cs | 57 +++++++++++++++++++ Gear.Test.Api/WeatherForecast.cs | 15 +++++ Gear.Test.Api/appsettings.Development.json | 9 +++ Gear.Test.Api/appsettings.json | 10 ++++ Gear.sln | 6 ++ .../ServiceCollectionServiceExtensions.cs | 7 ++- 10 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 Gear.Test.Api/Controllers/WeatherForecastController.cs create mode 100644 Gear.Test.Api/Gear.Test.Api.csproj create mode 100644 Gear.Test.Api/Program.cs create mode 100644 Gear.Test.Api/Properties/launchSettings.json create mode 100644 Gear.Test.Api/Startup.cs create mode 100644 Gear.Test.Api/WeatherForecast.cs create mode 100644 Gear.Test.Api/appsettings.Development.json create mode 100644 Gear.Test.Api/appsettings.json diff --git a/Gear.Test.Api/Controllers/WeatherForecastController.cs b/Gear.Test.Api/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..239fec7 --- /dev/null +++ b/Gear.Test.Api/Controllers/WeatherForecastController.cs @@ -0,0 +1,34 @@ +using Gear.Abstractions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Gear.Test.Api.Controllers +{ + + public class WeatherForecastController :AppService + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + + public IEnumerable Get() + { + var d = LazyServiceProvider; + var c = Configuration; + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/Gear.Test.Api/Gear.Test.Api.csproj b/Gear.Test.Api/Gear.Test.Api.csproj new file mode 100644 index 0000000..f6d144f --- /dev/null +++ b/Gear.Test.Api/Gear.Test.Api.csproj @@ -0,0 +1,15 @@ + + + + net5.0 + + + + + + + + + + + diff --git a/Gear.Test.Api/Program.cs b/Gear.Test.Api/Program.cs new file mode 100644 index 0000000..d9f843e --- /dev/null +++ b/Gear.Test.Api/Program.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Gear.Test.Api +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/Gear.Test.Api/Properties/launchSettings.json b/Gear.Test.Api/Properties/launchSettings.json new file mode 100644 index 0000000..a8ff201 --- /dev/null +++ b/Gear.Test.Api/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:44378", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Gear.Test.Api": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Gear.Test.Api/Startup.cs b/Gear.Test.Api/Startup.cs new file mode 100644 index 0000000..aca2000 --- /dev/null +++ b/Gear.Test.Api/Startup.cs @@ -0,0 +1,57 @@ +using Gear.Infrastructure.Extensions; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.OpenApi.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Gear.Test.Api +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddGear(); + services.AddDynamicWebApi(); + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "Gear.Test.Api", Version = "v1" }); + }); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Gear.Test.Api v1")); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/Gear.Test.Api/WeatherForecast.cs b/Gear.Test.Api/WeatherForecast.cs new file mode 100644 index 0000000..da90192 --- /dev/null +++ b/Gear.Test.Api/WeatherForecast.cs @@ -0,0 +1,15 @@ +using System; + +namespace Gear.Test.Api +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git a/Gear.Test.Api/appsettings.Development.json b/Gear.Test.Api/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/Gear.Test.Api/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/Gear.Test.Api/appsettings.json b/Gear.Test.Api/appsettings.json new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ b/Gear.Test.Api/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/Gear.sln b/Gear.sln index 15eca23..7643ef7 100644 --- a/Gear.sln +++ b/Gear.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.31605.320 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gear", "Gear\Gear.csproj", "{E98AD43A-28B5-4046-805E-313EF48D9AEE}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gear.Test.Api", "Gear.Test.Api\Gear.Test.Api.csproj", "{D8E0FF08-B8FA-45AE-B20A-6393B1DBCF18}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {E98AD43A-28B5-4046-805E-313EF48D9AEE}.Debug|Any CPU.Build.0 = Debug|Any CPU {E98AD43A-28B5-4046-805E-313EF48D9AEE}.Release|Any CPU.ActiveCfg = Release|Any CPU {E98AD43A-28B5-4046-805E-313EF48D9AEE}.Release|Any CPU.Build.0 = Release|Any CPU + {D8E0FF08-B8FA-45AE-B20A-6393B1DBCF18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D8E0FF08-B8FA-45AE-B20A-6393B1DBCF18}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D8E0FF08-B8FA-45AE-B20A-6393B1DBCF18}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D8E0FF08-B8FA-45AE-B20A-6393B1DBCF18}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs b/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs index 8dc3dcd..44228de 100644 --- a/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs +++ b/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs @@ -34,7 +34,12 @@ namespace Gear.Infrastructure.Extensions !lib.Serviceable && lib.Type != "package" && lib.Type != "referenceassembly"). - Select(lib => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name))); + Select(lib => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name))).ToList(); + + if (!assemblies.Contains(typeof(ServiceCollectionServiceExtensions).Assembly)) + { + assemblies.Add(typeof(ServiceCollectionServiceExtensions).Assembly); + } foreach (var assembly in assemblies) {