Can I invoke an Azure webjob from an Azure website and pass it parameters?

13,204

Solution 1

If you want to invoke a WebJob from your Website, the best thing you can do is simply have the WebJob code inside your Website and simply call that code, you can still easily use the WebJob SDK from inside your Website. (for calling a WebJobs SDK method sample: https://web.archive.org/web/20180415074357/http://thenextdoorgeek.com/post/WAWS-WebJob-to-upload-FREB-files-to-Azure-Storage-using-the-WebJobs-SDK).

The reason you wouldn't want to invoke the WebJob from your Website is that the invocation contains a secret you rather not store on your Website (deployment credentials).

If you rather separate WebJob and Website code, the best thing to do is to communicate using a queue, the WebJob listens on the queue and the Website pushes the request to the queue.

Regarding the original question, currently there is no way to pass parameters to the WebJob invoke call.

Solution 2

You can invoke an azure webjob with parameters using the address: "https://mywebsite.scm.azurewebsites.net/api/triggeredwebjobs/mywebjob/run?arguments=myparameter"

class Program
{
    static void Main(string[] args)
    { 
        if (args[0]=="myparameter")
        ... 
    }
}

Some info in: https://github.com/projectkudu/kudu/pull/1183

Solution 3

Took me a while to figure out how to setup the job with arguments using Azure Portal UI (not Post Api/Kudu), so here are the steps:

  1. Create the Webjob on your WebApp

  2. Locate the Web Job in one of the regional collections in the "Scheduler Job Collections", "Scheduler Job" lists

  3. Change the Url in the "Action settings" for your job and append the ?arguments=<myArgument> to it so it ends up looking like:

    ...scm.azurewebsites.net/api/triggeredwebjobs/<my-job-name>/run?arguments=<myArgument>

Solution 4

The documented way of doing this is to put one or more Azure Queue Messages into a Queue. Each message should contain enough parameter information to allow your webjob to do it's magic.

Within your WebJob use a QueueTriggerAttribute to allow Azure to automatically start the WebJob up when the appropriate queue message is received.

Details here

http://azure.microsoft.com/en-gb/documentation/articles/websites-dotnet-webjobs-sdk-storage-queues-how-to/

Share:
13,204
LeeKay
Author by

LeeKay

Updated on June 06, 2022

Comments

  • LeeKay
    LeeKay almost 2 years

    I have an Azure webjob that I want to invoke from an Azure website. I want to pass string parameters from the website to the webjob.

    I know I can invoke the webjob as a REST API (https://github.com/projectkudu/kudu/wiki/Web-jobs).
    So I can invoke the webjob without any parameters: POST jobs/triggered/myjobname/run

    But adding parameters at the end doesn't appear to be working, i.e. jobs/triggered/myjobname/run?myparam1=value1

    The information I see on using attributes in Microsoft.WindowsAzure.Jobs for binding doesn't mention my case, just binding to Azure storage items (http://blogs.msdn.com/b/jmstall/archive/2014/01/28/trigger-bindings-and-route-parameters-in-azurejobs.aspx).

    Is what I want to do doable? Do I need to do something like create a new item in an Azure storage queue to trigger my webjob?

    Thanks.

  • LeeKay
    LeeKay about 10 years
    Wow, I had completely not gotten that the WebJobs SDK enabled you to invoke the webjobs from websites. I thought the webjobs could only be uploaded. Thanks.
  • user1060500
    user1060500 almost 10 years
    Not sure if that's a good idea. Webjobs, as I understand, are for long running tasks that you don't necessarily want to invoke via your web application or through a REST service because those were not designed for long running tasks.
  • Gumzle
    Gumzle over 9 years
    Agree with user1060500 - having the actual legwork that the webjob was going to do within your website is a bad idea.
  • Nirman
    Nirman about 5 years
    Also, web jobs have more access of sandbox resources than Web App. For example, some Html to PDF generation libraries use GDI+/USR32+ resources, which is restricted to use from Web Apps, but can be accessed from Web Jobs.