Tải bản đầy đủ
Objective 5.5: Create, configure, and publish a web package

Objective 5.5: Create, configure, and publish a web package

Tải bản đầy đủ

This objective covers how to:
■■

Create an IIS InstallPackage

■■

Configure the build process to output a web package

■■

Apply pre- and post-condition actions

■■

Include appropriate assets

Creating an IIS InstallPackage
There are several ways to create a Web Deployment package. You can use Visual Studio, IIS,
the command prompt, or Windows PowerShell to create and deploy your packages. Visual
Studio is the easiest way to deploy; Windows PowerShell and the command prompt give you
a lot of power but are more difficult to use.

Visual Studio
In Visual Studio, you can use the Publish tool to create a Web Deploy package. When doing
this you can add database script files, change the database connections in your web.config
and execute EF Code First Migrations on destination.
Visual Studio does not offer you all the features that you can configure when creating a
package, but it’s one of the easiest ways to create a package.
You start the publish process by choosing Publish from the Build
menu. Figure 5-14 shows the start page of your wizard. The first time, you need to create a
publish profile. This profile is stored as a part of your solution. This way, you can add your
profile to source control and reuse it each time you publish your app. This is called one-click
publishing. Next time, you only need to click Publish, and a Web Deploy will be automatically
executed. You can create multiple profiles for different scenarios. Maybe you are running in
a DTAP (development, test, acceptance, production) environment and you want a publish
profile for each of these locations. By creating multiple profiles, you can easily store your
deployment settings.



Objective 5.5: Create, configure, and publish a web package

CHAPTER 5

407

FIGURE 5-14  Publish Web Wizard

When deploying to Windows Azure Websites, you don’t have to create the publish profile
manually. When you log in to your Windows Azure Management Portal, you can download a
publish profile for your website. If you import this file into Visual Studio, your project is automatically configured for deployment to your Windows Azure Websites.
If you want to create your own profile, you can create a new one, give it a name, and then
navigate to the second page of the wizard. Here you can select your publish method. If you
want to use Web Deploy, you need to choose Web Deploy or Web Deploy Package.
Web Deploy directly deploys your application to a remote server that has the Web Deploy
service installed. For this to work, you need to enter the address of the server and the credentials for connecting to the server.
MORE INFO  CONFIGURING YOUR SERVER FOR WEB DEPLOY

For instructions on how to configure your web server for Web Deploy, see http://www.iis.
net/learn/install/installing-publishing-technologies/installing-and-configuring-web-deploy.

If you want to create a package, select the Web Deploy Package option (see Figure 5-15).
Now you can enter a location where you want to store the package and the name of the site
you want to deploy to (this can be changed later when deploying the package).

408

CHAPTER 5

Deploying web applications and services

FIGURE 5-15  Selecting a publish method

The third page of your wizard enables you to configure additional settings (see Figure
5-16). On this page, you can select your deployment configuration (Release by default),
precompile your site, exclude files from App_Data, and configure your database.



Objective 5.5: Create, configure, and publish a web package

CHAPTER 5

409

FIGURE 5-16  Configuring additional publish settings

After you click Publish, Visual Studio will build a Web Deploy package for you. You can
then use this package and deploy it to IIS, both by manually importing it from IIS Manager or
from the command line.
There are some extra options that you can configure through the properties page of your
project. If you open the project properties, you will see a page for Package/Publish Web and
Package/Publish SQL.
On the Package/Publish Web page, you can select the files you want to deploy. This way,
you can choose the assets that you want to deploy with your project. You can select to only
deploy the required files, all files in your project, or all files stored in your project folder. You
can also select an option to include your IIS Express settings and deploy them to the target
server. This way, you can be sure that every deployment server is correctly configured.
The Package/Publish SQL page is disabled by default, showing a message that you can
now control these settings through the Publish Wizard. If you want, you can enable the settings page and configure settings for database deployment.

410

CHAPTER 5

Deploying web applications and services

If you use the Entity Framework, you can use Code First Migrations to update the target
database on deployment. You can also use more complex features such as the dbDacFx or
dbFullSql Provider.
MORE INFO  CONFIGURING DATABASE DEPLOYMENT FOR WEB DEPLOY

For more information on how to deploy your database, see http://msdn.microsoft.com/
en-us/library/dd394698.aspx.

IIS
The IIS Manager supports both creating and importing Web Deployment packages. You can
create a package for an application, site, or even a complete server. IIS provides you with
more control over the creation of your package than Visual Studio does.
To integrate Web Deploy with IIS, you need to run the installer that you can find at http://
www.iis.net/downloads/microsoft/web-deploy. It will add extra options to your IIS Manager for
importing and exporting applications.
MORE INFO  CONFIGURING A SERVER FOR WEB DEPLOY

When installing Web Deploy on a server, you need to configure the server to enable remote publishing. This way, you can use Web Deploy to remotely install a Web Deployment
package on your server.
You can find detailed instructions on how to configure your server at the ASP.NET website:
http://www.asp.net/web-forms/tutorials/deployment/configuring-server-environments-forweb-deployment/configuring-a-web-server-for-web-deploy-publishing-(remote-agent).

