【发布时间】:2022-02-24 22:29:50
【问题描述】:
我正在构建一个全新的 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,但错误仍然存在。
谁能提供建议?
编辑:我尝试使用在创建时分配给应用程序的 localhost 端口号 7161 和 5161,但我得到了相同的错误:“SocketException: An invalid argument was provided。”
我也尝试在 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?}");
});
}
}
【问题讨论】:
-
嗯,消息很清楚,它无法绑定地址,这是因为它正在使用中造成的......再次检查开放端口(或者您正在尝试执行两个项目)同一个端口?)
-
你也必须发布启动,因为你在你的 webbuilder 中使用它
-
@Serge 启动发布
标签: c# asp.net-core-mvc localhost