开发机器上的网络核心网络 api 应用程序出现 Kestrel 错误,但在另一台机器上却没有
Kestrel error on a netcore web api app on a dev machine but not on another
我在执行 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();
}
更新
好的,终于找到了问题,它与文化有关,查询中有一些双打(由于我仍然不清楚的原因,因为我的文化确实具有正确的格式),其中使用','代替 '。'用于小数分隔符。
使用文化不变字符串转换解决了问题
原始答案
发生错误是因为在调用 get 请求后,结果是一个空对象,我在处理该对象之前检查了空值,这解决了这个问题。
事实是,这指向另一个问题,完全相同的请求在另一台机器上 return 数据,仍然没有找到原因。
我在执行 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();
}
更新
好的,终于找到了问题,它与文化有关,查询中有一些双打(由于我仍然不清楚的原因,因为我的文化确实具有正确的格式),其中使用','代替 '。'用于小数分隔符。
使用文化不变字符串转换解决了问题
原始答案
发生错误是因为在调用 get 请求后,结果是一个空对象,我在处理该对象之前检查了空值,这解决了这个问题。
事实是,这指向另一个问题,完全相同的请求在另一台机器上 return 数据,仍然没有找到原因。