2016-07-13 22 views
7

Tôi đã tạo một ứng dụng bảng điều khiển tải lên dưới dạng Azure kích hoạt Webjob. Nó hoạt động tốt khi tôi chạy nó từ Azure Portal. Tôi muốn chạy điều này từ mã C# của tôi. Tôi không muốn sử dụng hàng đợi hoặc xe buýt dịch vụ. Tôi chỉ muốn kích hoạt nó khi người dùng thực hiện một hành động cụ thể trong ứng dụng web của tôi.Chạy Azure WebJob được kích hoạt từ Mã số

Sau khi tìm kiếm tôi có một giải pháp để kích hoạt công việc từ một dự http://blog.davidebbo.com/2015/05/scheduled-webjob.html

ý tưởng Bất kỳ thế nào để chạy từ mã?

Trả lời

6

Như Justin nói, chúng ta có thể sử dụng WebJob API để đạt được yêu cầu này. Chúng tôi có thể tìm thấy API KUDU này tại: https://github.com/projectkudu/kudu/wiki/WebJobs-API. Dưới đây là mã thử nghiệm của tôi:

  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://<web appname>.scm.azurewebsites.net/api/triggeredwebjobs/<web job name>/run"); 
      request.Method = "POST"; 
      var byteArray = Encoding.ASCII.GetBytes("user:password"); //we could find user name and password in Azure web app publish profile 
      request.Headers.Add("Authorization", "Basic "+ Convert.ToBase64String(byteArray));    
      request.ContentLength = 0; 
      try 
      { 
       var response = (HttpWebResponse)request.GetResponse(); 
      } 
      catch (Exception e) { 

      } 

Nó hoạt động đứng về phía tôi. Hy vọng nó giúp.

7

Bạn có thể kích hoạt WebJob qua API WebJob. Code C# bao gồm trong bài sau đây:

http://chriskirby.net/blog/running-your-azure-webjobs-with-the-kudu-api

HttpClient client = new HttpClient(); 
client.BaseAddress = new Uri("https://mysiteslot.scm.azurewebsites.net/api/"); 
// the creds from my .publishsettings file 
var byteArray = Encoding.ASCII.GetBytes("username:password"); 
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); 
// POST to the run action for my job 
var response = await client.PostAsync("triggeredwebjobs/moJobName/run", null) 
Các vấn đề liên quan