c# - 具有 Http 触发功能的连续 Azure WebJob

标签 c# azure azure-webjobs

我目前有一个 azure 的网络作业,每天执行从一个数据库到另一个数据库的同步,但也想添加手动触发同步的功能。我在webjob项目中设置的功能如下:

public static void SyncData([TimerTrigger("0 0 5 * * *", RunOnStartup = false)] TimerInfo timerInfo) { }

[NoAutomaticTrigger]
public static Task SyncAll(TraceWriter log){ }
[NoAutomaticTrigger]
public static Task SyncBranches(TraceWriter log){ }
[NoAutomaticTrigger]
public static Task SyncCustomers(TraceWriter log){ }
[NoAutomaticTrigger]
public static Task SyncInventory(TraceWriter log){ }

我可以在 webjob 下的 Kudu 仪表板中看到这些功能,但不确定如何使用 MS 文档 ( here ) 中列出的 http 请求触发这些功能:

http://<yourapp>.azurewebsites.net/api/<funcname> 

当我向该端点发出请求时,我收到 404 响应 - 我需要做什么才能通过 http 请求手动触发这些功能?

谢谢, 瑞安

最佳答案

我们可以使用 Kudu WebJob API开始或停止连续作业

POST https://{user}:{password}@{sitename}.scm.azurewebsites.net/api/continuouswebjobs/{job name}/start

我们可以从Azure WebApp的publishSetting文件中获取用户和密码信息。我们可以从门户下载文件

enter image description here

使用 fiddler 测试 Rest API

enter image description here

注意:如何创建Azure函数请引用document .

更新:

is there a way I can trigger a specific function in that continuous job?

根据我的经验,我找不到一种方法或Rest API来直接触发连续作业中的特定功能。

我的解决方法是我们可以使用 Webjob QueueTrigger。根据队列消息信息触发特定功能。 我们可以创建一个WebJob with QueueTrigger

以下是我根据您的代码的详细步骤。

1.为触发器创建Azure存储队列

enter image description here

2.创建Webjob项目并添加以下代码

 public static void SyncData([QueueTrigger("backup")] string logMessage, TextWriter logger)
    {
        Console.WriteLine($"Start time:{DateTime.Now}");
        switch (logMessage.ToLower())
        {
            case "syncall":
                SyncAll(logger);
                break;
            case "syncbranches":
                SyncBranches(logger);
                break;
            case "synccustomers":
                SyncCustomers(logger);
                break;
            case "syncinventory":
                SyncInventory(logger);
                break;
            default:
                Console.WriteLine("Default case");
                break;
        }
        Console.Write($"Endtime:{DateTime.Now}");

    }
    [NoAutomaticTrigger]
    public static Task SyncAll(TextWriter log)
    {

        Console.WriteLine("SyncAll :"+DateTime.Now);
        return null;
        //await Task.Delay(10);
    }

    [NoAutomaticTrigger]
    public static Task SyncBranches(TextWriter log)
    {
        Console.WriteLine("SyncBranches :" + DateTime.Now);
        return null;
    }

    [NoAutomaticTrigger]
    public static Task SyncCustomers(TextWriter log)
    {
        Console.WriteLine("SyncCustomers :" + DateTime.Now);
        return null;
    }

    [NoAutomaticTrigger]
    public static Task SyncInventory(TextWriter log)
    {
        Console.WriteLine("SyncInventory :" + DateTime.Now);
        return null;
    }

3.使用Azure Storage Queue REST API创建队列消息。

4.从控制台检查结果。

enter image description here

关于c# - 具有 Http 触发功能的连续 Azure WebJob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42817438/

相关文章:

Azure Webjob 未连接到所连接网站的数据库

c# - 如何以编程方式将 DataGrid 列设置为 DatePicker 输入

c# - 如何计算转弯方向

c# - 是否可以仅使用用户名和密码从我的 .net core 应用程序登录 Azure?

azure - 错误代码: JA018 whie runnnig oozie workflow in HDInsight spark2 cluster

azure - 没有为 WebJob 注册路由

c# - bluemix vm openstack 使用c++语言

c# - 如何在 MVC 中自定义脚手架模板和更改 .cs.t4 文件

sql-server - 如何更改 Azure SQL 数据库中的时区?

Azure Web 作业 : Can't find Trace logging