Logic Apps scheduled jobs - micklpl/avanade-azure-workshop GitHub Wiki

Available tags

scheduler-consume-message scheduler-send-email

Azure Logic Apps consume message

git checkout service-bus-process-message

Add this code to Topics project, Functions.cs

    private const string NewsletterTriggerName = "Newsletter";

    public async Task ProcessNewsletter([ServiceBusTrigger(NewsletterTriggerName, SubscriptionName)] BrokeredMessage message, TextWriter textWriter)
    {
        await WriteMessage("Newsletter arrived", textWriter);
    }

Go to Portal Azure and add a new resource of the type of Logic App.

Choose recurring trigger.

Set the frequency of the trigger.

Add another step which sends a message to the Service Bus.

Choose the topic from the list, add Content parameter and set some value there.

Test connection by running the trigger manually or wait for it. Open Web Jobs logs to check if the message has arrived.

Azure Logic Apps send email

git scheduler-consume-message

Add RestSharp NuGet to WebApp project.

Create MailgunService in Services. Put your email in the code.

public class MailgunService
{
    public HttpStatusCode SendEmail()
    {
        var client = new RestClient
        {
            BaseUrl = new Uri("https://api.mailgun.net/v3"),
            Authenticator = new HttpBasicAuthenticator("api",
                "key-137c11c4021afffb03433a088e2d03bd")
        };

        var request = new RestRequest();
        request.AddParameter("domain", "sandboxc0852d6e6a1c4f2685f9333b377bdb28.mailgun.org", ParameterType.UrlSegment);
        request.Resource = "{domain}/messages";
        request.AddParameter("from", "Mailgun Sandbox <[email protected]>");
        request.AddParameter("to", "[email protected]");
        request.AddParameter("subject", "World Cup Newsletter");
        request.AddParameter("text", "Daily Summary");
        request.Method = Method.POST;

        var response = client.Execute(request);

        return response.StatusCode;
    }
}

Create NewsletterService in BusinessLogic.

public class NewsletterService
{
    private readonly MailgunService _mailgunService;

    public NewsletterService(MailgunService mailgunService)
    {
        _mailgunService = mailgunService;
    }

    public async Task SendNewsletter()
    {
        await Task.FromResult<object>(_mailgunService.SendEmail());
    }
}

Surprise! A task for you! Register new services in Autofac for both, WepApp and Topics. Look for Global.asax.cs and IocConfig.cs files.

Update Functions.cs and you are ready to go.

    public async Task ProcessNewsletter([ServiceBusTrigger(NewsletterTriggerName, SubscriptionName)] BrokeredMessage message, TextWriter textWriter)
    {
        using (var scope = Program.Container.BeginLifetimeScope())
        {
            try
            {
                var newsletterService = scope.Resolve<NewsletterService>();
                await newsletterService.SendNewsletter();
            }
            catch (Exception ex)
            {
                textWriter.WriteLine($"Unexpected error {ex.Message} {ex.StackTrace} {ex.InnerException}");
                throw;
            }
        }
    }

Trigger a message from Portal Azure.

Azure Logic Apps code complete

git checkout scheduler-send-email

⚠️ **GitHub.com Fallback** ⚠️