编程语言
首页 > 编程语言> > c# – 我是否可以创建一个Azure Webjob,它将功能公开给仪表板但不使用Azure存储?

c# – 我是否可以创建一个Azure Webjob,它将功能公开给仪表板但不使用Azure存储?

作者:互联网

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

在这种情况下,我仍然可以将我的方法暴露给Azure函数调用仪表板吗?或者是Azure存储属性是唯一暴露的方法?

作为一个例子,我可能有一个功能:

ShowTotalNumRecordsProcessedToday()

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

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

解决方法:

无论是否使用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:直接在浏览器中编写代码,因此可能会出现一些错误.

标签:c,azure,azure-webjobs,azure-storage
来源: https://codeday.me/bug/20190528/1172822.html