2014-09-12 53 views
4

我想要創建一個Azure Webjob來滿足批處理需求(具體來說,它將連續遍歷SQL Azure數據庫表獲取某些記錄,執行一些操作,然後更新表)。我不需要Azure存儲。我可以創建一個向儀表板公開功能但不使用Azure存儲的Azure Webjob嗎?

在這種情況下,我仍然可以將我的方法暴露給Azure函數調用儀表板?或者,只有具有Azure存儲屬性的方法纔會暴露?

例如,我可能有一個功能:

ShowTotalNumRecordsProcessedToday() 

,我想揭露和能夠從儀表板調用。我創建了一些公共測試函數,但它們不顯示在儀表板中。

我可以在我的情況下做到這一點嗎?

回答

5

您可以利用WebJobs SDK,無論您是否使用Azure存儲數據或不。

下面是一個使用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:直接在瀏覽器編寫的代碼,從而可能有一些錯誤。

+0

+1爲偉大的第二個例子!我已經意識到獲取函數索引並顯示在儀表板中的要求(如您在第二個示例中的註釋中所述) - > https://stackoverflow.com/questions/25811659/can-i-create-an-azure- webjob - 即-自曝函數對的儀表板,但-犯規/ 25819212?iemail = 1&noredirect = 1#comment40378135_25811820 – Emilio 2014-09-13 17:05:45

+0

我標記了你接受的答案,因爲你是第一位的,你的回答是正確的,但是你能給你的答案添加Lopez提到的有關連接字符串的要求嗎? – Emilio 2014-09-22 22:22:54

+0

您在顯示板中爲函數進行索引的明確資格列表是一個很棒的筆記,也是我在任何Azure WebJob SDK文檔中都沒有找到的筆記。非常感謝。 – BradV 2016-09-30 17:51:12

1

快速的回答是:

是的,你可以。您無需在Azure webjobs中與Azure存儲進行交互。我已經實現了一個與SQL Azure數據庫交互的處理器。您可以像任何其他webjob一樣從儀表板進行部署和運行。

希望這有助於

更新: 確保您配置的ConnectionString正確。也許這是問題。 請注意,如果您正在使用網站進行部署,則需要將連接字符串添加到Azure中的網站配置中。

enter image description here

+0

我比我甚至想象的更遠。我沒有意識到,除了你所說的,我必須:1)定義一個JobHost對象並啓動它(這可能很明顯,但是根據這個問題https://stackoverflow.com/questions/25811719/azure-webjobs -sdk-in-what-scenarios-is-creation-of-jobhost-object-required-在某些情況下不需要這樣做),2)將公共類和所有我想公開的方法標記到儀表板作爲公共靜態,並且3)在我想要公開給儀表板的方法中包含[NoAutomaticTrigger]屬性。 – Emilio 2014-09-12 16:29:50

相關問題