【问题标题】:Can I create an Azure Webjob that exposes functions to the dashboard but doesn't use Azure Storage?我是否可以创建一个向仪表板公开功能但不使用 Azure 存储的 Azure Webjob?
【发布时间】:2016-05-29 07:53:03
【问题描述】:

我想创建一个 Azure Webjob 来满足批处理需求(具体来说,它会不断迭代 SQL Azure 数据库表,获取某些记录,执行一些操作,然后更新表)。我不需要 Azure 存储。

在这种情况下,我是否仍可以将我的方法公开给 Azure 函数调用仪表板?还是只有具有 Azure 存储属性的方法才会暴露?

例如,我可能有一个函数:

ShowTotalNumRecordsProcessedToday()

我想公开并能够从仪表板调用。我创建了一些公共测试功能,但它们没有显示在仪表板中。

我可以在我的场景中这样做吗?

【问题讨论】:

    标签: c# azure azure-storage azure-webjobs


    【解决方案1】:

    无论您是否使用 Azure 存储来存储数据,您都可以利用 WebJobs SDK。

    这是一个使用 SDK 进行日志记录的作业示例:

    public static void Main
    {
         using(JobHost host = new JobHost())
         {
             // Invoke the function from code
             host.Call(typeof(Program).GetMethod("DoSomething"));
    
             // The RunAndBlock here is optional. However,
             // if you want to be able to invoke the function below
             // from the dashboard, you need the host to be running
             host.RunAndBlock();
             // Alternative to RunAndBlock is Host.Start and you
             // have to create your own infinite loop that keeps the
             // process alive
        }
    }
    
    // In order for a function to be indexed and visible in the dashboard it has to 
    // - be in a public class
    // - be public and static
    // - have at least one WebJobs SDK attribute
    [NoAutomaticTrigger]
    public static void DoSomething(TextWriter log)
    {
        log.WriteLine("Doing something. Maybe some SQL stuff?");
    }
    

    但是,您需要一个存储帐户来连接主机和仪表板。

    您还可以为 SQL 或类似的任何东西创建自己的“自定义触发器”:

    public static void Main
    {
         using (JobHost host = new JobHost())
         {
             host.Start();
    
             while (!TerminationCondition)
             {
                 if (SomeConditionRequiredForTheTrigger)
                 {
                     host.Call(typeof(Program).GetMethod("DoSomething"));
                 }
                 Thread.Sleep(500);
             }
    
             host.Stop();
        }
    }
    
    // In order for a function to be indexed and visible in the dashboard it has to 
    // - be in a public class
    // - be public and static
    // - have at least one WebJobs SDK attribute
    [NoAutomaticTrigger]
    public static void DoSomething(TextWriter log)
    {
        log.WriteLine("Doing something. Maybe some SQL stuff?");
    }
    

    PS:直接在浏览器中写代码,可能会出现一些错误。

    【讨论】:

    • +1 是第二个很棒的例子!我已经意识到在仪表板中对函数进行索引和显示的要求(正如您在第二个示例的 cmets 中放置的那样)--> stackoverflow.com/questions/25811659/…
    • 我将您的答案标记为已接受,因为您是第一个并且您的答案是正确的,但是您能否在您的答案中添加 Lopez 提到的关于连接字符串的要求?
    • 您在仪表板中要索引的函数的明确资格列表是一个很好的说明,我还没有在任何 Azure WebJob SDK 文档中找到它。非常感谢。
    【解决方案2】:

    快速回答是:

    是的,你可以。您无需在 Azure Webjobs 中与 Azure 存储进行交互。我已经实现了一个与 SQL Azure 数据库交互的处理器。您可以像任何其他网络作业一样从仪表板部署和运行它。

    希望对你有帮助

    更新: 确保正确配置连接字符串。也许这就是问题所在。 附带说明一下,如果您使用网站进行部署,则需要将连接字符串添加到 Azure 中的网站配置。

    【讨论】:

    • 我比我想象的还要离谱。我没有意识到除了您所说的之外,我还必须:1)定义一个 JobHost 对象并启动它(这可能很明显,但是根据这个问题stackoverflow.com/questions/25811719/… 在某些情况下没有必要这样做), 2) 将类 public 和我想向仪表板公开的所有方法标记为公共静态,3) 在我想向仪表板公开的方法上包含 [NoAutomaticTrigger] 属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多