【问题标题】:Kestrel error when sending a request from an emulator从模拟器发送请求时出现 Kestrel 错误
【发布时间】:2016-11-23 19:18:28
【问题描述】:

通过 Fiddler 向 Kestrel 发出请求时,以下请求成功。

GET http://192.168.1.148:5000/ HTTP/1.1
Host: 192.168.1.148:5000
Connection: keep-alive

通过 NETMF Emulator 发出请求时,以下请求失败。

GET http://192.168.1.148:5000 HTTP/1.1
Host: 192.168.1.148:5000
Connection: keep-alive

这是 ASP.NET Core 错误消息。错误似乎与日志记录有关!

Request finished in 24788.6203ms 500
fail: Microsoft.AspNet.Server.Kestrel[13]
An unhandled exception was thrown by the application.
System.AggregateException: An error occurred while writing to logger(s).

---> System.ArgumentException: Parameter name: value

at Microsoft.AspNet.Http.PathString..ctor(String value)
at Microsoft.AspNet.Http.Internal.DefaultHttpRequest.get_Path()
at Microsoft.AspNet.Hosting.Internal.HostingLoggerExtensions
    .HostingRequestStarting.ToString()
at Microsoft.Extensions.Logging.Console.ConsoleLogger.Log(
    LogLevel logLevel, Int32 eventId, Object state, 
    Exception exception, Func`3 formatter)
at Microsoft.Extensions.Logging.Logger.Log(
    LogLevel logLevel, Int32 eventId, Object state, 
    Exception exception, Func`3 formatter)

--- End of inner exception stack trace ---

at Microsoft.Extensions.Logging.Logger.Log(
    LogLevel logLevel, Int32 eventId, Object state, 
    Exception exception, Func`3 formatter)
at Microsoft.AspNet.Hosting.Internal.HostingLoggerExtensions
    .RequestStarting(ILogger logger, HttpContext httpContext)
at Microsoft.AspNet.Hosting.Internal.HostingEngine
    .<>c__DisplayClass32_0.<<Start>b__0>d.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter
    .HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Server.Kestrel.Http.Frame
    .<RequestProcessingAsync>d__79.MoveNext()

---> (Inner Exception #0) System.ArgumentException: Parameter name: value

at Microsoft.AspNet.Http.PathString..ctor(String value)
at Microsoft.AspNet.Http.Internal.DefaultHttpRequest.get_Path()
at Microsoft.AspNet.Hosting.Internal.HostingLoggerExtensions
    .HostingRequestStarting.ToString()
at Microsoft.Extensions.Logging.Console.ConsoleLogger.Log(
    LogLevel logLevel, Int32 eventId, Object state, 
    Exception exception, Func`3 formatter)
at Microsoft.Extensions.Logging.Logger.Log(
    LogLevel logLevel, Int32 eventId, Object state, 
    Exception exception, Func`3 formatter)<---

这是整个 ASP.NET Core 程序。

using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Logging;

namespace EmptyApplication01
{
    public class Startup
    {
        public void Configure(
            IApplicationBuilder app, 
            ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(minLevel: LogLevel.Verbose);

            app.Run(async (context) =>
            {
                var logger = loggerFactory.CreateLogger("CatchAll");
                logger.LogInformation(DateTime.Now.ToString());

                await context.Response.WriteAsync("head, body");
            });
        }
    }
}

【问题讨论】:

    标签: asp.net-core kestrel-http-server


    【解决方案1】:

    我想通了。如果 AbsoluteURL 在路径中,Kestrel 将中断。为了使它工作,这是解决方法

        app.Use(next => async context => {
    
                     // get the frame from kestrel and then but the path by removing the hostname 
                    var Frame = (Microsoft.AspNet.Server.Kestrel.Http.Frame)context.Features;
    
                    var newpath = Frame.RequestUri.Replace("http://" + context.Request.Host.Value, "");
                    context.Request.Path = newpath;
    
                    // Invoke the rest of the pipeline.
                    await next(context);
    
        });
    

    【讨论】:

      【解决方案2】:

      关闭日志记录解决了这个问题。这可能是 ASP.NET 日志记录实现中的一个错误。

      using Microsoft.AspNet.Builder;
      using Microsoft.AspNet.Http;
      using System;
      
      namespace EmptyApplication01
      {
          public class Startup
          {
              public void Configure(IApplicationBuilder app)
              {
                  app.Run(async (context) =>
                  {
                      // no more logging!
                      System.Console.WriteLine(DateTime.Now.ToString());
                      await context.Response.WriteAsync("head, body");
                  });
              }
          }
      }
      

      【讨论】:

      • 我很想说这是一个 Kestrel 错误(尝试使用空字符串创建 PathString 是不合法的操作)。您应该能够通过尝试从您的内联中间件访问context.Request.Path 来确认它。
      • 好的。会做。现在,为什么 Kestrel 会处理来自 Firefox 和 Fiddler 的请求,而不是来自 NETMF 模拟器的请求?
      • NETMF 请求中使用的 URI 末尾没有斜杠。
      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 2016-01-06
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多