下面我介紹處理設備通知邏輯的Azure Web App的一部分。我正在做的是從ASP MVC控制器調用下面顯示的代碼。 但是當我運行它時,我得到了來自瀏覽器的一個掛起的(永不停止的)請求。從Azure Web App發送到Azure IoT Hub設備時,消息會掛起(超時)
我以爲我發現一個解決方法是在BackgroundWorker線程中包裝SendAsync調用。這比較好,但我不正確。前幾次(一兩次)它可以正常工作,但是它再次發生,包裝的線程掛起。
該代碼與MSDN上的控制檯應用程序的代碼差別不大。我錯過了什麼?
using System.Web.Configuration;
using Microsoft.Azure.Devices;
namespace MyTest
{
public class Sender
{
private readonly string connectionString;
private readonly Microsoft.Azure.Devices.ServiceClient serviceClient;
public Sender()
{
connectionString = WebConfigurationManager.AppSettings["ConnectionString"];
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
}
public async void SendRequest(string deviceId, string msgText)
{
var message = new Message();
message.Properties.Add("text", msgText));
await serviceClient.SendAsync(deviceId, message);
}
}
}
你可以嘗試創建一個控制檯應用程序,並從網站上的控制檯應用程序運行它?您可以通過轉到https:// .scm.azurewebsites.net/DebugConsole並使用您的Azure帳戶登錄,從而使用Kudu的調試控制檯。我問這個問題的原因是試圖確定它是一個天藍色的問題還是異步/死鎖問題。您所描述的行爲聽起來像是ASP.NET線程中的異步死鎖,它會指向您正在使用的庫中的錯誤。 –
ahmelsayed