Friday, June 10, 2022

Call WebJob from .Net Core Web Api

In previous tutorial we had created a webjob which can be run manually by click on Run in azure portal. But most of the cases we need to trigger this job when an event gets triggered. See the previous tutorial to create web jobs Create webjobs in .net core. I will use same web jobs here.

In this tutorial we will see how to call webjob from our web api call

Its a simple as we call any other services/api . 

1. Go to azure portal -> appservice->webjob-> click on proprties .from right panel copy webhook ,username and password in you local . 

Now open visual studio and create webapi project and paste below code 

 [HttpPost]
        [Route("webjobstest")]
        public async Task<IActionResult> CallWebJobs()
        {
            HttpClient _httpClient = new HttpClient();
            _httpClient.BaseAddress = new System.Uri("xxxxxxxx.scm.azurewebsites.net/api/");
            var bytArray = Encoding.ASCII.GetBytes("username:pwd");
            _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(bytArray));
            var response = await _httpClient.PostAsync("triggeredwebjobs/azwebjoblearn/run", null);

            return Ok(response);
       
        }

Now when you call the api above webjob will be triggered , in case of success we get response  202(accepted) code.






No comments:

Post a Comment

Thanks for your valuable comments

Convert Html to Pdf in azure function and save in blob container

 In this post  I am going to create an azure function ( httpTrigger ) and send html content  which will be converted into PDF and save in bl...