Run Existing Web Farm Framework 2.0 providers using CreateRunOptions

Posted: Oct 04, 2010  1 comments  

Average Rating

Tags
Web Farm Framework
WebFarmFX

Share this Post

Overview

Web Farm Framework lets you run the existing custom and built-in providers using CreateRunOptions. There are lot of uses of it, you can use it to write custom providers that make use of existing functionality or write a test infrastructure using it.

This post will help you understand on how to call existing provider using CreateRunOptions.

Details

The program below calls the existing providers using CreateRunOptions. The first call gets the installed products on the whole web farm. The provider does not expect any parameters.

The second call tries to start web management service (wmsvc). The call takes in three parameters, first parameter is the name of the service, second parameter is to ignore any service errors, and third parameter is the desired status of the service.

The third call makes use of existing RebootProvider to reboot the whole web farm. Web Farm Framework makes use of maximumStoppedServers attribute in the webfarm.xml to reboot the whole web farm. 

using System;
using 
System.Collections.Generic;
using 
System.IO;
using 
System.Text;
using 
System.Xml;
using 
System.Xml.Serialization;
using 
Microsoft.Web.Farm;

namespace 
CreateRunOptions {
    
class Program {
        
private const string WebFarmName "WFF";
        static void 
Main(string[] args) {
            
try {
                
// Query Installed Products on the whole webfarm
                
Object returnedObject RunOperation(WebFarmName, string.Empty, GetInstalledProductsOperationProvider.ProviderName, nullfalse);
                
Console.WriteLine(GetResult(returnedObject));

                
// Start and stop a service on a particular machine
                
List<KeyValuePair<stringstring>> parameters = new List<KeyValuePair<stringstring>>();
                
parameters.Add(new KeyValuePair<stringstring>(ControlServiceOperationProvider.ServiceParameterName, "wmsvc"));
                
parameters.Add(new KeyValuePair<stringstring>(ControlServiceOperationProvider.IgnoreServiceErrorsParameterName, "true"));
                
parameters.Add(new KeyValuePair<stringstring>(ControlServiceOperationProvider.ControllerStatusParameterName, "Running"));
                
RunOperation(WebFarmName, "demoprimary", ControlServiceOperationProvider.ProviderName, parameters, true);

                
// Reboot the whole webFarm
                
parameters = new List<KeyValuePair<stringstring>>();
                
parameters.Add(new KeyValuePair<stringstring>(RebootOperationProvider.RebootReasonParameterName, "AdministrativeReboot"));
                
RunOperation(WebFarmName, string.Empty, RebootOperationProvider.ProviderName, parameters, true);
            
}
            
catch (Exception ex) {
                Console.WriteLine(ex.ToString())
;
            
}
        }

        
private static string GetResult(Object obj) {
            
if (obj == null) {
                
throw new ArgumentNullException("obj");
            
}

            
using (MemoryStream ms = new MemoryStream()) {
                XmlSerializer serializer 
= new XmlSerializer(obj.GetType(), GetSecondaryTypes(obj));
                using 
(XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, Encoding.UTF8)) {
                    xmlTextWriter.Formatting 
Formatting.Indented;
                    
serializer.Serialize(xmlTextWriter, obj);
                    using 
(MemoryStream newStream (MemoryStream)xmlTextWriter.BaseStream) {
                        
return Encoding.UTF8.GetString(newStream.ToArray());
                    
}
                }
            }
        }

        
private static Type[] GetSecondaryTypes(object obj) {
            List<Type> secondaryTypes 
= new List<Type>();
            
WebFarmOperationResult[] farmResults obj as WebFarmOperationResult[];
            if 
(farmResults != null) {
                
foreach (WebFarmOperationResult result in farmResults) {
                    
if (result.Value != null) {
                        secondaryTypes.Add(result.Value.GetType())
;
                    
}
                }
            }

            
return secondaryTypes.ToArray();
        
}

        
private static object RunOperation(string webFarmName,
            
string serverAddress,
            
string providerName,
            IEnumerable<KeyValuePair<
stringstring>> parameters,
            
bool completeImmidiately) {
            
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");
                
}

                
// if server not empty, make sure its  valid
                
Server server = null;
                if 
(!string.IsNullOrEmpty(serverAddress)) {
                    
if (!webFarm.Servers.TryGetServer(serverAddress, out server)) {
                        
throw new ArgumentException("Server does not exist");
                    
}
                }

                
// Create the appropriate runOptions
                
RunOperationOptions options = null;
                if 
(server != null) {
                    options 
server.CreateRunOperationOptions(providerName);
                
}
                
else {
                    options 
webFarm.CreateRunOperationOptions(providerName);
                
}
                
                
// Should the operation return immidiately?
                
options.CompleteImmediately completeImmidiately;

                
// Add Parameters
                
if (parameters != null) {
                    
foreach (KeyValuePair<stringstring> parameter in parameters) {
                        options.Parameters[parameter.Key].Value 
parameter.Value;
                    
}
                }

                
// run the options
                
if (server != null) {
                    
return server.RunOperation(options);
                
}
                
else {
                    
return webFarm.RunOperation(options);
                
}
            }
        }
    }
}

Submit a Comment

  • Plain text is accepted.
  • URLs starting with http:// are converted to links.