Thursday, January 22, 2015

VS 2010 Web Deploy Projects and web.config File Section Replacement

Intro

Let's pretend you want to have a different web.config file for live vs qa vs dev. If you're working on a web site (not web application) in Visual Studio you don't have access to web.release.config and web.debug.config, the easiest of the transformation methods. However, don't fret! With a web site you still have the option of creating a Web Deployment Project and enabling web.config file section replacement.

Why might you want to use this? Here's one common scenario. It's common knowledge that in the compilation element of your web.config, you don't want to leave it set to debug="true" in your production deployment. This has both security and performance implications and is just a good idea to have your site set to live mode rather than debug mode. Using web.config file section replacement with a Web Deployment project makes this quick and easy.

Creating a Web Deployment Project

For the remainder of this blog I'm assuming you already have a VS2010 solution with a web site project in it. If not, find one or create one. Onward...

Step #1 in getting all this going is creating a Web Deployment Project. Open up your VS 2010 solution (feels weird doing that; I've done nothing but VS2013 for this blog so far). Hey I suppose this probably works in VS2013 too, I just haven't tested it there. But I digress. Right-click the web site and select "Add Web Deployment Project...".


Name it whatever you like on the next screen and click "OK" when ready.


That was painless. You've got your first web deployment project ready to roll.



Web.config File Section Replacement

Open up the web.config if your site and scroll down to the "compilation" tag. It probably looks something like this:

    <compilation debug="true" targetFramework="4.0">

It's that debug attribute we want to do something about. You want to leave it true when you're debugging, as it makes debugging possible. But when you push this somewhere that the outside world can get at it, say on a production server, you want that set to false as it's more performant and more secure than the alternative. Let's do this! Right-click on your new web deployment project and select "Property Pages". On the window that comes up, click on "Deployment" in the left-side view. Check the box that says "Enable Web.config file section replacement" and put the following text in the big textbox: "system.web/compilation=web.compilation.config;". I tend to do this for both the Debug configuration (the drop-down in the upper-left) as well as the Release configuration, and if you're not sure which build configurations are used for what just do this in both of them. Click OK when you're done.


That's half the picture; we told the web deploy project to use a file called web.compilation.config, but that file doesn't exist yet. Create this file in your web site. Open up your web.config, copy the contents of the "compilation" tag and it's children, and paste it all into your new web.compilation.config file. Now set that debug attribute to "false" in the web.compilation.config file and save it. Mine looks like this, yours will be similar (though my sub-tags are probably different):

<compilation debug="false" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Speech, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </assemblies>
</compilation>


Arrsome! Now we have web.config file section replacement running, so what do we do with it? Post the site!

Deploying The Site Manually

The simplest method of posting your site to take advantage of replacement is to just copy the files. Right-click on the web deployment project and select "Build". When it's done building, right-click the web-deployment project in VS again and select "Open Folder in Windows Explorer". Open up either the "Debug" or "Release" folder (depending on which build configuration you're using), and voila! Them files be yer website. If you want to verify that your web.config file was properly transformed, just open it up from this folder and find the compilation element. It should have debug set to false.

Caveats

Web deployment projects are kind of slow to compile. For that reason you don't want to build the deploy project every time you compile your website. You can tell VS to only compile your web deploy project when you explicitly compile it (by right-clicking it and selecting "Build"). To do this, right-click your solution in VS and select "Configuration Manager...".


 On the Configuration Manager screen select the solution configuration(s) you want to change (I usually set both Debug and Release), and uncheck the "Build" checkbox for the deploy project. Click "Close" when you're done and save everything. Now when you build the solution the deploy project won't compile.



What's Next?

Web applications (not web sites) have a cleaner method of web.config transforms, using web.debug.config and web.release.config. Play around with it, see if you like it.

You could also see what other transforms are possible using the above method. Hint: You can transform just about any element, not just the "compilation" element!

No comments:

Post a Comment