When exporting a complete server package, you create a Web Deploy that contains the
configuration for your web server. This includes configuration from applicationHost.config,
IIS registry settings, SSL certificates, and the content of all web applications hosted on your
server.
You export a package by opening the IIS Manager and then selecting the application,
site, or server that you want to export. By clicking Export Application in the right action
pane, you start a wizard that will help you configure your package. You can see the wizard
in Figure 5-17.



Objective 5.5: Create, configure, and publish a web package

CHAPTER 5

411

FIGURE 5-17  Configuring additional publish settings

If you select the Manage Components option, you can configure additional providers for
things like setting registry keys or deploying assemblies to the GAC. You can also select a
provider to deploy a database with your package.
The next step in the wizard enables you to configure parameters for the providers you
have configured. The parameters are used on deployment, so you can configure the providers
you have selected. You can use a parameter for the name of your website or the location of
the database, for example.
This is useful when you want to deploy your package to a testing and production environment. You probably want to use the same database schema, but vary the location of your database servers. By using a parameter, you can change this value when executing your deployment.
MORE INFO  EXPORTING A PACKAGE FROM IIS

For a complete example of exporting a package from IIS, see http://www.iis.net/learn/
publish/using-web-deploy/export-a-package-through-iis-manager.

After creating your package, you can now import it in IIS. You do this by selecting your
server or site and selecting the Import Package options in the right action pane. Packages
that you import this way can be created by Visual Studio, by IIS, or from the command
prompt. After selecting the location of your package, you go through a wizard in which you
can select the content you want to deploy. You also specify the values for the parameters that
you created when exporting your package.

412

CHAPTER 5

Deploying web applications and services

When importing a package, you have the option of setting the whatif flag, which shows
you the list of actions that will be performed during deployment without actually performing
the deployment. This way, you can see whether everything is configured correctly.

Command-line tools
In addition to using tools such as Visual Studio and IIS, you can also use command-line tools.
Although they are harder to work with, they offer you a lot of flexibility. Especially when you
want to set up a continuous deployment process, you will want to look at the command-line
tools because they can be integrated with your build process.
NOTE  LOCATION OF MSDEPLOY

Normally, MSDeploy is added to your environment variables so you can access it from a
command prompt. If you experience problems running MSDeploy, make sure that you
have installed Visual Studio 2012 Service Pack 1.

One such command-line tool is MSDeploy. You can use MSDeploy for the following:
■■

Live server-to-server synchronization

■■

Package creation

■■

Package deployment

When working with MSDeploy, make sure that you are an administrator. The following
command, for example, executes a synchronization between two servers with the whatif flag:
msdeploy -whatif -verb:sync -source:iisApp="Default Web Site/MyApp"
-dest:iisApp="Default Web Site/MyApp", computerName=Server2

This command has a couple of important segments:
■■

verb  Specifies the required operation (delete, dump, getDependencies,
getSystemInfo, or sync).

■■

source  Specifies the source of the data for the verb argument.

■■

dest  Specifies the destination of a synchronization operation.

■■

whatif  Specifies that the command should run without making any actual changes.

As you can see, the whole syntax for using MSDeploy can become quite difficult.
When you create a Web Deployment package through Visual Studio, it also generates a
.cmd file that you can use to install the package from the command line. You can edit the
SetParameters.xml file to change parameter values for your deployment.
The following command deploys your package to a remote server running the Web
Management Service:
ProjectName.deploy.cmd /y /m:https://DestinationServerName:8172/MSDeploy.axd



Objective 5.5: Create, configure, and publish a web package

CHAPTER 5

413

MORE INFO  MSDEPLOY COMMAND-LINE REFERENCE

For all options that you can use when using MSDeploy, see http://technet.microsoft.com/
en-us/library/dd569106.aspx.

For the exam, it’s not reasonable that you memorize all options of Web Deploy. However,
knowing the basic syntax, for example, the whatif flag, is important. Try to experiment with
MSDeploy, and use the commands to prepare for the exam.
Next to using MSDeploy from a regular command prompt, you can also use the Web Deployment Framework from Windows PowerShell. Windows PowerShell is a very powerful tool.
It looks like a command line, but with extra capabilities. It’s built in .NET and it gives you full
access to the Component Object Model (COM) and Windows Management Instrumentation
(WMI).
If you want to use Web Deploy from within Windows PowerShell, you need to install the
WDeploySnapin3.0 snap-in. Just as with the regular MSDeploy command-line tool, you can
use Windows PowerShell to synchronize servers and create packages.
If you open a new Windows PowerShell window, you can execute the following commands
to get a list of available Windows PowerShell commands for Web Deploy:
Add-PSSnapin WDeploySnapin3.0
Get-Command -module WDeploySnapin3.0

Figure 5-18 shows the result of executing these commands.

FIGURE 5-18  The commands for the WDeploySnapin3.0 Windows PowerShell snap-in

414

CHAPTER 5

Deploying web applications and services

