文档更新

main
wanggaofeng 11 months ago
parent 2710383322
commit a4aace285b

@ -26,7 +26,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"metadata": { "metadata": {
"dotnet_interactive": { "dotnet_interactive": {
"language": "csharp" "language": "csharp"
@ -38,24 +38,7 @@
"languageId": "polyglot-notebook" "languageId": "polyglot-notebook"
} }
}, },
"outputs": [ "outputs": [],
{
"ename": "Error",
"evalue": "System.IO.FileNotFoundException: The configuration file 'ConfigFiles/Config.json' was not found and is not optional. The expected physical path was 'C:\\Users\\asus\\.nuget\\packages\\microsoft.dotnet-interactive\\1.0.522904\\tools\\net8.0\\any\\ConfigFiles\\Config.json'.\r\n at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)\r\n at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)\r\n at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()\r\n at HttpClientStudy.Config.WebApiConfigManager.GetWebApiConfigOption() in E:\\王高峰\\我的项目\\学习项目\\HttpClientStudy\\HttpClientStudy.Config\\WebApiConfigManager.cs:line 27\r\n at HttpClientStudy.Core.Utilities.StartupUtility.StartWebApiDll(String dllPath) in E:\\王高峰\\我的项目\\学习项目\\HttpClientStudy\\HttpClientStudy.Core\\Utilities\\StartupUtility.cs:line 71\r\n at Submission#2.<<Initialize>>d__0.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)",
"output_type": "error",
"traceback": [
"System.IO.FileNotFoundException: The configuration file 'ConfigFiles/Config.json' was not found and is not optional. The expected physical path was 'C:\\Users\\asus\\.nuget\\packages\\microsoft.dotnet-interactive\\1.0.522904\\tools\\net8.0\\any\\ConfigFiles\\Config.json'.\r\n",
" at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)\r\n",
" at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)\r\n",
" at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()\r\n",
" at HttpClientStudy.Config.WebApiConfigManager.GetWebApiConfigOption() in E:\\王高峰\\我的项目\\学习项目\\HttpClientStudy\\HttpClientStudy.Config\\WebApiConfigManager.cs:line 27\r\n",
" at HttpClientStudy.Core.Utilities.StartupUtility.StartWebApiDll(String dllPath) in E:\\王高峰\\我的项目\\学习项目\\HttpClientStudy\\HttpClientStudy.Core\\Utilities\\StartupUtility.cs:line 71\r\n",
" at Submission#2.<<Initialize>>d__0.MoveNext()\r\n",
"--- End of stack trace from previous location ---\r\n",
" at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)"
]
}
],
"source": [ "source": [
"//Nuget包\n", "//Nuget包\n",
"\n", "\n",
@ -63,11 +46,14 @@
"#r \"./Publish/HttpClientStudy.Core/HttpClientStudy.Core.dll\"\n", "#r \"./Publish/HttpClientStudy.Core/HttpClientStudy.Core.dll\"\n",
"\n", "\n",
"//全局对象\n", "//全局对象\n",
"global using System.IO;\n",
"global using HttpClientStudy.Core;\n", "global using HttpClientStudy.Core;\n",
"global using HttpClientStudy.Core.Utilities;\n", "global using HttpClientStudy.Core.Utilities;\n",
"\n", "\n",
"string dllFile = Path.GetFullPath(\"Publish\\\\HttpClientStudy.WebApp\\\\HttpClientStudy.WebApp.dll\",Directory.GetCurrentDirectory());\n",
"Console.WriteLine($\"指定的文件为:{dllFile}\");\n",
"//启动WebAPI项目\n", "//启动WebAPI项目\n",
"StartupUtility.StartWebApiDll(\"xxxxx\");" "StartupUtility.StartWebApiDll(dllFile);"
] ]
}, },
{ {
@ -350,7 +336,7 @@
"\n", "\n",
"var client2 = new HttpClient(socketsHandler2);\n", "var client2 = new HttpClient(socketsHandler2);\n",
"\n", "\n",
"for (var i = 0; i < 3; i++)\n", "for (var i = 0; i < 5; i++)\n",
"{\n", "{\n",
" if(i>0)\n", " if(i>0)\n",
" {\n", " {\n",
@ -408,31 +394,21 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## 3、推荐使用方式" "在这种情况下我们可以看到使用了五个连接。其中的前四个在1秒后从池中删除因此无法在下一个请求中重复使用。结果每个请求都打开了一个新连接。现在原始连接处于TIME_WAIT状态并且操作系统无法将其重新用于新的出站连接。最终连接显示为ESTABLISHED因为我在它过期之前就抓住了它。"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [] "source": [
"### 测试最大连接数"
]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": null, "metadata": {},
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [ "source": [
"#!set --value @csharp:ips --name ips\n" "## 3、推荐使用方式"
] ]
} }
], ],

