前言:
最近在項目中遇到了遇到了寫部署步驟過多的問題,為了減少.net core項目部署步驟;需要對一些基礎問題進行驗證:
如端口設置、單頁應用程序(angluar)合並部署方式等相關問題,特將解決過程記錄下來
一、.NET Core部署端口指定問題?
Kestrel 是 ASP.NET Core 項目模板指定的默認 Web 服務器。
那么在.NET Core中以Kestrel 作為作為web服務器有哪些方式能指定服務的監聽端口呢?
- 方式1:環境變量設置:launchSettings.json文件中指定applicationUrl地址
修改launchSettings.json的配置項applicationUrl值:如下兩種設置方式;多個地址用;分割
{ "$schema": "http://json.schemastore.org/launchsettings.json","profiles": { "AuditLogDemo": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "http://localhost:5000;https://localhost:5001", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
運行結果:
- 方式2:代碼中設置地址
在ConfigureWebHostDefaults中設置啟動默認值時,使用UseUrls綁定地址;多個地址用;分割
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) //改用Autofac來實現依賴注入 .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); webBuilder.UseUrls("http://*:5100;http://*:5101"); });
運行如下:
此時:方式1的設置依然存在,說明優先級:方式2>方式1
- 方式3:配置文件設置(appsettings.json)-推薦
修改程序配置文件:添加以下節點:
{ "Urls": "http://*:5200;https://*:5201" }
運行效果:
此時:方式1、方式2的設置依然存在,說明優先級:方式3>方式2>方式1
- 方式4:使用命令行配置
使用以下命令啟動程序:
//項目根目錄運行: dotnet run --urls "http://*:5300;https://*:5301" //編譯輸出命令運行 dotnet AuditLogDemo.dll --urls "http://*:5300;https://*:5301"
運行效果:
所以最后可以得出各種方式優先級為:
命令行配置(方式4)>配置文件方式(方式3)>程序指定(方式2)>環境變量配置(方式1)
二、Angular(單頁應用程序)開發頁面采用Kestrel 服務器運行
由於項目前期采用前后端分離實現,但在實施部署環節需要分成兩個站點;給實施人員帶來了多余的步驟。那么怎么解決這個問題呢?
1、添加包引用:Microsoft.AspNetCore.SpaServices.Extensions
Install-Package Microsoft.AspNetCore.SpaServices.Extensions
2、修改Startup.cs 文件中方法:
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.AddSpaStaticFiles(configuration => {
//指定單頁應用文件路徑地址 configuration.RootPath = "wwwroot/dist"; });
//…… } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseStaticFiles();
// app.UseSpaStaticFiles(); app.UseEndpoints(endpoints => { endpoints.MapControllers();
//綁定路由;必須設置 endpoints.MapControllerRoute(name: "default", pattern: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(configuration =>{}); } }
運行效果:
三、LogDashboard的使用
在項目中查看日志一直都是直接查看日志文件,那么有沒有辦法直接在頁面中查看日志內容呢?
最近了解到一個開源項目:LogDashboard 采用中間件方式,提供了一個可以簡單快速查看日志的面板。使用簡單方便。
使用方式:
1、添加包引用:LogDashboard
Install-Package LogDashboard
2、在Startup中使用LogDashboard
public void ConfigureServices(IServiceCollection services) { services.AddLogDashboard(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //使用日志看板 app.UseLogDashboard(); }
3、添加NLog及NLog配置文件
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <variable name="myvar" value="myvalue"/> <targets> <target xsi:type="file" name="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate}||${level}||${logger}||${message}||${exception:format=ToString:innerFormat=ToString:maxInnerExceptionLevel=10:separator=\r\n}||end" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="file" /> </rules> </nlog>
4、運行效果:
更加詳細的使用方式:
https://doc.logdashboard.net/ru-men/quickstart