一、环境概述
-
开发工具:Visual Studio 2022
-
开发环境:.NET Core 8.0
-
项目骨架:标准ASP.NET Core Web API项目
-
日志框架:Serilog
二、 报错日志
本地做项目练手突然日志报错,引用无法启动,报错日志如下:
[2026-03-23 14:32:54.671 DBG] Registered model binder providers, in the following order: ["Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BinderTypeModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ServicesModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.HeaderModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FloatingPointTypeModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.EnumTypeModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.DateTimeModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.TryParseModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.CancellationTokenModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ByteArrayModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormFileModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormCollectionModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.KeyValuePairModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.DictionaryModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ArrayModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.CollectionModelBinderProvider","Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexObjectModelBinderProvider"]
[2026-03-23 14:32:54.766 DBG] Hosting starting
[2026-03-23 14:32:54.820 INF] User profile is available. Using 'C:\Users\XXX\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
[2026-03-23 14:32:54.842 DBG] Reading data from file 'C:\Users\XXX\AppData\Local\ASP.NET\DataProtection-Keys\key-16beedb6-7235-4b0f-b6f2-ff9b7da37b76.xml'.
[2026-03-23 14:32:54.857 DBG] Reading data from file 'C:\Users\XXX\AppData\Local\ASP.NET\DataProtection-Keys\key-6252971e-cc21-483a-9790-51a62aa3eed3.xml'.
[2026-03-23 14:32:54.882 DBG] Found key {16beedb6-7235-4b0f-b6f2-ff9b7da37b76}.
[2026-03-23 14:32:54.891 DBG] Found key {6252971e-cc21-483a-9790-51a62aa3eed3}.
[2026-03-23 14:32:54.897 DBG] Considering key {16beedb6-7235-4b0f-b6f2-ff9b7da37b76} with expiration date 2026-05-20 14:30:42Z as default key.
[2026-03-23 14:32:54.903 DBG] Forwarded activator type request from Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor, Microsoft.AspNetCore.DataProtection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 to Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor, Microsoft.AspNetCore.DataProtection, Culture=neutral, PublicKeyToken=adb9793829ddae60
[2026-03-23 14:32:54.908 DBG] Decrypting secret element using Windows DPAPI.
[2026-03-23 14:32:54.916 DBG] Forwarded activator type request from Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 to Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Culture=neutral, PublicKeyToken=adb9793829ddae60
[2026-03-23 14:32:54.920 DBG] Opening CNG algorithm 'AES' from provider 'null' with chaining mode CBC.
[2026-03-23 14:32:54.923 DBG] Opening CNG algorithm 'SHA256' from provider 'null' with HMAC.
[2026-03-23 14:32:54.925 DBG] Using key {16beedb6-7235-4b0f-b6f2-ff9b7da37b76} as the default key.
[2026-03-23 14:32:54.928 DBG] Key ring with default key {16beedb6-7235-4b0f-b6f2-ff9b7da37b76} was loaded during application startup.
[2026-03-23 14:32:55.125 INF] Unable to bind to https://localhost:7096 on the IPv4 loopback interface: '以一种访问权限不允许的方式做了一个访问套接字的尝试。'.
[2026-03-23 14:32:55.128 INF] Unable to bind to https://localhost:7096 on the IPv6 loopback interface: '以一种访问权限不允许的方式做了一个访问套接字的尝试。'.
[2026-03-23 14:32:55.141 ERR] Hosting failed to start
System.IO.IOException: Failed to bind to address https://localhost:7096.
---> System.AggregateException: One or more errors occurred. (以一种访问权限不允许的方式做了一个访问套接字的尝试。) (以一种访问权限不允许的方式做了一个访问套接字的尝试。)
---> System.Net.Sockets.SocketException (10013): 以一种访问权限不允许的方式做了一个访问套接字的尝试。
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, Boolean disconnectOnFailure, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass28_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
---> (Inner Exception #1) System.Net.Sockets.SocketException (10013): 以一种访问权限不允许的方式做了一个访问套接字的尝试。
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, Boolean disconnectOnFailure, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass28_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)<---
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(ListenOptions[] listenOptions, AddressBindContext context, Func`2 useHttps, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__15_1(IHostedService service, CancellationToken token)
at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)1、端口占用
三、排查思想和解决办法
1、端口占用导致
# 找到占用的端口
netstat -ano | findstr :7096
# 不重要进程的直接杀掉
tasklist | findstr <PID>
# 重要的就在appsettings.json改下自己应用启动的端口错开。
{
"profiles": {
"YourApp": {
"applicationUrl": "https://localhost:7400;http://localhost:5080"
}
}
}
2、https证书过期
以管理员身份运行 PowerShell:
# 1. 检查证书状态
dotnet dev-certs https --check
# 2. 清除现有证书
dotnet dev-certs https --clean
# 3. 创建并信任新证书
dotnet dev-certs https --trust
# 4. 验证证书
dotnet dev-certs https --check
3、应用端口被windows排除
这个是最他妈恶心的,因为如果你装了WSL2什么的软件的话,windows 10或者11会排除一些端口范围作为备用端口,访问就会提示无法绑定端口,我就这种。
# 查看备用
netsh int ipv4 show excludedportrange protocol=tcp
# 这个示例
C:\Users\XXX>netsh int ipv4 show excludedportrange protocol=tcp
协议 tcp 端口排除范围
开始端口 结束端口
---------- --------
1046 1145
1246 1345
4807 4807
4811 4811
6739 6838
-- 7039 7138
7139 7238
7239 7338
13488 13587
44337 44337
44399 44399
50000 50059
开始的7095端口好像就在--标记的备用端口范围之内,直接改成7400解决。