@ -52,7 +52,7 @@
"\n", "\n",
"//执行C#工具方法\n", "//执行C#工具方法\n",
"using HttpClientStudy.Core.Utilities;\n", "using HttpClientStudy.Core.Utilities;\n",
"var result = CmdUtility.RunCmd(\"ls\");\n", "var result = CmdUtility.RunCmd(\"dir\");\n",
"Console.WriteLine(result);" "Console.WriteLine(result);"
] ]
}, },
@ -131,6 +131,31 @@
" Write-Host \"$WebAppProcName 进程已退出\"\n", " Write-Host \"$WebAppProcName 进程已退出\"\n",
"}" "}"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"var basePath = System.Environment.CurrentDirectory;\n",
"\n",
"var filePath =\"./Docs/a.txt\";\n",
" \n",
"var full = System.IO.Path.GetFullPath(filePath,basePath);\n",
"\n",
"Console.WriteLine(full);"
]
} }
], ],
"metadata": { "metadata": {

@ -0,0 +1,116 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"# 多语言笔记:内核中的各种路径"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"搞清楚,内核执行时的各种默认目录、路径,方便解决一些引用、文件等跟路径有关的问题。"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"应用程序的基准目录 [AppContext.BaseDirectory] C:\\Users\\asus\\.nuget\\packages\\microsoft.dotnet-interactive\\1.0.522904\\tools\\net8.0\\any\\\n",
"程序安装当前基目录 [AppDomain.CurrentDomain.BaseDirectory] C:\\Users\\asus\\.nuget\\packages\\microsoft.dotnet-interactive\\1.0.522904\\tools\\net8.0\\any\\\n",
"程序安装或启动基目录 [AppDomain.CurrentDomain.SetupInformation.ApplicationBase] C:\\Users\\asus\\.nuget\\packages\\microsoft.dotnet-interactive\\1.0.522904\\tools\\net8.0\\any\\\n",
"进程主模块的完整路径 [Process.GetCurrentProcess().MainModule.FileName] C:\\Program Files\\dotnet\\dotnet.exe\n",
"当前工作目录 [Environment.CurrentDirectory] e:\\王高峰\\我的项目\\学习项目\\HttpClientStudy\\Docs\n",
"文件当前目录 [Directory.GetCurrentDirectory()] e:\\王高峰\\我的项目\\学习项目\\HttpClientStudy\\Docs\n",
"可执行程序集目录 [Assembly.GetExecutingAssembly().Location] \n",
"指定程序集目录 [Type.Assembly.Location] C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.5\\System.Private.CoreLib.dll\n"
]
}
],
"source": [
"// C#中,内核执行时,各种默认路径如下:\n",
"\n",
"using System;\n",
"using System.IO;\n",
"using System.Threading;\n",
"using System.Threading.Tasks;\n",
"using System.Diagnostics;\n",
"using System.Reflection;\n",
"using Microsoft.Extensions.DependencyInjection;\n",
"\n",
"//应用程序信息\n",
"\n",
"//程序的上下文基准目录:通常是应用程序启动时的目录,也可以理解为包含可执行文件的目录\n",
"Console.WriteLine($\"应用程序的基准目录 [AppContext.BaseDirectory] {AppContext.BaseDirectory}\");\n",
"\n",
"//程序安装当前基目录 \n",
"Console.WriteLine($\"程序安装当前基目录 [AppDomain.CurrentDomain.BaseDirectory] {AppDomain.CurrentDomain.BaseDirectory}\");\n",
"\n",
"//程序安装或启动基目录\n",
"Console.WriteLine($\"程序安装或启动基目录 [AppDomain.CurrentDomain.SetupInformation.ApplicationBase] {AppDomain.CurrentDomain.SetupInformation.ApplicationBase}\");\n",
"\n",
"//进程主模块的完整路径\n",
"Console.WriteLine($\"进程主模块的完整路径 [Process.GetCurrentProcess().MainModule.FileName] {Process.GetCurrentProcess()?.MainModule?.FileName}\");\n",
"\n",
"//环境变量:当前工作目录\n",
"Console.WriteLine($\"当前工作目录 [Environment.CurrentDirectory] {Environment.CurrentDirectory}\");\n",
"\n",
"//文件当前目录:当前工作目录:不一定是启动目录\n",
"Console.WriteLine($\"文件当前目录 [Directory.GetCurrentDirectory()] {Directory.GetCurrentDirectory()}\");\n",
"\n",
"//可执行程序集目录\n",
"Console.WriteLine($\"可执行程序集目录 [Assembly.GetExecutingAssembly().Location] {Assembly.GetExecutingAssembly().Location}\");\n",
"\n",
"//指定程序集目录:可以是引用类库、指定类所在类库目录 \n",
"Console.WriteLine($\"指定程序集目录 [Type.Assembly.Location] {typeof(System.Environment).Assembly.Location}\");\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "python"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

@ -2,7 +2,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Diagnostics;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.EnvironmentVariables; using Microsoft.Extensions.Configuration.EnvironmentVariables;
@ -19,6 +21,7 @@ using Microsoft.Extensions.Options;
using NetEscapades.Configuration.Yaml; using NetEscapades.Configuration.Yaml;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
namespace HttpClientStudy.Config namespace HttpClientStudy.Config
{ {
@ -29,6 +32,36 @@ namespace HttpClientStudy.Config
/// </summary> /// </summary>
public static IConfigurationBuilder AddWebApiConfiguration(this IConfigurationBuilder configuration) public static IConfigurationBuilder AddWebApiConfiguration(this IConfigurationBuilder configuration)
{ {
/*
//应用程序信息
//程序的上下文基准目录:通常是应用程序启动时的目录,也可以理解为包含可执行文件的目录
Console.WriteLine($"应用程序的基准目录[AppContext.BaseDirectory] = {AppContext.BaseDirectory}");
//程序安装或启动基目录
Console.WriteLine($"程序安装或启动基目录[AppDomain.CurrentDomain.SetupInformation.ApplicationBase]{AppDomain.CurrentDomain.SetupInformation.ApplicationBase}");
//进程主模块的完整路径
Console.WriteLine($"进程主模块的完整路径[Process.GetCurrentProcess().MainModule.FileName]:{Process.GetCurrentProcess()?.MainModule?.FileName}");
//环境变量:当前工作目录
Console.WriteLine($"当前工作目录[Environment.CurrentDirectory]:{Environment.CurrentDirectory}");
//文件当前目录:当前工作目录:不一定是启动目录
Console.WriteLine($"文件当前目录[Directory.GetCurrentDirectory()]{Directory.GetCurrentDirectory()}");
//可执行程序集目录
Console.WriteLine($"可执行程序集目录[Assembly.GetExecutingAssembly().Location]:{Assembly.GetExecutingAssembly().Location}");
//指定程序集目录:可以是引用类库、指定类所在类库目录
Console.WriteLine($"指定程序集目录[Type.Assembly.Location]{typeof(WebApiConfigExtensions).Assembly.Location}");
*/
//配置文件根目录配置项目Dll文件所在目录(被其它项目引用时引用后dll的文件所在目录)
string configDellPath = Path.GetDirectoryName(typeof(WebApiConfig).Assembly.Location) ?? Directory.GetCurrentDirectory();
configuration.SetBasePath(configDellPath);
Console.WriteLine($"配置文件根目录:{configDellPath}");
configuration.AddJsonFile($"ConfigFiles/Config.json", optional: false, reloadOnChange: true); configuration.AddJsonFile($"ConfigFiles/Config.json", optional: false, reloadOnChange: true);
configuration.AddJsonFile($"ConfigFiles/Config.{System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true); configuration.AddJsonFile($"ConfigFiles/Config.{System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true);

@ -27,6 +27,7 @@ namespace HttpClientStudy.Core.Utilities
ProcessStartInfo startInfo = new ProcessStartInfo("cmd", $"/c {cmd}") ProcessStartInfo startInfo = new ProcessStartInfo("cmd", $"/c {cmd}")
{ {
RedirectStandardOutput = true, // 重定向标准输出 RedirectStandardOutput = true, // 重定向标准输出
StandardOutputEncoding = Encoding.UTF8,
UseShellExecute = false, // 不使用系统外壳程序启动 UseShellExecute = false, // 不使用系统外壳程序启动
CreateNoWindow = true // 不创建新窗口 CreateNoWindow = true // 不创建新窗口
}; };

@ -69,20 +69,27 @@ namespace HttpClientStudy.Core.Utilities
public static void StartWebApiDll(string dllPath) public static void StartWebApiDll(string dllPath)
{ {
string projectAndMutexName = WebApiConfigManager.GetWebApiConfigOption().CurrentValue.WebAppMutexName; string projectAndMutexName = WebApiConfigManager.GetWebApiConfigOption().CurrentValue.WebAppMutexName;
string dllBasePath = Environment.CurrentDirectory;
Console.WriteLine($"根目录为:{dllBasePath}");
string dllFullPath = string.Empty;
if (Path.IsPathRooted(dllPath))
{
dllFullPath = dllPath;
}
else
{
dllPath = dllPath.TrimStart('.').TrimStart('\\', '/');
dllFullPath = Path.GetFullPath(Path.Combine(dllBasePath, dllPath));
}
//webapi项目不在运行状态则启动webapi项目 //webapi项目不在运行状态则启动webapi项目
if (webAppIsRunningByMutex() == false) if (webAppIsRunningByMutex() == false)
{ {
//VS项目根目录 Console.WriteLine($"启动程序为:{dllFullPath}");
string vsProjectPath = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory)!.Parent!.Parent!.Parent!.Parent!.FullName;
Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
//webapi项目根项目
string webApiProjectPath = Path.Combine(vsProjectPath, projectAndMutexName);
//启动命令信息 //启动命令信息
var prossInfo = new System.Diagnostics.ProcessStartInfo("dotnet", $"run --project {webApiProjectPath}") var prossInfo = new System.Diagnostics.ProcessStartInfo("dotnet", $"{dllFullPath}")
{ {
UseShellExecute = true, UseShellExecute = true,
CreateNoWindow = false, CreateNoWindow = false,
@ -93,12 +100,6 @@ namespace HttpClientStudy.Core.Utilities
System.Diagnostics.Process.Start(prossInfo); System.Diagnostics.Process.Start(prossInfo);
} }
//由进程名判断
//bool webAppIsRunningByProcessName()
//{
// return Process.GetProcessesByName(projectAndMutexName).ToList().Count == 0;
//}
//由互斥锁判断 //由互斥锁判断
bool webAppIsRunningByMutex() bool webAppIsRunningByMutex()
{ {
@ -116,7 +117,6 @@ namespace HttpClientStudy.Core.Utilities
//互斥锁是否创建成功 //互斥锁是否创建成功
return !createdResult; return !createdResult;
} }
} }
/// <summary> /// <summary>

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HttpClientStudy.Core.Utilities;
namespace HttpClientStudy.UnitTest.UtilitiesTest
{
public class StartupUtilityTest
{
private readonly ITestOutputHelper _output;
public StartupUtilityTest(ITestOutputHelper testOutput)
{
_output = testOutput;
}
[Fact]
public void StartWebApiDll_Test()
{
}
}
}
Loading…
Cancel
Save