Creating connectors for your Logic Apps/Flow (Part 3 – More triggers)

So, let's recap: we had regular connectors, we had poll triggers. So, we're still missing the push triggers. A poll trigger needs to be polled regularly by the logic app to see if it can continue. A push trigger does not have to be polled, it will tell the logic app when it needs to continue, passing the necessary information. At this moment a push trigger needs to be called by a webhook-action. Your trigger needs two function : Subscribe and Unsubscribe. In the Webhook-action these functions will automatically be called when the Logic App starts or stops. The WebHook action needs to pass the callback-url to your Subscribe/Unsubscribe-action. This callback-url is the url your API app will call for starting the Logic App. Your functions should look like this :

public static List subscriptions = new List();
[HttpPost, Route("api/webhooktrigger/subscribe")]
public HttpResponseMessage Subscribe([FromBody] string callbackUrl)
{
    subscriptions.Add(callbackUrl);
    return Request.CreateResponse();
}
[HttpPost, Route("api/webhooktrigger/unsubscribe")]
public HttpResponseMessage Unsubscribe([FromBody] string callbackUrl)
{
    subscriptions.Remove(callbackUrl);
    return Request.CreateResponse();
}
The idea is that you call back the logic apps using the list of subscriptions as this:
using (HttpClient client = new HttpClient())
{
    foreach (string callbackUrl in subscriptions)
        await client.PostAsync(callbackUrl, 
            @"{""trigger"":""fired""}", 
            new JsonMediaTypeFormatter(), "application/json");
}

After this your Logic app continues.

For using the pushtrigger you need a webhook-connector in your logic app. This needs to call the subscribe and unsubscribe-methods of your API App. For the body you need to pass the callback url.

 image

 

But still it won't work. When going to the Trigger History you can find that you get a failure because of an error with the content-Type. It is simple to solve: Show the advanced options of your webhook-connector and add the content-type to your subscribe- and unsubscribe-headers as this:

image

Problem solved !

Loading