简体   繁体   English

为什么 Asp.Net Core Kestrel 服务器向 Ngrok 返回 404 并且控制器永远不会被调用?

[英]Why is Asp.Net Core Kestrel server returns 404 to Ngrok and Controller is never gets called?

For several days I am trying to run a simple asp.net core application, with setup webhook for Telegram bot.几天来,我一直在尝试运行一个简单的 asp.net 核心应用程序,并为 Telegram bot 设置 webhook。 It runs on Mac, with Kestrel server, and Ngrok over it for proxying https to webhook.它在 Mac 上运行,带有 Kestrel 服务器和 Ngrok,用于将 https 代理到 webhook。 Currently it returns 404 to Ngrok and also I see 404 in console.目前它返回 404 给 Ngrok,我也在控制台中看到 404。 Any help is appreciated.任何帮助表示赞赏。 I have tried various modifications of this line ./ngrok http http://localhost:5000 -host-header=localhost:5000我已经尝试了这一行的各种修改./ngrok http http://localhost:5000 -host-header=localhost:5000

I am a very beginner in Asp.Net Core.我是 Asp.Net Core 的初学者。 Here is my core parts of app:这是我的应用程序的核心部分:

[Route("/")]
[ApiController]
public class UpdateController : ControllerBase
{
    [HttpPost]
    public async Task<OkResult> Post([FromBody]Update update)

This one is never gets called.这个永远不会被调用。 Here is my Program.cs这是我的 Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    private static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .Build();
}

Startup.cs启动文件

public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        
        Bot.GetBotClientAsync().Wait();
    }
}

Bot.cs机器人

public static class Bot
{
    private static TelegramBotClient botClient;
    private static List<Command> commandsList;

    public static IReadOnlyList<Command> Commands => commandsList.AsReadOnly();

    public static async Task<TelegramBotClient> GetBotClientAsync()
    {
        if (botClient != null)
        {
            return botClient;
        }

        commandsList = new List<Command>
        {
            new StartCommand()
        };

        botClient = new TelegramBotClient(AppSettings.Token);
        await botClient.SetWebhookAsync(AppSettings.NGrokUrl);
        return botClient;
    }
}

Setting the webhook works fine, Ngrok receives requests, but the answer from Kestrel is always 404, and Post method is never get called.设置 webhook 工作正常,Ngrok 收到请求,但来自 Kestrel 的答案始终是 404,并且永远不会调用 Post 方法。

An example of an output in console:控制台输出示例:

dbug: Microsoft.AspNetCore.Hosting.Diagnostics[3]
      Hosting starting
dbug: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer[2]
      Failed to locate the development https certificate at '(null)'.
dbug: Microsoft.AspNetCore.Hosting.Diagnostics[4]
      Hosting started
dbug: Microsoft.AspNetCore.Hosting.Diagnostics[0]
      Loaded hosting startup assembly eBot
Hosting environment: Development
Content root path: /Users/yuriibabii/Projects/eBot/eBot
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[6]
      Connection id "0HM4GMVPJLPSF" received FIN.
dbug: Microsoft.AspNetCore.Server.Kestrel[39]
      Connection id "0HM4GMVPJLPSF" accepted.
dbug: Microsoft.AspNetCore.Server.Kestrel[1]
      Connection id "0HM4GMVPJLPSF" started.
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[7]
      Connection id "0HM4GMVPJLPSF" sending FIN because: "The client closed the connection."
dbug: Microsoft.AspNetCore.Server.Kestrel[10]
      Connection id "0HM4GMVPJLPSF" disconnecting.
dbug: Microsoft.AspNetCore.Server.Kestrel[2]
      Connection id "0HM4GMVPJLPSF" stopped.
dbug: Microsoft.AspNetCore.Server.Kestrel[39]
      Connection id "0HM4GMVPJLPSG" accepted.
dbug: Microsoft.AspNetCore.Server.Kestrel[1]
      Connection id "0HM4GMVPJLPSG" started.
dbug: Microsoft.AspNetCore.Server.Kestrel[39]
      Connection id "0HM4GMVPJLPSH" accepted.
dbug: Microsoft.AspNetCore.Server.Kestrel[1]
      Connection id "0HM4GMVPJLPSH" started.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
dbug: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
      Wildcard detected, all requests with hosts will be allowed.
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
      All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
      Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 45.4537ms 404 
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000001": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000001": done reading request body.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://localhost:5000/  
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
      All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
      Connection id "0HM4GMVPJLPSG" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 9.3331ms 404 
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
      All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
      Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 10.7676ms 404 
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000002": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000002": done reading request body.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
      All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
      Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 8.5738ms 404 
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000003": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000003": done reading request body.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
      All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
      Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 8.8111ms 404 
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000004": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000004": done reading request body.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
      All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
      Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 9.6591ms 404 
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000005": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
      Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000005": done reading request body.

Ngrok 检查选项卡。表明 Ngrok 正在识别服务器。

Have you post all your Startup class?你发布了所有的 Startup 课程吗? I don't see any ConfigureServices method where you have to have the services.AddMvc().AddMvcOptions() line, nor the app.UseMvc() line in the Configure method.我没有看到任何 ConfigureServices 方法,您必须在 ConfigureServices 方法中使用 services.AddMvc().AddMvcOptions() 行,也没有看到 app.UseMvc() 行。

I'm not so sure, but I always thought you need this two lines to enable controllers.我不太确定,但我一直认为您需要这两行来启用控制器。

Try to change your Startup class in this way:尝试以这种方式更改您的 Startup 类:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().AddMvcOptions();
        services.AddControllersWithViews();
    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        
        app.UseRouting();
        app.UseMvc();

        Bot.GetBotClientAsync().Wait();
    }
}

With some modifications to @PiGi78 answer, it did work.通过对@PiGi78 的回答进行一些修改,它确实有效。 Here is the final solution:这是最终的解决方案:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().AddMvcOptions(ApplyMvcOptions);
        services.AddControllersWithViews();
        services.AddControllers().AddNewtonsoftJson();
    }

    private void ApplyMvcOptions(MvcOptions options)
    {
        options.EnableEndpointRouting = false;
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        
        app.UseRouting();
        app.UseMvc();
        
        Bot.GetBotClientAsync().Wait();
    }
}

@PiGi78 thank you very much ! @PiGi78 非常感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM