以编程方式在 Azure 上重新启动 Web/Api-App

Restart Web/Api-App on Azure programmatically

如何以编程方式重启 Azure 上的 Web 应用程序和 API 应用程序?

(我想从同一应用程序服务计划中的另一个 API-应用程序调用它。)

您可以使用 Powershell 来执行此操作。相关指令为:

开始-AzureWebsite -名称“xxxx”

停止-AzureWebsite -名称“xxxx”

您可以在以下链接中找到有关这些命令的帮助: https://msdn.microsoft.com/en-us/library/azure/dn495288.aspx https://msdn.microsoft.com/en-us/library/azure/dn495185.aspx

还有“Microsoft Azure Management Libraries”Nuget,它允许您从应用程序内部使用 Azure 服务。

有关可用网站相关方法的列表,请参阅 this page for an example on how to create new web sites from inside of an Azure Web site. Restarting web services work in a similar way to creating new services. See this page

此外,使用基于证书的身份验证进行身份验证,有关详细信息,请参阅 this page

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 模块。有关以编程方式 运行ning powershell cmdlet 的详细信息,请参阅 this page

我认为处理基础 REST API 是更好的选择。 Azure SDK 变化很大并且缺乏很好的文档。

这是最新的示例代码: https://github.com/davidebbo/AzureWebsitesSamples/

您可以根据自己的需要进行调整。