This example will help you out in cases when you have to deploy a solution in many workstations, without the need of copying the files in any of them manually. The strategy used is to store the compressed program into a repository and give the ability to each workstation for updating the program if necessary and run it, all in one step.
In order to accomplish this we will create two C# console projects with the proper app.config file, to cover the following tasks:
- Import the new deployment into a repository.
- Export the new deployment from the repository.
- Execute the latest deployment.
Note: As for the repository, this example uses the main app database but it’s also suitable for file repository as well.
Import Console program
This console is in charge of importing the new package into the repository, therefore the main operations must be getting the new package, convert it into byte and make it available for further use. This console is only suitable for the database approach, as none of this is necessary if you prefer/need to use a ftp or shared folder.
In the first line we take the sourcePath key from the config file, that is, the path where will be allocated the new program. As for the second one, we convert the zip file into a byte array.
Finally, fileData is inserted into the BINPACKAGES table, by passing it as sqlparameter as binary type. If you check the source code on github, you’ll see I chose deleting all rows before inserting the program. This’s optional but recommended.
Deploy & Run Console program
This console will compare assembly information between the program from the repository and local one, install the new version if needed and then, run the program in the workstation.
As you can see, we get the record (bintable DataSet) from the database and extract the latest program into the path configured by the newPath key.
Then we must use assembly information to verify if we need to update the program, therefore we extract the AssemblyName property from both exe files.
In case the repository program is newer we start deploying it. As the processing is taking place, we need to make sure the temp path exists when exporting the new content, so if it does not we create it and once everything is prepared, all files are overwritten. The last line executes the current program (already updated if it was the case).
App Config sample
Of course this’s a rough solution and needs to be improved, or even there’re better approaches, but my intention with this post was to show you one of many ways to achieve tasks like the one proposed here.
I’ll upload both App.Config and Program.cs files (the console programs will be placed in one sample file) on binaryupdate github repo so you can check it also there.