事前说明:
1 部署使用.net6.0
2 日志查找路径 win11搜索>事件查看器>Windows日志>应用程序
服务未部署成功的几种可能情况:
↓↓↓↓↓↓↓
Application: WebApi.exe
CoreCLR Version: 6.0.422.16404
.NET Version: 6.0.4
Description: The process was terminated due to an unhandled exception.
Exception Info: System.PlatformNotSupportedException: EventLog access is not supported on this platform.
报错原因:
引用的包版本与当前项目版本不一致。
如Microsoft.Extensions.Hosting.WindowsServices引用了版本不依赖于.net6而是使用了其他版本。
解决方案:
引入依赖于.ne6开发环境的nuget包,如Microsoft.Extensions.Hosting.WindowsServices版本7.0.0
Application: OptasWebApi.exe
CoreCLR Version: 6.0.422.16404
.NET Version: 6.0.4
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.InvalidDataException: Failed to load configuration from file 'C:\appsettings.json'.
---> System.IO.FileLoadException: Could not load file or assembly 'System.IO.Pipelines, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
File name: 'System.IO.Pipelines, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
问题原因:
在本机程序集里有System.IO.Pipelines, Version=9.0.0.0(.net9.0)的引用缓存,就算项目中没有用到在生成服务时也会被检测到与运行时尝试加载的版本不一致而报错
解决方案:
从本机缓存中删除关于System.IO.Pipelines, Version=9.0.0.0的内容,即删除与项目.net6.0不通环境的
Powershell:Remove-Item -Path "C:\路径\System.IO.Pipelines.9.0.0.0" -Recurse –Force
Unhandled exception. System.IO.IOException: Failed to bind to address http://[::]:xxx: address already in use.
---> Microsoft.AspNetCore.Connections.AddressInUseException: 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
---> System.Net.Sockets.SocketException (10048): 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
问题原因:
系统进程中有之前安装过的exe结束掉,需要手动结束掉已经错误安装或者已存在的exe
解决方案:
在任务管理器>详细信息中找到[运行文件名].exe,结束任务。
Application: WebApi.exe
CoreCLR Version: 6.0.422.16404
.NET Version: 6.0.4
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentNullException: String reference not set to an instance of a String. (Parameter 's')
at System.Text.Encoding.GetBytes(String s)
at System.Text.UTF8Encoding.UTF8EncodingSealed.GetBytes(String s)
问题原因:
在configuration中使用了pathcombine连接的路径,导致报错。
_configuration = _configurationBuilder.Add(new JsonConfigurationSource
{
Path = Path_Combine(AppContext.BaseDirectory,ApppcontentPath),
Optional = false,
ReloadOnChange = true
}).Build()
解决方案:
在configuration中不设置,还是使用相对路径,在Program的代码最初就指定
Directory.SetCurrentDirectory(AppContext.BaseDirectory);
通过如下代码可以找到所有引用的路径(包括appsetting中的内容)验证是否成功读取配置文件
((IConfigurationRoot)configuration).GetDebugView();
故障存储段 1209277491919480569,类型 4
事件名称: APPCRASH
响应: 不可用
Cab ID: 0
问题签名:
P1: WebApi.exe
P2: 1.0.0.0
P3: 622f78e0
P4: KERNELBASE.dll
P5: 10.0.22621.5189
P6: 45f9d0b6
P7: e0434352
P8: 000000000006016c
P9:
P10:
问题:
通过C#的exe部署为windows服务,必须添加相关注册服务的代码
解决方案:
builder.Host.UseWindowsService();
部署后提示 Unhandled exception. System.Net.Sockets.SocketException (10049): 在其上下文中,该请求的地址无效。
问题:
想要运行启动指定的url,必须添加相关代码
解决方案:
builder.WebHost.UseUrls("http://*:50001");
Category: Microsoft.AspNetCore.Server.Kestrel
EventId: 13
SpanId: 2d6d6a48ed876071
TraceId: ae36677935b6aabe8edfc60bdd0e1c0f
ParentId: 0000000000000000
ConnectionId: 0HNCK92UFMCSN
RequestId: 0HNCK92UFMCSN:00000009
RequestPath: /
Connection id "0HNCK92UFMCSN", Request id "0HNCK92UFMCSN:00000009": An unhandled exception was thrown by the application.
Exception:
System.InvalidOperationException: The exception handler configured on ExceptionHandlerOptions produced a 404 status response. This InvalidOperationException containing the original exception was thrown since this is often due to a misconfigured ExceptionHandlingPath. If the exception handler is expected to return 404 status responses then set AllowStatusCode404Response to true.
---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Resources\options.json'.
问题原因:
将c#部署的exe作为服务托管时,加了ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default,但是File.ReadAllText("Resources/options.json"); 的路径没有指向发布地址,还是指向了C:\WINDOWS\system32\Resources\options.json导致报错
解决方案:
在代码最上层添加Directory.SetCurrentDirectory(AppContext.BaseDirectory);指定当前工作目录