Copied to clipboard

Flag this post as spam?

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


  • Alastair Gordon 7 posts 107 karma points
    Aug 08, 2019 @ 15:55
    Alastair Gordon
    0

    Custom Routes - MapRoute - Error: Unable to resolve type: XXX.SurfaceControllers.JobQueueRunnerController, service name:

    Hi,

    I need to set-up a custom MapRoute so I can set a VisualCron task to periodically call the url and let it kick off a database check / email function.

    Either I'm losing the plot, or I've messed something up. This used to work in V7 in ApplicationEventHandler and a simple MapRoute.

    I understand that ApplicationEventHandlers are now out for V8, so I have the following:

    RegisterCustomRouteComposer in the root of the Umbraco project:

    public class RegisterCustomRouteComposer : ComponentComposer<RegisterCustomRouteComponent>
    {
    }
    
    public class RegisterCustomRouteComponent : IComponent
    {        
        public void Initialize()
        {
            RouteTable.Routes.MapRoute("JobQueueRunner", "runjobqueue", new
            {
                controller = "JobQueueRunner",
                action = "ProcessQueue",
                id = UrlParameter.Optional
            });
        }
    
        public void Terminate()
        {
            throw new NotImplementedException();
        }
    }
    

    Then a JobQueueRunnerController (SurfaceController folder) and Interface to the JobQueueService

    public class JobQueueRunnerController : Controller
    {
        private readonly IJobQueueService _jobQueueService;
    
        public JobQueueRunnerController(IJobQueueService jobQueueService)
        {
            _jobQueueService = jobQueueService;
        }
    
        public ActionResult ProcessQueue()
        {
            _jobQueueService.ProcessNextJobs(ControllerContext);
            return Content("Ok");
        }
    }
    
    public interface IJobQueueService
    {
        void ProcessNextJobs(ControllerContext controllerContext);
    }
    

    When http://<--sitename-->/runjobqueue is called, I get the following exception:

    [InvalidOperationException: Unable to resolve type: XXX.SurfaceControllers.JobQueueRunnerController, service name: ]
       LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) +212
       LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError) +146
       LightInject.ServiceContainer.GetInstance(Type serviceType) +192
       Umbraco.Web.Mvc.ContainerControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +46
    
    [Exception: Failed to create an instance of controller type XXX.SurfaceControllers.JobQueueRunnerController (see inner exception).]
       Umbraco.Web.Mvc.ContainerControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +220
       Umbraco.Web.Mvc.UmbracoControllerFactory.CreateController(RequestContext requestContext, String controllerName) +109
       Umbraco.Web.Mvc.RenderControllerFactory.CreateController(RequestContext requestContext, String controllerName) +12
       System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +262
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +77
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1122
       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +213
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +131
    

    Any ideas / pointers / obvious mistakes on my part?

    Thanks,

    Alastair

  • Bo Jacobsen 606 posts 2404 karma points
    Aug 08, 2019 @ 19:20
    Bo Jacobsen
    100

    Hi Alastair.

    Could it be that JobQueueRunnerController should inherit from SurfaceController instead of Controller?

    public class JobQueueRunnerController : Umbraco.Web.Mvc.SurfaceController
    

    Have you installed your IJobQueueService in an IUserComposer?

        using Umbraco.Core;
        using Umbraco.Core.Composing;
    
        namespace My.Website
        {
            public class CustomComposer : IUserComposer
            {
                public void Compose(Composition composition)
                {
                    composition.Register<IJobQueueService, JobQueueService>();
                }
            }
        }
    
  • Alastair Gordon 7 posts 107 karma points
    Aug 09, 2019 @ 09:20
    Alastair Gordon
    0

    Hi Bo,

    Thanks! I changed the controller to a SurfaceController and everything started working as I expected.

    I'd lifted the JobQueueRunnerController code straight from a working Umbraco7 site, which was happily working with just inheriting from 'Controller' and expected it to work...

    Thanks again.

Please Sign in or register to post replies

Write your reply to:

Draft