我在使用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();
}
答案 0 :(得分:0)
更新
好的终于找到了问题,它与文化有关,查询中有一些双打(由于我的文化确实有正确的格式我还不清楚),使用&#39 ;,&#39;而不是&#39;而不是&#39;小数分隔符。
使用文化不变字符串转换解决了问题
原始答案
发生错误是因为在调用get请求之后,结果是一个null对象,我在处理对象之前检查了空值,这解决了这个问题。
问题是,这指向了一个不同的问题,完全相同的请求确实在另一台机器上返回数据,但仍未找到原因。