1

I'm using ASP.NET Core application with WebAPI template and all existing controllers are inherited from System.Web.Http.ApiController.
So, requests ends with error that pointed to NRE in method ExecuteAsync. Stack stace of this exception is shown below:

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()

Interesting moment I found overrides ExecuteAsync method: controllerContext.ControllerDescriptor property has empty value (null) and base implementation of ExecuteAsync throws this exception trying to execute code

ServicesContainer services = controllerContext.ControllerDescriptor.Configuration.Services;

Controller state

Another intersting moment for me is debug log message:

Microsoft.AspNetCore.Mvc.Internal.ActionSelector: Debug: Action 'WebAPI.Controllers.AuthController.Post (WebAPI)' with id '9848905d-176e-44bc-9453-53c5185b46c6' did not match the constraint

Full debug message stack is shown below:

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's actions are marked with AspNetCore.Mvc HTTP method attributes but it was not a problem for application before:

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)

I'm confused what is the real root of the problem. Does anyone have any ideas?

// Some another linees of code, I think, have to be shown there for better understanding of the problem:

project.json dependencies

  "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();
}
1
  • 1
    You should be deriving from Microsoft.AspNetCore.Mvc.Controller or use the WebApi compat shim (nuget.org/packages/Microsoft.AspNetCore.Mvc.WebApiCompatShim/…) if you'd like to use the WebApi pattern. Microsoft.AspNet.WebApi.Core isn't intended to be used with Asp.Net Core. Commented Nov 7, 2016 at 17:28

1 Answer 1

1

Well, thanks to Pranav for his response. For some reason the alpha-version of MVC/WebAPI controllers mix worked well. But life is a complicated thing and there were a problems related to the Controller and ApiController incompatibility. The only solution was to remove all AspCore.WebApi relations and install Microsoft.AspNetCore.Mvc.WebApiCompatShim package.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.