我正在构建一个全新的 ASP.Net Core MVC 项目。当我尝试在调试器中运行它时,出现此错误:
System.IO.IOException
HResult=0x80131620
Message=Failed to bind to address https://localhost:5001.
Source=Microsoft.AspNetCore.Server.Kestrel.Core
StackTrace:
at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.<BindAsync>d__2.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
AggregateException: One or more errors occurred. (An invalid argument was supplied.) (An invalid argument was supplied.)
Inner Exception 2:
SocketException: An invalid argument was supplied.
错误发生在我的Program.cs类中:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run(); //<-- ERROR BREAKS HERE
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
我发现this SO post遇到了类似的问题,但是我的问题不是该地址已被使用,而且我不在Mac上。在撰写本文时,我已更新到最新的 VS 版本 17.1.0,但错误仍然存在。
有人可以提供建议吗?
编辑:我尝试使用在创建时分配给应用程序的本地主机端口号 7161 和 5161,并且收到相同的错误:“SocketException:提供了无效的参数。”
我还尝试在 Windows 防火墙中打开端口。没有变化。
编辑 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.AddHttpClient();
services.AddControllersWithViews();
// declare external services and database contexts
services.AddDistributedMemoryCache();
services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(1);
});
// Adds the HTTPS Redirect
services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
options.HttpsPort = 5001;
});
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(20);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.Cookie.Name = "GLEntry.Authentication";
});
// Add IHttpContextAccessor
services.AddHttpContextAccessor();
services.AddMvc();
// May need this for application authentication?
services.AddAuthentication(IISDefaults.AuthenticationScheme);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
所以这绝对没有意义,但我可以通过将调试器设置为使用“IIS Express”而不是项目名称来使其工作。这是 VS 中绿色“播放”按钮旁边的下拉菜单;不确定该设置叫什么。
无论如何,这为我解决了问题。
在ASP.NET Core中,我是通过更改launchSettings.json文件中指定的端口号来解决的。
在 ASP.NET Core 项目中找到 launchSettings.json 文件。它通常位于 Properties 或 Properties/launchSettings 文件夹中。
打开 launchSettings.json 文件并找到用于运行应用程序的配置文件。配置文件名称通常是 IIS Express 或 Kestrel。
在配置文件设置中,找到 applicationUrl 属性。此属性指定开发服务器应侦听的 URL 和端口号。
将端口号更改为未被任何其他应用程序使用的其他值。例如,您可以将其从“https://localhost:5001”更改为“https://localhost:5002”。
保存 launchSettings.json 文件。
我最近也遇到过这样的事。 为了仍然能够使用 Swagger(当您按下运行按钮作为项目名称(而不是 IIS)时启动),我所做的如下:
通过这些步骤,我发现 5255(我的第二个应用程序 URL)导致了整个问题。