dotnet 小筆記 === ## 安裝在CentOS ``` sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm ``` aspnetcore-runtime 相依 dotnet-runtime 所以安裝 aspnetcore-runtime 時也會安裝 dotnet-runtime 3.1 ``` yum install dotnet-sdk-3.1 -y yum install aspnetcore-runtime-3.1 -y yum install dotnet-runtime-3.1 -y ``` 5.0 ``` yum install dotnet-sdk-5.0 -y yum install aspnetcore-runtime-5.0 -y yum install dotnet-runtime-5.0 -y ``` ## 安裝在Ubuntu ``` wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb sudo add-apt-repository universe sudo apt-get install apt-transport-https sudo apt-get update sudo apt-get install dotnet-sdk-3.1 ``` --- ## 執行 #### debug (.csproj) ``` dotnet run --environment=Production --urls=http://localhost:5001 dotnet run --environment=Production --urls http://+:5001 dotnet run --urls http://+:5001 ``` #### release (.dll) ``` set ASPNETCORE_ENVIRONMENT=Production dotnet app.dll --server.urls "http://localhost:5101;http://*:5102" dotnet app.dll --urls http://+:3412 ``` #### vscode 自動產生 F1 > .NET: Generate Assets for Build and Debug REF: https://blog.miniasp.com/post/2019/01/22/Configure-Tasks-and-Launch-in-VSCode-for-NET-Core > lanuch.json > 調整項目 > "serverReadyAction.uriFormat": "%s/swagger" > "env.ASPNETCORE_URLS": "http://+:5001" ``` { "version": "0.2.0", "configurations": [ { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceFolder}/WebAppCore/bin/Debug/netcoreapp3.1/WebAppCore.dll", "args": [], "cwd": "${workspaceFolder}/WebAppCore", "stopAtEntry": false, // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser "serverReadyAction": { "action": "openExternally", "pattern": "^\\s*Now listening on:\\s+(https?://\\S+)", "uriFormat": "%s/swagger" }, "env": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_URLS": "http://+:5001" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach", "processId": "${command:pickProcess}" } ] } "configurations": [ ... "env": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_URLS": "http://+:5001" }, ``` > task.json > ctrl + shift + B 可以進行編譯 > set group defualt build ``` { "version": "2.0.0", "tasks": [ { "label": "build", "command": "dotnet", "type": "process", "args": [ "build", "${workspaceFolder}/WebAppCore/WebAppCore.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile", "group": { "kind": "build", "isDefault": true } }, { "label": "publish", "command": "dotnet", "type": "process", "args": [ "publish", "${workspaceFolder}/WebAppCore/WebAppCore.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, { "label": "watch", "command": "dotnet", "type": "process", "args": [ "watch", "run", "${workspaceFolder}/WebAppCore/WebAppCore.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" } ] } ``` --- ## linux dotnet sample.service ``` [Unit] Description=Sample Service [Service] # 工作目錄 WorkingDirectory=/var/sample # 執行指令 ExecStart=/bin/dotnet sample.dll # 自動重啟 Restart=always RestartSec=10 # 環境參數 Environment=ASPNETCORE_ENVIRONMENT=Development Environment=ASPNETCORE_URLS=https://localhost:50001;http://localhost:50000 [Install] WantedBy=multi-user.target ``` > 先使用 which dotnet 查看 dotnet CLI 路徑 ``` systemctl enable sample && systemctl start sample ``` ## 使用nginx當反代轉發dotnet ``` # nginx conf server { listen 8090; server_name SERVER_IP; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { root C:/nginx/html/admin; index index.html index.htm index.php; } location ~ ^/api { proxy_pass https://127.0.0.1:50001; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } ``` --- ## IIS deploy ### 設定 application pool 網站新增完成後,到應用程式集區,把 CLR 版本改為沒有 Managed 程式碼 ### 設定 Environment dotnet publish 後, 編輯 web.config, 增加 <PropertyGroup> 區段 ``` <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\Xuenn.IBR.Web.Service.OBSports.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess"> <!-- 增加此區塊 --> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="QAT" /> </environmentVariables> </aspNetCore> </system.webServer> </location> </configuration> ``` --- ### dotnet 4.X #### build ``` msbuild .\src\Piper\Piper.csproj /p:Configuration=Debug;DeployOnBuild=true;DeployTarget=Package;_PackageTempDir=../../aaaaa ``` /p:DeployOnBuild=True;DeployTarget=Package;_PackageTempDir=... 打包出來是可以直接佈署的結構, 不會有 _PublishedWebsites 的目錄 /p:OutputPath="..." 會有 _PublishedWebsites 的目錄 #### deploy ``` MSDeploy.exe -source:package="package.zip" -dest:contentPath="c:\project\website" -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -enableRule:DoNotDeleteRule -allowUntrusted ```