![](/img/trans.png)
[英]RestSharp causes internal server error in ASP.NET Core 3.1
[英]Asp.Net Core 3.1 server starts and then stops immediately - Why?
這是我在一個更大的項目中遇到的一個問題的簡單重現。 我從一個基本的 .Net Core 3.1 CLI 應用程序開始,我只是想啟動一個 Web 服務器,監聽 localhost:42420。
這是輸出,如您所見,它啟動然后立即關閉。 為什么? 我必須在配置中添加什么才能讓它繼續運行?
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:42420
infoWeb server stopped.
: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\skrym\source\repos\wstest\wstest\bin\Debug\netcoreapp3.1\
info: Microsoft.Hosting.Lifetime[0]
Application is shutting down...
info: Microsoft.Hosting.Lifetime[0]
Waiting for the host to be disposed. Ensure all 'IHost' instances are wrapped in 'using' blocks.
注意:我故意沒有處理主機,以便我可以看到額外的日志消息。
這是我的項目文件:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
這是所有的代碼:
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace wstest
{
class Program
{
static void Main(string[] args)
{
// Don't dispose this so we get all the log messages
var host = CreateHost();
host.Start();
Console.WriteLine($"Web server stopped.");
}
private static IHost CreateHost()
{
var host = Host.CreateDefaultBuilder()
.UseConsoleLifetime()
.ConfigureWebHostDefaults(x => ConfigureWebHostBuilder(x));
return host.Build();
}
private static IWebHostBuilder ConfigureWebHostBuilder(IWebHostBuilder builder)
{
return builder.UseStartup<SimpleStartup>()
.UseUrls("http://localhost:42420")
.UseContentRoot(AppContext.BaseDirectory);
}
}
class SimpleStartup
{
public void Configure()
{
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.