使用Azure计划的webjob调用方法

时间:2015-03-17 17:22:16

标签: c# azure asp.net-web-api azure-webjobs

我想使用Azure webjobs调用我的方法,如图所示。但我希望从3月13日到3月20日每隔5分钟运行一次,以进行本地测试。我按照article按照说明操作。我创建了测试存储帐户,然后将连接字符串添加到app.config和web.config文件中。

class Program
{
    static void Main()
    {
      var host = new JobHost();
      // The following code will invoke a function called ManualTrigger and 
      // pass in data (value in this case) to the function
      host.Call(typeof(Functions).GetMethod("GetStockAndStoreToDB"), new { value = 20 });   
      //host.RunAndBlock();
    }
}

在这里,我想调用我的方法GetStockAndStoreToDB。

public static void GetStockAndStoreToDB(TextWriter log, int value, [Queue("stockrequestqueue")] out string message)
{
    log.WriteLine("Function is invoked with value={0}", value);
    message = value.ToString();
    bool dbUpdate = false;
    string[] symbolArray = { "Yahoo", "Google", "Microsoft"};

    foreach (string symbol in symbolArray)
    {
        //Get Stock at closing
        var returnStock = Helper.GetDailyStock(symbol);
        message = symbol.ToString();
        //Store it to the DB            
        dbUpdate = Helper.SetHistoricStockToDB(returnStock);
    }
    log.WriteLine("Following message will be written on the Queue={0}", message);
}

预定作业的代码(每5分钟运行一次仅用于测试目的,一旦开始工作,我会将其更改为每天运行一次):

    {
        "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
        "webJobName": "StockWebJob",
        "startTime": "2015-03-13T16:00:00-05:00",
        "endTime": "2015-03-20T16:00:00-05:00", 
        "jobRecurrenceFrequency": "Minute", //"Day"
        "interval": 5, //1
        "runMode": "Scheduled"
    }

在我的控制器中:

    private CloudQueue stockRequestQueue;

    public StockController()
    {
        InitializeStorage();
    }

    private void InitializeStorage()
    {            
        // Open storage account using credentials from .cscfg file.
        var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ToString());
        //CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureWebJobsStorage"));

        // Get context object for working with queues, and 
        // set a default retry policy appropriate for a web user interface.
        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
        //queueClient.DefaultRequestOptions.RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(3), 3);

        // Get a reference to the queue.
        stockRequestQueue = queueClient.GetQueueReference("stockrequestqueue");
        // Create the queue if it doesn't already exist
        stockRequestQueue.CreateIfNotExists();

        // Create a message and add it to the queue.
        CloudQueueMessage queueMessage = new CloudQueueMessage("Get Stock");
        stockRequestQueue.AddMessage(queueMessage);
    }

请告诉我我做错了什么,因为控制台创建一次并停止,它不会每5分钟运行一次。如果我使用host.RunAndBlock();然后控制台是打开的,但它没有运行任何东西。

0 个答案:

没有答案