From 3613d4f3ce51a6ab5c20ab59874b731b8d88b963 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Sat, 26 Dec 2020 22:17:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0VS=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XUnitDIStudy.Model/Student.cs | 13 ++++ XUnitDIStudy.Model/XUnitDIStudy.Model.csproj | 7 ++ XUnitDIStudy.Service/IStudentService.cs | 12 +++ XUnitDIStudy.Service/StudentService.cs | 21 ++++++ .../XUnitDIStudy.Service.csproj | 11 +++ XUnitDIStudy.Test/Startup.cs | 73 +++++++++++++++++++ XUnitDIStudy.Test/UseXUnit.cs | 15 ++++ XUnitDIStudy.Test/XUnitDIStudy.Test.csproj | 30 ++++++++ .../Controllers/DefaultController.cs | 49 +++++++++++++ XUnitDIStudy.WebApp/Program.cs | 27 +++++++ .../Properties/launchSettings.json | 31 ++++++++ XUnitDIStudy.WebApp/Startup.cs | 65 +++++++++++++++++ .../XUnitDIStudy.WebApp.csproj | 17 +++++ .../appsettings.Development.json | 9 +++ XUnitDIStudy.WebApp/appsettings.json | 11 +++ XUnitDIStudy.sln | 43 +++++++++++ 16 files changed, 434 insertions(+) create mode 100644 XUnitDIStudy.Model/Student.cs create mode 100644 XUnitDIStudy.Model/XUnitDIStudy.Model.csproj create mode 100644 XUnitDIStudy.Service/IStudentService.cs create mode 100644 XUnitDIStudy.Service/StudentService.cs create mode 100644 XUnitDIStudy.Service/XUnitDIStudy.Service.csproj create mode 100644 XUnitDIStudy.Test/Startup.cs create mode 100644 XUnitDIStudy.Test/UseXUnit.cs create mode 100644 XUnitDIStudy.Test/XUnitDIStudy.Test.csproj create mode 100644 XUnitDIStudy.WebApp/Controllers/DefaultController.cs create mode 100644 XUnitDIStudy.WebApp/Program.cs create mode 100644 XUnitDIStudy.WebApp/Properties/launchSettings.json create mode 100644 XUnitDIStudy.WebApp/Startup.cs create mode 100644 XUnitDIStudy.WebApp/XUnitDIStudy.WebApp.csproj create mode 100644 XUnitDIStudy.WebApp/appsettings.Development.json create mode 100644 XUnitDIStudy.WebApp/appsettings.json create mode 100644 XUnitDIStudy.sln diff --git a/XUnitDIStudy.Model/Student.cs b/XUnitDIStudy.Model/Student.cs new file mode 100644 index 0000000..1a47c94 --- /dev/null +++ b/XUnitDIStudy.Model/Student.cs @@ -0,0 +1,13 @@ +using System; + +namespace XUnitDIStudy.Model +{ + public class Student + { + public int Id { get; set; } + + public string Name { get; set; } + + public int Age { get; set; } + } +} diff --git a/XUnitDIStudy.Model/XUnitDIStudy.Model.csproj b/XUnitDIStudy.Model/XUnitDIStudy.Model.csproj new file mode 100644 index 0000000..9f5c4f4 --- /dev/null +++ b/XUnitDIStudy.Model/XUnitDIStudy.Model.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/XUnitDIStudy.Service/IStudentService.cs b/XUnitDIStudy.Service/IStudentService.cs new file mode 100644 index 0000000..bddc1f4 --- /dev/null +++ b/XUnitDIStudy.Service/IStudentService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; + +using XUnitDIStudy.Model; + +namespace XUnitDIStudy.Service +{ + public interface IStudentService + { + List GetAll(); + } +} diff --git a/XUnitDIStudy.Service/StudentService.cs b/XUnitDIStudy.Service/StudentService.cs new file mode 100644 index 0000000..eb32cec --- /dev/null +++ b/XUnitDIStudy.Service/StudentService.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; + +using XUnitDIStudy.Model; + +namespace XUnitDIStudy.Service +{ + public class StudentService:IStudentService + { + public List GetAll() + { + return new List() + { + new Student(){Id=1,Name="乔峰",Age=18 }, + new Student(){Id=1,Name="段誉",Age=16 }, + new Student(){Id=1,Name="阿紫",Age=20 }, + new Student(){Id=1,Name="阿娇",Age=15 }, + }; + } + } +} diff --git a/XUnitDIStudy.Service/XUnitDIStudy.Service.csproj b/XUnitDIStudy.Service/XUnitDIStudy.Service.csproj new file mode 100644 index 0000000..c89644f --- /dev/null +++ b/XUnitDIStudy.Service/XUnitDIStudy.Service.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + diff --git a/XUnitDIStudy.Test/Startup.cs b/XUnitDIStudy.Test/Startup.cs new file mode 100644 index 0000000..8cc5433 --- /dev/null +++ b/XUnitDIStudy.Test/Startup.cs @@ -0,0 +1,73 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +using Microsoft.AspNetCore.TestHost; + +using System; +using System.Collections.Generic; + +using XUnitDIStudy.Service; + +namespace XUnitDIStudy.Test +{ + public class Startup + { + /// + /// 自定义 host 构建 + /// + /// + public void ConfigureHost(IHostBuilder hostBuilder) + { + hostBuilder + .ConfigureWebHost(config => + { + config + .UseTestServer() + .UseStartup(); + }) + .ConfigureAppConfiguration(builder => + { + // 注册配置 + builder + .AddInMemoryCollection(new Dictionary() + { + {"UserName", "Alice"} + }) + .AddJsonFile("appsettings.json"); + }) + .ConfigureServices((context, services) => + { + // 注册自定义服务 + services.AddSingleton(); + + if (context.Configuration.GetValue("EnableDemo")) + { + + } + }); + } + + /// + /// 注册服务(支持以下三种) + /// ConfigureServices(IServiceCollection services) + /// ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) + /// ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services) + /// + public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) + { + + } + + + /// + /// 配置服务:类似于 asp.net core 里 Configure 方法 + /// 可以注册已经注册的自定义服务 + /// + public void Configure(IServiceProvider applicationServices) + { + + } + } +} diff --git a/XUnitDIStudy.Test/UseXUnit.cs b/XUnitDIStudy.Test/UseXUnit.cs new file mode 100644 index 0000000..6a13f3a --- /dev/null +++ b/XUnitDIStudy.Test/UseXUnit.cs @@ -0,0 +1,15 @@ +using System; + +using Xunit; + +namespace XUnitDIStudy.Test +{ + public class UseXUnit + { + [Fact] + public void UseXunit_Test() + { + Assert.True(true,"ʹXUnit"); + } + } +} diff --git a/XUnitDIStudy.Test/XUnitDIStudy.Test.csproj b/XUnitDIStudy.Test/XUnitDIStudy.Test.csproj new file mode 100644 index 0000000..afd730a --- /dev/null +++ b/XUnitDIStudy.Test/XUnitDIStudy.Test.csproj @@ -0,0 +1,30 @@ + + + + net5.0 + + false + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + diff --git a/XUnitDIStudy.WebApp/Controllers/DefaultController.cs b/XUnitDIStudy.WebApp/Controllers/DefaultController.cs new file mode 100644 index 0000000..6a8f8fe --- /dev/null +++ b/XUnitDIStudy.WebApp/Controllers/DefaultController.cs @@ -0,0 +1,49 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using XUnitDIStudy.Model; +using XUnitDIStudy.Service; + +namespace XUnitDIStudy.WebApp.Controllers +{ + /// + /// 默认控制器 + /// + [ApiController] + [Route("[controller]/[action]")] + public class DefaultController : ControllerBase + { + private readonly ILogger _logger; + private readonly StudentService _studentService; + + public DefaultController(ILogger logger, StudentService studentService) + { + _logger = logger; + _studentService = studentService; + } + + /// + /// 获取所有学生 + /// + [HttpGet] + public List GetAll() + { + var list = _studentService.GetAll(); + return list; + } + + /// + /// 获取学生 + /// + [HttpGet] + public Student Get() + { + return new Student() { Id=10,Name="小三",Age =90}; + } + } +} diff --git a/XUnitDIStudy.WebApp/Program.cs b/XUnitDIStudy.WebApp/Program.cs new file mode 100644 index 0000000..be8d0f6 --- /dev/null +++ b/XUnitDIStudy.WebApp/Program.cs @@ -0,0 +1,27 @@ +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 XUnitDIStudy.WebApp +{ + 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/XUnitDIStudy.WebApp/Properties/launchSettings.json b/XUnitDIStudy.WebApp/Properties/launchSettings.json new file mode 100644 index 0000000..97ade7c --- /dev/null +++ b/XUnitDIStudy.WebApp/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:29571", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "XUnitDIStudy.WebApp": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/XUnitDIStudy.WebApp/Startup.cs b/XUnitDIStudy.WebApp/Startup.cs new file mode 100644 index 0000000..aff4516 --- /dev/null +++ b/XUnitDIStudy.WebApp/Startup.cs @@ -0,0 +1,65 @@ +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 XUnitDIStudy.WebApp +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "XUnitDIStudy.WebApp", Version = "v1" }); + }); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + + app.UseSwagger(setup=> + { + + }); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint("/swagger/v1/swagger.json", "XUnitDIStudy.WebApp v1"); + + //expressionΪʼַ + c.EnableFilter(expression:""); + + }); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/XUnitDIStudy.WebApp/XUnitDIStudy.WebApp.csproj b/XUnitDIStudy.WebApp/XUnitDIStudy.WebApp.csproj new file mode 100644 index 0000000..76fefcf --- /dev/null +++ b/XUnitDIStudy.WebApp/XUnitDIStudy.WebApp.csproj @@ -0,0 +1,17 @@ + + + + net5.0 + + + + + + + + + + + + + diff --git a/XUnitDIStudy.WebApp/appsettings.Development.json b/XUnitDIStudy.WebApp/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/XUnitDIStudy.WebApp/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/XUnitDIStudy.WebApp/appsettings.json b/XUnitDIStudy.WebApp/appsettings.json new file mode 100644 index 0000000..a929521 --- /dev/null +++ b/XUnitDIStudy.WebApp/appsettings.json @@ -0,0 +1,11 @@ +{ + "EnableDemo": true, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/XUnitDIStudy.sln b/XUnitDIStudy.sln new file mode 100644 index 0000000..25bb58b --- /dev/null +++ b/XUnitDIStudy.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30804.86 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitDIStudy.Service", "XUnitDIStudy.Service\XUnitDIStudy.Service.csproj", "{12105FBE-85A9-4B2A-88F9-3EDF67C8CD5F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitDIStudy.WebApp", "XUnitDIStudy.WebApp\XUnitDIStudy.WebApp.csproj", "{4E18B97C-D40D-449E-AB78-F97B5D4913F3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitDIStudy.Test", "XUnitDIStudy.Test\XUnitDIStudy.Test.csproj", "{4D1A2657-8B91-49CD-97C3-F34FD3D6DCB8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitDIStudy.Model", "XUnitDIStudy.Model\XUnitDIStudy.Model.csproj", "{706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {12105FBE-85A9-4B2A-88F9-3EDF67C8CD5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {12105FBE-85A9-4B2A-88F9-3EDF67C8CD5F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {12105FBE-85A9-4B2A-88F9-3EDF67C8CD5F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {12105FBE-85A9-4B2A-88F9-3EDF67C8CD5F}.Release|Any CPU.Build.0 = Release|Any CPU + {4E18B97C-D40D-449E-AB78-F97B5D4913F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4E18B97C-D40D-449E-AB78-F97B5D4913F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4E18B97C-D40D-449E-AB78-F97B5D4913F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4E18B97C-D40D-449E-AB78-F97B5D4913F3}.Release|Any CPU.Build.0 = Release|Any CPU + {4D1A2657-8B91-49CD-97C3-F34FD3D6DCB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4D1A2657-8B91-49CD-97C3-F34FD3D6DCB8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4D1A2657-8B91-49CD-97C3-F34FD3D6DCB8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4D1A2657-8B91-49CD-97C3-F34FD3D6DCB8}.Release|Any CPU.Build.0 = Release|Any CPU + {706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {706B4560-0B72-4EE8-9AF5-1907BB2CCCDB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F7A69734-95DF-437A-92F2-D8805ED1A10D} + EndGlobalSection +EndGlobal