Copied to clipboard

Flag this post as spam?

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


  • Henrik Sunesen 85 posts 282 karma points
    Jun 21, 2020 @ 14:36
    Henrik Sunesen
    0

    Call method in Surface controller from hangfire

    Hello all,

    I'm trying to call a method in one of my surface controllers from hangfire. But i'm getting a "Object reference not set to an instance of an object." exception.

    As far as I can read the problem is, that controllers is not made for this.

    But what is the right way, if I need to have access to Contentservice in a method, that needs to work as a hangfire cronjob?

    An example:

     public void DoSomething()
        {
            //Creating instance of the surface controller
            MailController mail = new MailController();
    
            RecurringJob.AddOrUpdate(() => mail.SendCSV("test"), Cron.Minutely);
        }
    

    OwinStartup Class:

      public class UmbracoStandardOwinStartup : UmbracoDefaultOwinStartup
    {
        public override void Configuration(IAppBuilder app)
        {
            //ensure the default options are configured
            base.Configuration(app);
    
            // Configure hangfire
            var options = new SqlServerStorageOptions { PrepareSchemaIfNecessary = true };
            const string umbracoConnectionName = Umbraco.Core.Constants.System.UmbracoConnectionName;
            var connectionString = System.Configuration
                .ConfigurationManager
                .ConnectionStrings[umbracoConnectionName]
                .ConnectionString;
    
            GlobalConfiguration.Configuration
                .UseSqlServerStorage(connectionString, options);
    
            // Give hangfire a URL and start the server                
            app.UseHangfireDashboard("/hangfire");
            app.UseHangfireServer();
    
            var scheduler = new ScheduleHangfireJobs();
            scheduler.DoSomething();
        }
    }
    
  • Walter 4 posts 94 karma points
    Jun 21, 2020 @ 20:09
    Walter
    100

    Hi, to consume an umbraco content from third party applications, you need to build a UmbracoApiController (API) which is similar to surface controller (MVC).

    https://our.umbraco.com/Documentation/Reference/Routing/WebApi

  • Henrik Sunesen 85 posts 282 karma points
    Jun 25, 2020 @ 20:05
    Henrik Sunesen
    0

    Hi Walter,

    Thank you, that helped me so much!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies