Azure

Set off Azure WebJob By Utilizing WebAPI

Introduction

On this article, I’ll focus on easy methods to set off/run an Azure net job through the use of net api.

Azure net job is sort of a service that may run a job within the background. It doesn’t include any UI and it’s the similar as our Home windows service.

The eventualities the place we use Azure net jobs are

  • Picture processing or different CPU-intensive background work.
  • Queue processing.
  • File upkeep, equivalent to aggregating or cleansing up log information.
  • Lengthy-running duties equivalent to sending emails.

Prerequisite

  • Microsoft Azure account.
  • They’re current app companies.
  • Current net job however that’s of kind both triggered or scheduled.

So let’s create a pattern software and after that, we’ll name GET and POST name.

Right here I’ve created an asp.web software that accommodates two buttons Get Internet Job Name and Submit Internet Job Name and whose design appears to be like like as beneath.

So to start with, now we have to find out about what the Azure net job webapi appears to be like like.

As you’ll be able to see on this URL, https://abc.azurewebsites.web belonged to my Azure app service.

As the online job is current beneath the Azure app service, the online job api can also be barely related and appears like this, https://abc.scm.azurewebsites.web/api/.

After the online job api URL is shaped, we have to add triggeredwebjobs eventually, and after that, we have to add our net job title.

So the entire net job api URL appears to be like like this

Complete url

Get name rationalization

Within the get name, we’re retrieving the standing of an internet job. After we click on on the get net job name button in UI then it’ll name getCallbtn_Click on the server facet and the code appears to be like as beneath.

protected void getCallbtn_Click(object sender, EventArgs e) 
{
    var standing = GetWebJobSync("triggeredwebjobs/Take a look at");
    dynamic jsonStatus = JsonConvert.DeserializeObject(standing);
    if (jsonStatus.latest_run != null) 
    {
        var jobStatus = jsonStatus.latest_run.standing;
    }
}

The getCallbtn_Click methodology will name the GetWebJobSync methodology bypassing the online job title and can return the job standing.

personal string GetWebJobSync(string name) 
{
    string ApiUrl = "https://abc.scm.azurewebsites.web/api/";
    string end result = string.Empty;
    string userPswd = "$demo" + ":" + "igQqdPY2pjbF6mTvo5WmjBpualiyqxBWgjgkaA0yc8tEo7Kbun7bRnddRvMb";
    userPswd = Convert.ToBase64String(Encoding.Default.GetBytes(userPswd));
    string baseURL = string.Format("{0}", name);
    strive 
    {
        utilizing (var shopper = new HttpClient()) 
        {
            shopper.BaseAddress = new Uri(ApiUrl);
            shopper.Timeout = TimeSpan.FromMinutes(30);
            shopper.DefaultRequestHeaders.Settle for.Clear();
            shopper.DefaultRequestHeaders.Settle for.Add(new MediaTypeWithQualityHeaderValue("software/json"));
            shopper.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Fundamental", userPswd);
            var response = new HttpResponseMessage();
            response = shopper.GetAsync(baseURL).End result;
            end result = response.IsSuccessStatusCode ? (response.Content material.ReadAsStringAsync().End result) : response.IsSuccessStatusCode.ToString();
        }
        return end result;
    } 
    catch (Exception ex) 
    {
        throw ex;
    }
}

The GetWebJobSync methodology makes use of an internet api method with a primary authentication strategy to retrieve the standing of the job.

Submit name rationalization

Within the post-call, we’re operating the online job. After we click on on the put up net job name button, then it’ll name postCallbtn_Click on the server facet and the code appears to be like as beneath.

protected void postCallbtn_Click(object sender, EventArgs e)
{
    var statusPOST = POSTWebJobSync();
}

The postCallbtn_Click methodology will name the POSTWebJobSync methodology and return the response message.

personal HttpResponseMessage POSTWebJobSync() 
{
    string ApiUrl = "https://abc.scm.azurewebsites.web/api/";
    string name = "triggeredwebjobs/Take a look at/run";
    string end result = string.Empty;
    string userPswd = "$demo" + ":" + "igQqdPY2pjbF6mTvo5WmjBpualiyqxBWgjgkaA0yc8tEo7Kbun7bRnddRvMb";
    userPswd = Convert.ToBase64String(Encoding.Default.GetBytes(userPswd));
    var response = new HttpResponseMessage(HttpStatusCode.NotFound);
    strive 
    {
        utilizing (var shopper = new HttpClient()) 
        {
            shopper.BaseAddress = new Uri(ApiUrl);
            shopper.Timeout = TimeSpan.FromMinutes(30);
            shopper.DefaultRequestHeaders.Settle for.Clear();
            shopper.DefaultRequestHeaders.Settle for.Add(new MediaTypeWithQualityHeaderValue("software/json"));
            shopper.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Fundamental", userPswd);
            response = shopper.PostAsync(name, new StringContent(string.Empty, System.Textual content.Encoding.UTF8, "software/json")).End result;
        }
        return response;
    } 
    catch (Exception ex) 
    {
        throw ex;
    }
}

In each get and put up name, we’re retrieving the username and password from the publish profile whose publishMethod=”MSDeploy” which is current within the net app and proven beneath.

<publishData>
    <publishProfile 
        profileName="demo - Internet Deploy" 
        publishMethod="MSDeploy" 
        publishUrl="demo.scm.azurewebsites.web:443" 
        msdeploySite="demo" 
        userName="$demo" 
        userPWD="igQqdPY2pjbF6mTvo5WmjBpualiyqxBWgjgkaA0yc8tEo7Kbun7bRnddRvMb" 
        destinationAppUrl="http://demo.azurewebsites.web" 
        SQLServerDBConnectionString="" 
        mySQLDBConnectionString="" 
        hostingProviderForumLink="" 
        controlPanelLink="http://home windows.azure.com" 
        webSystem="WebSites">
        <databases />
    </publishProfile>
    <publishProfile 
        profileName="demo - FTP" 
        publishMethod="FTP" 
        publishUrl="ftp://demo.ftp.azurewebsites.home windows.web/web site/wwwroot" 
        ftpPassiveMode="True" 
        userName="demo$demo" 
        userPWD="igQqdPY2pjbF6mTvo5WmjBpualiyqxBWgjgkaA0yc8tEo7Kbun7bRnddRvMb" 
        destinationAppUrl="http://demo.azurewebsites.web" 
        SQLServerDBConnectionString="" 
        mySQLDBConnectionString="" 
        hostingProviderForumLink="" 
        controlPanelLink="http://home windows.azure.com" 
        webSystem="WebSites">
        <databases />
    </publishProfile>
</publishData>

Output

The output of the get net job name standing is proven beneath.

Output of get web job call

Additionally, the output of the post-call standing is proven beneath.

Output of post call status

Observe

  1. We are able to combine the above code in asp.web, MVC, or every other applied sciences.
  2. Rather than ABC and Take a look at, it’s essential to use your app service title and net job title.

Know extra about our firm at Skrots. Know extra about our companies at Skrots Companies, Additionally checkout all different blogs at Weblog at Skrots

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button