开发机器上的网络核心 Web api 应用程序上的 Kestrel 错误,但在另一台机器上却没有

Posted

技术标签:

【中文标题】开发机器上的网络核心 Web api 应用程序上的 Kestrel 错误,但在另一台机器上却没有【英文标题】:Kestrel error on a netcore web api app on a dev machine but not on another 【发布时间】:2016-11-22 23:16:23 【问题描述】:

执行 netcore WebApi 应用程序时出现错误,奇怪的是它在另一台机器上运行正常。

这是我调用导致错误的方法时的控制台信息:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 1640.1033ms 200
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/api/weather/provider/1/latitude/37.8267/longitude/-122.423
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method WeatherServices.Controllers.WeatherController.Get (WeatherServices) with arguments (1, 37.8267, -122.423) - ModelState is Valid 
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HKTG39DAMIHF": An unhandled exception was thrown by the application.
System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.) ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at WeatherServices.WeatherApiWrappers.Wunderground.Wunderground.<GetLocalWeather>d__6.MoveNext() in C:\Workspace\ChathamWeatherAPI\WeatherServices\WeatherApiWrappers\Wunderground\Wunderground.cs:line 37
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at WeatherServices.Controllers.WeatherController.Get(Int32 source, String latitude, String longitude) in C:\Workspace\ChathamWeatherAPI\WeatherServices\Controllers\WeatherController.cs:line 50
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionFilterAsync>d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__18.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.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.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.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.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.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
   at WeatherServices.WeatherApiWrappers.Wunderground.Wunderground.<GetLocalWeather>d__6.MoveNext() in C:\Workspace\ChathamWeatherAPI\WeatherServices\WeatherApiWrappers\Wunderground\Wunderground.cs:line 37<---

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 526.1252ms 200

由此看来,错误似乎与程序的一种异步方法有关,但我不明白为什么,以及为什么它会在另一台机器上工作。

project.json:


  "dependencies": 
    "Microsoft.NETCore.App": 
      "version": "1.0.0",
      "type": "platform"
    ,
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.Extensions.Caching.Memory": "1.0.0-*",
    "RestClientHelper": "1.0.0-*"
  ,

  "tools": 
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  ,

  "frameworks": 
    "netcoreapp1.0": 
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    
  ,

  "buildOptions": 
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  ,

  "runtimeOptions": 
    "configProperties": 
      "System.GC.Server": true
    
  ,

  "publishOptions": 
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  ,

  "scripts": 
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  ,

  "tooling": 
    "defaultNamespace": "WeatherServices"
  

触发错误的方法,错误发生在return语句上:

    public async Task<IList<Forecast>> GetLocalWeather(double latitude, double longitude)
    
        var url = $"http://Domain/api/ApiKey/forecast10day/q/latitude,longitude.json";
        var result = await _restApiCaller.CallListEndPoint(url);

        //Limit the query to 8 days
        return result.Forecast.Txt_Forecast.ForecastDay.Where(day => day.Period < 16)
            .Select(day => day.MapToForecast()).ToList();
    

【问题讨论】:

刚刚看到 Omnisharp 也有此消息:发生了未处理的异常:必须能够聚合响应以将它们分散到 /updatebuffer 的所有插件中 你调试了吗?它告诉你有些东西是空的——在第 37 行设置一个断点,看看什么是空的。 由于某种原因,vs2015 安装程序在出现问题的机器上崩溃,并且 Rider 没有达到断点,所以我无法做到。但是......昨天我设法在机器上重现了错误,如果它工作正常,并修复它(这个确实有 vs2015)。我将在发生错误的机器上尝试修复,如果有效,我会在此处发布。 好的,我设法避免了抛出的错误,但它仍然无法正常工作。该程序构建一个查询并使用它执行获取请求,在有问题的机器上所有请求都返回空对象,而在没有问题的机器上,相同的查询返回数据。我将关闭这个问题,因为这是一个不同的问题。 【参考方案1】:

更新

好的,终于找到了问题,它与文化有关,查询中有一些双打(由于我的文化确实有正确的格式,我仍然不清楚),其中使用“,”而不是'。'用于小数点分隔符。

使用文化不变的字符串转换解决了这个问题

原始答案

发生错误是因为在调用 get 请求后,结果是一个空对象,我在处理该对象之前检查了空值,这解决了这个问题。

问题是,这指向了一个不同的问题,完全相同的请求确实在另一台机器上返回了数据,但仍然没有找到原因。

【讨论】:

以上是关于开发机器上的网络核心 Web api 应用程序上的 Kestrel 错误,但在另一台机器上却没有的主要内容,如果未能解决你的问题,请参考以下文章

资源服务器 API 上的 OpenIddict MVC 核心 2.0 和 JWT

我无法从 Windows 托管的 Web API 上的 IIS 访问 wkhtmltopdf

IIS 上的 Angular 发布到 Asp.Net(非核心)Web Api 错误 415

本地机器上的 Web 服务器,有啥陷阱吗?

IIS 上的 .Net 5.0 Web API 部署问题

如何向 Internet 公开 Web API 以授予与同一网络上的不同计算机连接的能力