2015-10-06 121 views
2

如何以编程方式在Azure上重新启动Web应用程序和API应用程序?以编程方式重新启动Azure上的Web/Api-App

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

+0

为什么你需要重新启动它们? –

+0

重新创建DocumentDB集合后,存储库的某些静态成员不再有效。所以在这种情况下(不是每天使用),它似乎是最简单的方法。 –

+0

你是否检查过powershell/cli apis?他们应该有一个重新启动或停止/启动api。最糟糕的情况下,你可以杀死网站上的w3wp进程:) –

回答

1

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

有关如何从Azure网站内部创建新网站的示例,请参阅this page。重新启动Web服务的工作方式与创建新服务类似。有关可用网站相关方法的列表,请参阅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模块。有关以编程方式运行PowerShell cmdlet的更多信息,请参阅this page

相关问题