When you want to execute a remote deployment with Windows PowerShell, you first create a publish settings file. You can do this by using the New-WDPublishSettings command in
Windows PowerShell:
$cred = Get-Credential
New-WDPublishSettings -ComputerName WebServer -Site WebSite1 -Credentials $cred
-AllowUntrusted -SiteUrl "http://www.mywebsite.com" -FileName C:\temp\mywebsite.
publishsettings -AgentType wmsvc

The first line shows you a dialog box in which you can enter the credentials you want to
use for the deployment. However, you need to be careful because the credentials are stored
as plain text in the resulting file. The second line creates a new publish profile. It is stored in
the file C:\temp\mywebsite.publishsettings. After running this command, you have an XML file
with the following content:


publishMethod="MSDeploy"
publishUrl="WebServer"
msdeploySite="WebSite1"
userName="Administrator"
userPWD="p@ssw0rd"
destinationAppUrl="http://www.mywebsite.com"
SQLServerDBConnectionString=""
mySQLDBConnectionString=""
msdeployAllowUntrustedCertificate="True"
agentType="WMSvc"
useNTLM="False" />


After creating the publish settings file, you can use the Sync-WDApp command to deploy
your application:
  Sync-WDApp "MyApp" " MyAppDeployedWithPowerShell" -DestinationPublishSettings "C:\
temp\mywebsite.publishsettings"

MORE INFO  USING WINDOWS POWERSHELL TO DEPLOY TO WINDOWS AZURE

For information on how to use Windows PowerShell to deploy to Windows Azure, see
http://msdn.microsoft.com/en-us/library/windowsazure/jj554332.aspx.

Configuring the build process to output a web package
To automate the deployment of your web application, you need to integrate with your build
process. Normally, you do this by using TFS to run your builds. By modifying the build template, you can create a package and deploy it to a testing or production environment.
You use MSBuild to create a package like this:
MSBuild "MyProjectName.csproj" /T:Package



Objective 5.5: Create, configure, and publish a web package

CHAPTER 5

415

The /T option specifies the MSBuild Target that you want to use. If you remove the Package option, MSBuild just compiles your project.
By default, MSBuild uses the Debug configuration for your project. This is probably not
what you want. To specify a different configuration, such as Release, use the /P parameter:
MSBuild "MyProjectName.csproj" /T:Package /P:Configuration=Release

Now to integrate this with TFS, you need to edit your Build Definition. You can find the
Build Definitions in the Build tab of the Team Explorer in Visual Studio. A Build Definition
contains all the settings that TFS uses to run your build.
Inside your Build Definition, you can specify additional parameters that should be passed
to MSBuild. By specifying the /T:Package and /P:Configuration=Release parameters, you will
generate a package each time a build runs on TFS.
Instead of only creating the package, you can also start an automatic deployment by
specifying /p:DeployOnBuild=True. By using a correctly configured publish setting file, you
can now deploy your application automatically on each build.
MORE INFO  CONFIGURING TFS FOR CONTINUOUS DEPLOYMENT

For a walkthrough on how to configure TFS for continuous deployment, see http://
vishaljoshi.blogspot.nl/2010/11/team-build-web-deployment-web-deploy-vs.html.

Applying pre- and post-condition actions
Web Deploy also enables you to execute custom actions before or after running the
deployment of your application.
You can execute a command to run on the destination computer by using the
runCommand option:
msdeploy -verb:sync -source:runcommand=d:\test.bat -dest:auto,computername=Server1

By using the preSync and postSync options, you can run a command before or after your
deployment:
msdeploy -verb:sync -source:contentPath="C:\Test1" -dest:contentPath="C:\Test2"
-preSync:runcommand="c:\MyBatchFile.bat"

416

CHAPTER 5

Deploying web applications and services

Thought experiment 
Moving to continuous deployment
In this thought experiment, apply what you’ve learned about this objective. You can
find answers to these questions in the “Answers” section at the end of this chapter.
You are planning an internal presentation for your colleagues to show them the
benefits of automating your deployment. Currently, you are doing manual deployments in which you just copy the required files through FTP to the target servers.
Database updates are always done manually.
With this in mind, answer the following questions:

1. What are the advantages that continuous deployment can give you? Make a list.
2. Which techniques do you need for this?

Objective summary
■■
■■

■■

■■

■■

Web Deploy is the recommended way to deploy web applications.
You can create and deploy a Web Deploy package through Visual Studio, the IIS, command line, and Windows PowerShell.
You can configure different providers for deploying extra content such as database,
registry settings, or assemblies.
You can configure your build process to automatically create and deploy your
application.
By using the runCommand, preSync, and postSync options, you can execute additional
tasks when executing a deployment.

Objective review
Answer the following questions to test your knowledge of the information in this objective.
You can find the answers to these questions and explanations of why each answer choice is
correct or incorrect in the “Answers” section at the end of this chapter.
1. You want to deploy your website to Windows Azure Websites. What can you do?

(Choose all that apply.)
A. Create a new publish profile and choose the Web Deploy option. Enter the creden-

tials of your server and publish your website.
B. Download the publish profile from the Management Portal and import it in Visual

Studio.
C. Use FTP publishing to copy your files directly to Windows Azure.
D. Create a Web Deploy package and upload it to the Management Portal.


Objective 5.5: Create, configure, and publish a web package

CHAPTER 5

417