Publishing Message from AppHost Reference

First, Happy Holidays. Thanks for all the support!

I’m using the Coravel scheduling library and looking at using the MQ Background Server in Service Stack to publish a message. This is setup in the startup.cs and the only reference I have is to the AppHost so my question is, “Is this the correct way to publish the message from the AppHost’s reference?”

appHost.Resolve<IMessageService>().MessageFactory.CreateMessageProducer().Publish(new WeeklyReportScheduler()))

A more full set of code:

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            appHost = new AppHost
                AppSettings =  new MultiAppSettings(
                    new EnvironmentVariableSettings(),
                    new NetCoreAppSettings(Configuration),
                    new AppSettings())

            var provider = app.ApplicationServices;
            provider.UseScheduler(scheduler =>
                scheduler.Schedule(()=>  appHost.Resolve<IMessageService>().MessageFactory.CreateMessageProducer().Publish(new WeeklyReportScheduler())).DailyAtHour(14);
            }).OnError((exception) =>

You should only start publishing messages after the Background MQ Server has started, e.g:

AfterInitCallbacks.Add(host => {

Or you can use ServiceStackHost.IsReady() to check if the AppHost is initialized.

I’d recommend using the same AppHost PublishMessage API the services use, e.g:

var host = HostContext.AppHost;
host.PublishMessage(host.GetMessageProducer(), new WeeklyReportScheduler());

Although you could also execute the Service in the Scheduler callback, e.g:

HostContext.AppHost.ExecuteService(new WeeklyReportScheduler());
1 Like

Thanks. My code worked but I felt that maybe I didn’t need to call CreateMessageProducer. You pointed me in the right place. A few notes:

  • I had it working originally with ExecuteService which was the easiest way to do this probably but I wanted to push it to the MQ service
  • The scheduler executes the method (at a certain time of the day) after the mq server starts so there is no issue there
1 Like