Massoud Mazar

Sharing The Knowledge


Passing parameters to a custom Web Deployment Provider

Recently I spent numerous hours trying to find out how I can pass more parameters than just a path to my custom Deployment provider. It is amazin how simple it is when you finally find the solution. In my case, I needed to send a key-value pair from my C# code to the provider, so it can find the key in a config file and change the value for me. Here is the code for the calling program:

public static void ChangeConfig(string RemoteServer, string Path, string key, string value, string username, string password)
    Console.WriteLine("{0}: update {1} config to {2}", RemoteServer, key, value);
    DeploymentBaseOptions destBaseOptions = new DeploymentBaseOptions();
    destBaseOptions.ComputerName = RemoteServer;
    destBaseOptions.UserName = username;
    destBaseOptions.Password = password;
    DeploymentProviderOptions opts = new DeploymentProviderOptions("ConfigChange");
    opts.Path = Path;
    using (DeploymentObject depObject = DeploymentManager.CreateObject(opts, new DeploymentBaseOptions()))
        depObject.SyncParameters.Add(new DeploymentSyncParameter("Key", "", key, ""));
        depObject.SyncParameters.Add(new DeploymentSyncParameter("Value", "", value, ""));
        DeploymentChangeSummary results = depObject.SyncTo(DeploymentWellKnownProvider.Auto, string.Empty, destBaseOptions, new DeploymentSyncOptions());

And here is the code in provider which receives the parameters:

public override void Add(DeploymentObject source, bool whatIf)
    // This is called on the Destination so this.FilePath is the dest path not source path
    if (!whatIf && File.Exists(source.ProviderContext.Path))
        // Open the config file of the executable
        Configuration config = ConfigurationManager.OpenExeConfiguration(source.ProviderContext.Path);
        // Apply the change
        config.AppSettings.Settings[source.SyncParameters["Key"].Value].Value = source.SyncParameters["Value"].Value;
        // Save the configuration file.

Add comment