2017-10-13 119 views
2

我創建了一個.NET CORE控制檯應用程序,並作爲連續模式webjob上載到Azure應用程序(ASP.NET Core)。使用webjob運行時,webapp響應API請求的響應非常慢(請求時間上升到幾秒)。Webjob導致ASP.NET核心性能問題

WebJob代碼

static void Main(string[] args) 
     { 
queueClient.RegisterMessageHandler(
       async (message, token) => 
       { 

        // Process the message      
        // Complete the message so that it is not received again. 
        // This can be done only if the queueClient is opened in ReceiveMode.PeekLock mode. 
        await queueClient.CompleteAsync(message.SystemProperties.LockToken); 


       }, 
       new MessageHandlerOptions(exce => { 
        return Task.CompletedTask; 
       }) 
       { MaxConcurrentCalls = 1, AutoComplete = false }); 
      //Console.ReadKey(); 
      while (true) ; 
    } 

和消息操作花費幾秒鐘的處理。

從SCM控制檯 enter image description here

請求時間 enter image description here

回答

3

while(true) ;將針的CPU,所以我建議你不要做。

檢查隊列消息處理例如,對於如何實現信息處理的正確方法:https://github.com/Azure/azure-webjobs-sdk/wiki/Queues

你將不得不改變你的Main到:

static void Main(string[] args) 
{ 
    JobHost host = new JobHost(); 
    host.RunAndBlock(); 
} 

然後你就可以做出一個消息處理程序另一個文件:

public static void ProcessQueueMessage([QueueTrigger("logqueue")] string logMessage, TextWriter logger) 
{ 
    logger.WriteLine(logMessage); 
} 

的3.0.0的Microsoft.Azure.WebJobs庫支持的預覽版.NE T Standard 2.0,因此可以與.NET Core 2.0項目一起使用。

如果你想自己實現它,你可以檢查Webjobs SDK源代碼的例子,他們是如何做到這一點:https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.Host/JobHost.cs

+0

我想這個代碼是有效的.Net完整的框架,.NET Core不還有WebJob SDK支持。看起來你的答案基於WebJob SDK。使用.NET Core,我們必須手動管理所有內容。 – tssutha

+0

3.0 beta版本與Net Standard 2.0兼容,所以它應該可以工作。 – juunas

+0

我會看看,我還沒有將應用程序更新到.Net Core 2.0,但正如我們試驗的那樣,我們遇到了EF核心2.0的一些問題,等待修復錯誤。 – tssutha