我们有一个“始终开启”的 ASP.NET Web 应用程序,正在运行一个长时间任务。为了避免同时运行此任务两次或多次,在任务开始时将一个标志设置为数据库。如果该任务被强制关闭,则该标志不会被删除,并且该任务在没有手动干预的情况下不会再次运行。
我一直在寻找Azure中是否存在回收网站的概念,但我没有找到太多相关信息。我发现例如 https://stackoverflow.com/a/21841469/1081568似乎永远不会执行回收,但我发现有些人提示网络应用程序设置为“始终打开”,但会随机回收。
我想知道在什么情况下可以在 Azure 中回收/关闭应用程序?只是为了维护吗? Azure 回收 asp.net Web 应用程序?或者是本地服务器独有的概念?
还有一个问题,有没有办法从 Azure 捕获此关闭/回收,并在运行任务正在运行时优雅地停止它。
谢谢
最佳答案
据我所知,如果您将网络应用程序设置为“始终打开”,通常情况下,azure不会回收您的网络应用程序的资源。
如果网络应用程序的“始终打开”设置关闭,这意味着网站将在不活动一段时间(20 分钟)后回收。
And another question, Is there a way to capture this shutdown/recycle from Azure and stop my running task gracefully if it's running.
根据您的描述,我建议您可以发送 kudu Restapi 请求来获取当前 Web 应用程序的 processid。
如果应用程序重新启动,processid将会更改。通过比较processid,您可以捕获此Web应用程序被回收。
更多关于如何获取当前Web应用的processid的详细信息,您可以引用以下步骤:
1.在您的 Azure Web 应用程序中设置部署凭据,如下所示:
注意:请记住用户名和密码,我们将使用它们来生成访问 token
2.发送请求到以下url获取进程信息。
网址:https://yourwebsitename.scm.azurewebsites.net/api/processes
代码示例:
string url = @"https://yourwebsitename.scm.azurewebsites.net/api/processes";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "GET";
httpWebRequest.ContentLength = 0;
string logininforation = "username:password";
byte[] byt = System.Text.Encoding.UTF8.GetBytes(logininforation);
string encode = Convert.ToBase64String(byt);
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encode);
using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (System.IO.StreamReader r = new System.IO.StreamReader(response.GetResponseStream()))
{
string jsonResponse = r.ReadToEnd();
dynamic result = JsonConvert.DeserializeObject(jsonResponse);
dynamic resultList = result.Children();
foreach (var item in resultList)
{
Console.WriteLine(item.name + " : " + item.id);
}
}
}
结果:
您还可以在门户中找到 processid。
选择您的网络应用程序 --> 流程浏览器
关于asp.net - Asp.net Web App何时在Azure中被回收?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44307616/