NOTE: All code and instructions in this blog post were updated to work with the latest version of Web Deploy v1.1. A blog post that outlines the changes that are required to custom providers in Web Deploy v.1.1 since Web Deploy v1 RC is here.
The IIS Web Deployment Tool (also known as MSDeploy) simplifies the migration, management and deployment of IIS Web servers, Web applications and Web sites. MSDeploy also offers the ability to run custom providers. These providers unlock hidden power in MSDeploy.
In this article you will find a sample code for a custom MSDeploy provider that executes batch file commands as a part of MSDeploy sync. The batch file provider can be used to execute custom scripts before or after deployment to make the deployment process a seamless one. You can set up performance counters for a particular MSDeploy sync, make a batch file copy content with xcopy rather than with MSDeploy (but remember that we’re faster than xcopy!) to name just a few possibilities. The code in this article is meant as a sample to get you started on writing your own custom MSDeploy provider. This provider is similar to MSDeploy runCommand provider with the exception that this custom provider does not have the full set of features the official MSDeploy runCommand provider does. In any case, the code is built thankfully to a great sample and blog that Yamini Jagadeesan wrote a couple of months ago on MSDeploy MySql custom provider that lets you sync from one MySql database to another. If you haven’t familiarized yourself with the article, it is a great read and another good start-up solution for MSDeploy custom provider.
Download and Install
First, download the zip file with the projects (also contains Yamini’s MySql custom provider). BatchProvider solution contains the following files:
- BatchProviderFactory.cs – an entry point to your provider that kindly returns an instance of the provider in overridden CreateProvider method.
- BatchProvider.cs – the provider itself that initiates execution of a batch file.
- Strongname.pfx – the file with a key to sign the assembly for strong name verification. The password is “MSDeployRocks”, but you can delete the pfx file and specify your own key and password for singing the assembly by going to Project Properties-->Signing in Visual Studio.
To install the provider you need to
- Compile the project (or just use this dll)
- Create a folder called Extensibility under “%programfiles%\IIS\Microsoft Web Deploy”. This folder will contain all your MSDeploy custom providers
- Drop the dll into the newly created folder
MSDeploy is ready to work with the new provider.
The Internals of BatchProvider
- GetAttributes method is called on both source and destination to validate the object (think of performing a dump) without taking any action. If the object is invalid or if the object requires an action (in the example of the batch file provider, we need to run its commands in a shell program in a separate process), the method will throw a DeploymentException to make sure Add method is called. Finally, we want the code to take an action only when it is called on a destination object, which is insured by the following condition:
- Add method insures that the object is valid. Think of this method as a “Make valid” method that requires an action, since it is called only on a destination object. Destination objects will always be the objects that require an action, since dump can be performed only on source objects. Which makes sense, because in the command line, dump can be performed only with –source switch:
msdeploy –verb:dump –source:<destination>
- GetStream method is called when the source is getting synced to an archive directory (archiveDir provider) or a package (package provider). For example, this method will get called whenever the following commands are executed:
msdeploy –verb:sync –source:batch=”myBatchJob.bat” –dest:archiveDir=c:\backup
msdeploy –verb:sync –source:batch=”myBatchJob.bat” –dest:package=c:\backup.zip
msdeploy –verb:sync –source:manifest=”manifestWithBatchInIt.xml” –dest:package=c:\backup.zip
In GetStream method, the FileStream is returned, so that MSDeploy can attach a batch file to an archive or package as a simple text file.
You can tweak the provider to suit the task you have in mind. For example, you might want to tweak ProcessStartInfo of the process that will execute the batch file commands to control its behavior during sync. You might want to redirect output and error streams of the process to log the results of execution.
Please feel free to post any comments and questions.
Thanks to Yamini for pointing out these differences between MSDeploy versions.