Massoud Mazar

Sharing The Knowledge

NAVIGATION - SEARCH

Using Web Farm Framework to get list of Servers

Information about Web Farm Framework API is hard to find, so I think this may help some others like me who are interested in using WFF and web deploy APIs.

In this case, I wanted to get a list of all servers in a particular Web Farm and then push a file to all of them.

First step is to add references to these libreries in your project:

Microsoft.Web.Deployment
Microsoft.web.Farm 

The DLLs for these libraries are located in respective folders under C:\Program Files\IIS

I used the following helper methods to do what I needed.

    public class DeploymentHelper
     {
  
         public static List GetWebFarmServerNames(string WebFarmName)
         {
             List ServerNames = new List();
  
             using (WebFarmManager manager = new WebFarmManager())
             {
                 // Make sure webfarm is valid
                 WebFarm webFarm = null;
                 if (!manager.WebFarms.TryGetWebFarm(WebFarmName, out webFarm))
                 {
                     throw new ArgumentException("WebFarm does not exist");
                 }
  
                 foreach (Server server in webFarm.Servers)
                 {
                     ServerNames.Add(server.Address);
                 }
             }
  
             return ServerNames;
         }
  
  
         public static void SyncFolder(string SourcePath, string DestinationPath, List DestinationServers)
         {
             DeploymentWellKnownProvider provider = DeploymentWellKnownProvider.ContentPath;
             DeploymentSyncOptions syncOptions = new DeploymentSyncOptions();
             DeploymentBaseOptions sourceBaseOptions = new DeploymentBaseOptions();
             DeploymentBaseOptions destinationBaseOptions = new DeploymentBaseOptions();
             DeploymentObject deploymentObject = DeploymentManager.CreateObject(provider, SourcePath, sourceBaseOptions);
  
             foreach (string server in DestinationServers)
             {
                 destinationBaseOptions.ComputerName = server;
                 deploymentObject.SyncTo(provider, DestinationPath, destinationBaseOptions, syncOptions);
             }
         }
  
         public static void SyncFile(string SourcePath, string DestinationPath, List DestinationServers)
         {
             DeploymentWellKnownProvider provider = DeploymentWellKnownProvider.FilePath;
             DeploymentSyncOptions syncOptions = new DeploymentSyncOptions();
             DeploymentBaseOptions sourceBaseOptions = new DeploymentBaseOptions();
             DeploymentBaseOptions destinationBaseOptions = new DeploymentBaseOptions();
             DeploymentObject deploymentObject = DeploymentManager.CreateObject(provider, SourcePath, sourceBaseOptions);
  
             foreach (string server in DestinationServers)
             {
                 destinationBaseOptions.ComputerName = server;
                 deploymentObject.SyncTo(provider, DestinationPath, destinationBaseOptions, syncOptions);
             }
         }
  
     }

Add comment