如何以编程方式重新启动 Azure 上的 Web-Apps 和 API-Apps?
(我想从同一个 App 服务计划中的另一个 API-App 调用它。)
如何以编程方式重新启动 Azure 上的 Web-Apps 和 API-Apps?
(我想从同一个 App 服务计划中的另一个 API-App 调用它。)
还有“ Microsoft Azure 管理库”Nuget,它允许您从应用程序内部使用 Azure 服务。
有关如何从 Azure 网站内部创建新网站的示例,请参阅此页面。重新启动 Web 服务的工作方式与创建新服务类似。有关可用网站相关方法的列表,请参阅此页面。
此外,对于身份验证,使用基于证书的身份验证,有关详细信息,请参阅此页面。
Bellow 是一个简短的命令行程序,它将重新启动您在 Azure 订阅中获得的所有网络空间中的所有网站。它有点像 Azure 网站的 iisreset。
该代码基于从前面提到的链接中获取的示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Management.WebSites;
using Microsoft.WindowsAzure;
using System.Security.Cryptography.X509Certificates;
using Microsoft.WindowsAzure.Management.WebSites.Models;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var subscriptionId = "[INSERT_YOUR_SUBSCRIPTION_ID_HERE]";
var cred = new CertificateCloudCredentials(subscriptionId, GetCertificate());
var client = new WebSiteManagementClient(cred);
WebSpacesListResponse webspaces = client.WebSpaces.List();
webspaces.Select(p =>
{
Console.WriteLine("Processing webspace {0}", p.Name);
WebSpacesListWebSitesResponse websitesInWebspace = client.WebSpaces.ListWebSites(p.Name,
new WebSiteListParameters()
{
});
websitesInWebspace.Select(o =>
{
Console.Write(" - Restarting {0} ... ", o.Name);
OperationResponse operation = client.WebSites.Restart(p.Name, o.Name);
Console.WriteLine(operation.StatusCode.ToString());
return o;
}).ToArray();
return p;
}).ToArray();
if(System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine("Press anykey to exit");
Console.Read();
}
}
private static X509Certificate2 GetCertificate()
{
string certPath = Environment.CurrentDirectory + "\\" + "[NAME_OF_PFX_CERTIFICATE]";
var x509Cert = new X509Certificate2(certPath,"[PASSWORD_FOR_PFX_CERTIFICATE]");
return x509Cert;
}
}
}
另一种选择是,如果您无法从上述库中找到所需的功能,您还可以从应用程序内部以编程方式运行 powershell 命令。您很可能需要将应该运行这些 cmdlet 的应用程序移动到虚拟机,以便能够加载所需的 powershell 模块。有关以编程方式运行 powershell cmdlet 的更多信息,请参阅此页面。
我认为处理基本 REST API 是更好的选择。Azure SDK 变化很大,缺乏好的文档。
这是一个最新的示例代码: https ://github.com/davidebbo/AzureWebsitesSamples/
您可以根据自己的需要进行调整。
您可以使用 Powershell 来执行此操作。相关命令为:
开始-Azure网站-名称“xxxx”</p>
停止-Azure网站-名称“xxxx”</p>
您可以在以下链接中找到有关这些命令的帮助: https ://msdn.microsoft.com/en-us/library/azure/dn495288.aspx https://msdn.microsoft.com/en-us/library/azure /dn495185.aspx