Using WebHook of WebJob from VSTS

Many services such as Git, VSTS, Dropbox, PayPal, and others provide a provision to subscribe to WebHooks. Every WebJob is exposed with a WebHook, and the URL and credentials are accessible from the Properties page of WebJobs.

To use WebHooks with WebJobs, we have to add a NuGet package, Microsoft.Azure.WebJobs.Extensions.WebHooks, and then use WebHooks in the Main method, as follows:

    class Program 
{
static void Main()
{
var config = new JobHostConfiguration();

if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
config.UseWebHooks();
var host = new JobHost(config);
host.RunAndBlock();
}
}

We will modify our WebJob and add another method, which will be hooked up when the WebHook is triggered from VSTS. To invoke our method on the triggering of WebHook, we have to annotate our method parameter with the WebHookTrigger attribute. So, let's add the ProcessWebHookMessage method, which takes the message string parameter annotated with WebHook and a TextWriter to write the log in WebJobs logs.

Here is the method signatures which we will add in the Functions class:

    public static void ProcessWebHookMessage(
[WebHookTrigger] string message, TextWriter log)
{
log.WriteLine("Webhook has been invoked and the
message received is {0}", message);
}

Now build and publish your WebApp on Azure.

With VSTS, we can register a WebHook by selecting a project and proceeding towards the Service Hooks link, as shown in this screenshot:

To register a WebHook, we have to click on the Create Subscription button, which opens up a pop-up window where you can select the target service that exposes the callback URL. In this case, we will select Web Hooks and proceed with the Next button:

The next screen has some wizard steps where you can select the Trigger event, define filters, and specify the URL and credentials. The next screen will be the trigger window, where we can select the Build completed event:

And, finally, we can define the URL, username, and password, which can be retrieved from the Azure portal properties page.

The WebJob URL provided in the properties page is a generic one, and you have to tweak that based on your class and the method name which contains the WebHookTrigger attribute annotated for the input parameter.

The default URL format is as follows:

https://{WebAppName}.azurewebsites.net/api/continuouswebjobs/{WebJobName}/passthrough/{ClassName}/{MethodName}

In our case, ClassName will be Functions, and MethodName will be ProcessWebHookMessage, which has the WebHookTrigger attribute defined for the message parameter.

After setting this up, you can test it by hitting the Test button, and if the configuration is fine, you will get a success message, as shown in the following screenshot:

Finish the wizard, and once the project is built, the WebHook will be triggered and log the message in Azure logs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.137.75