Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nijaz Hameed 38 posts 173 karma points
    Nov 15, 2019 @ 04:09
    Nijaz Hameed
    0

    dependency injection for System.Web.Http.APIcontrollerin umbraco 8

    Can anyone help with dependency injection for System.Web.Http.APIcontroller in umbraco 8? I have tried light injection and autofac but nothing worked. always caught with an error below

    {
    "Message": "An error has occurred.",
    "ExceptionMessage": "An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor.",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Type 'Integration.Controllers.MyController' does not have a default constructor",
        "ExceptionType": "System.ArgumentException",
        "StackTrace": "   at System.Linq.Expressions.Expression.New(Type type)\r\n   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
    }
    
  • Nijaz Hameed 38 posts 173 karma points
    Nov 15, 2019 @ 10:10
    Nijaz Hameed
    100

    I have resolved the issue by using UmbracoApiController and i have registerd my services using IUserComposer so dependency injection works fine as expected. after all, UmbracoApiController is inheriting System.Web.Http.ApiController, so it is working fine as expected. also UmbracoApiController helps with routing.

Please Sign in or register to post replies

Write your reply to:

Draft