Ich verwende ASP.NET Core-Anwendung mit WebAPI-Vorlage und alle vorhandenen Controller sind von System.Web.Http.ApiController
geerbt.
So endet Anfragen mit Fehler, der auf NRE in Methode ExecuteAsync
zeigte. Stapel stace dieser Ausnahme ist unten dargestellt:ASP Core: NullReferenceException in ApiController.ExecuteAsync
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
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.Server.IISIntegration.IISMiddleware.<Invoke>d__8.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()
Interessante Moment fand ich überschreibt ExecuteAsync Methode: controllerContext.ControllerDescriptor Eigenschaft leer Wert (null) und Basisimplementierung von ExecuteAsync Würfen diese Ausnahme Code auszuführen versucht
ServicesContainer services = controllerContext.ControllerDescriptor.Configuration.Services;
für mich Eine weitere interessante Moment Nachricht Debug-Log:
Microsoft.AspNetCore.Mvc.Internal.ActionSelector: Debug: Action 'WebAPI.Controllers.AuthController.Post (WebAPI)' with id '9848905d-176e-44bc-9453-53c5185b46c6' did not match the constraint
Vollständige Debug-Meldung Stapel wird unten dargestellt:
Microsoft.AspNetCore.Hosting.Internal.WebHost: Information: Request starting HTTP/1.1 GET http://localhost:64513/api/auth application/json; charset=utf-8 0
Microsoft.AspNetCore.Routing.Tree.TreeRouter: Debug: Request successfully matched the route with name '' and template 'api/auth'.
Microsoft.AspNetCore.Mvc.Internal.ActionSelector: Debug: Action 'WebAPI.Controllers.AuthController.Post (WebAPI)' with id '9848905d-176e-44bc-9453-53c5185b46c6' did not match the constraint 'Microsoft.AspNetCore.Mvc.Internal.HttpMethodActionConstraint'
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Debug: Executing action WebAPI.Controllers.AuthController.ExecuteAsync (WebAPI)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Information: Executing action method WebAPI.Controllers.AuthController.ExecuteAsync (WebAPI) with arguments (System.Web.Http.Controllers.HttpControllerContext, System.Threading.CancellationToken) - ModelState is Valid
Exception thrown: 'System.NullReferenceException' in System.Web.Http.dll
Exception thrown: 'System.NullReferenceException' in Microsoft.AspNetCore.Mvc.Core.dll
Exception thrown: 'System.NullReferenceException' in mscorlib.dll
Exception thrown: 'System.NullReferenceException' in mscorlib.dll
Exception thrown: 'System.NullReferenceException' in mscorlib.dll
Exception thrown: 'System.NullReferenceException' in mscorlib.dll
Microsoft.AspNetCore.Server.Kestrel: Error: Connection id "0HL06UADGPFB4": An unhandled exception was thrown by the application.
Controller-Aktionen sind markiert mit AspNetCore.Mvc HTTP-Methode Attribute, aber es war kein Problem für die Anwendung vor:
using BodyObject = Microsoft.AspNetCore.Mvc.FromBodyAttribute;
using OPTIONS = Microsoft.AspNetCore.Mvc.HttpOptionsAttribute;
using POST = Microsoft.AspNetCore.Mvc.HttpPostAttribute;
[OPTIONS, POST]
public async Task<PostResponse> Post([BodyObject] PostRequest model)
Ich bin verwirrt, was ist die wahre Wurzel des Problems. Hat jemand irgendwelche Ideen?
// Einige weitere linees von Code, glaube ich, haben gezeigt, dass es für ein besseres Verständnis des Problems sein:
project.json Abhängigkeiten
"dependencies": {
"Microsoft.AspNet.WebApi.Core": "5.2.3",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"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.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.AspNetCore.Routing": "1.0.0"
},
Startup.RegisterServices
services.AddMvc(options => { /* some filters add code */ });
services.AddRouting();
Startup.Configure
#if DEBUG
loggerFactory.AddDebug((a, b) => true);
#endif
app.UseMvc();
app.UseCors(builder => builder.WithOrigins("*").AllowAnyHeader().AllowAnyMethod());
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
Sie sollten von 'Microsoft.AspNetCore.Mvc.Controller' abgeleitet sein oder das WebApi compat shim (https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.WebApiCompatShim/1.0.0) verwenden, wenn Sie möchte das WebApi-Muster verwenden. 'Microsoft.AspNet.WebApi.Core' ist nicht für die Verwendung mit Asp.Net Core vorgesehen. – Pranav