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

♀尐吖头ヾ 提交于 2019-11-28 23:32:18
Amit Apple

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.

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

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>

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!