ASP.NET Web Deployment using Visual Studio: Command Line Deployment
by Tom Dykstra
This tutorial series shows you how to deploy (publish) an ASP.NET web application to Azure App Service Web Apps or to a third-party hosting provider, by using Visual Studio 2012 or Visual Studio 2010. For information about the series, see the first tutorial in the series.
Overview
This tutorial shows you how to invoke the Visual Studio web publish pipeline from the command line. This is useful for scenarios where you want to automate the deployment process instead of doing it manually in Visual Studio, typically by using a source code version control system.
Make a change to deploy
Currently the About page displays the template code.
You’ll replace that with code that displays a summary of student enrollment.
Open the About.aspx page, delete all of the markup inside the MainContent
Content
element, and insert the following markup in its place:
[!code-aspxMain]
1: <h2>Student Body Statistics</h2>
2: <asp:ObjectDataSource ID="StudentStatisticsObjectDataSource" runat="server" TypeName="ContosoUniversity.BLL.SchoolBL"
3: SelectMethod="GetStudentStatistics" DataObjectTypeName="ContosoUniversity.DAL.EnrollmentDateGroup">
4: </asp:ObjectDataSource>
5: <asp:GridView ID="StudentStatisticsGridView" runat="server" AutoGenerateColumns="False"
6: DataSourceID="StudentStatisticsObjectDataSource">
7: <Columns>
8: <asp:BoundField DataField="EnrollmentDate" DataFormatString="{0:d}" HeaderText="Date of Enrollment"
9: ReadOnly="True" SortExpression="EnrollmentDate" />
10: <asp:BoundField DataField="StudentCount" HeaderText="Students" ReadOnly="True"
11: SortExpression="StudentCount" />
12: </Columns>
13: </asp:GridView>
Run the project and select the About page.
Deploy to Test by using the command line
You won’t be deploying another database change, so disable dbDacFx database deployment for the aspnet-ContosoUniversity database. Open the Publish Web wizard, and in each of the three publish profiles, clear the Update Database check box on the Settings tab.
In the Windows 8 Start page, search for Developer Command Prompt for VS2012.
Right-click the icon for Developer Command Prompt for VS2012 and click Run as administrator.
Enter the following command at the command prompt, replacing the path to the solution file with the path to your solution file:
[!code-consoleMain]
1: msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=Test
MSBuild builds the solution and deploys it to the test environment.
Open a browser and go to http://localhost/ContosoUniversity
, then click the About page to verify that the deployment was successful.
If you haven’t created any students in test, you’ll see an empty page under the Student Body Statistics heading. Go to the Students page, click Add Student, and add some students, and then return to the About page to see student statistics.
Key command line options
The command that you entered passed the solution file path and two properties to MSBuild:
[!code-consoleMain]
1: msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true
2: /p:PublishProfile=Test
Deploying the solution versus deploying individual projects
Specifying the solution file causes all projects in the solution to be built. If you have multiple web projects in the solution, the following MSBuild behavior applies:
- The properties that you specify on the command line are passed to every project. Therefore, each web project must have a publish profile with the name that you specify. If you specify
/p:PublishProfile=Test
, each web project must have a publish profile named Test. - You might successfully publish one project when another one doesn’t even build. For more information, see the stackoverflow thread MSBuild fails with two packages.
If you specify an individual project instead of a solution, you have to add a parameter that specifies the Visual Studio version. If you are using Visual Studio 2012 the command line would be similar to the following example:
[!code-consoleMain]
1: msbuild C:\ContosoUniversity\ContosoUniversity\ContosoUniversity.csproj /p:DeployOnBuild=true /p:PublishProfile=Test /p:VisualStudioVersion=11.0
The version number for Visual Studio 2010 is 10.0. For more information, see Visual Studio project compatability and VisualStudioVersion on Sayed Hashimi’s blog.
Specifying the publish profile
You can specify the publish profile by name or by the full path to the .pubxml file, as shown in the following example:
[!code-consoleMain]
1: msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=C:\ContosoUniversity\ContosoUniversity\Properties\PublishProfiles\Test.pubxml
Web publish methods supported for command-line publishing
Three publish methods are supported for command line publishing:
MSDeploy
- Publish by using Web Deploy.Package
- Publish by creating a Web Deploy Package. You have to install the package separately from the MSBuild command that creates it.FileSystem
- Publish by copying files to a specified folder.
Specifying the build configuration and platform
The build configuration and platform must be set in Visual Studio or on the command line. The publish profiles include properties that are named LastUsedBuildConfiguration
and LastUsedPlatform
, but you can’t set these properties in order to determine how the project is built. For more information, see MSBuild: how to set the configuration property on Sayed Hashimi’s blog.
Deploy to staging
To deploy to Azure, you must add the password to the command line. If you saved the password in the publish profile in Visual Studio, it was stored in encrypted form in the your .pubxml.user file. That file is not accessed by MSBuild when you do a command line deployment, so you have to pass in the password in a command line parameter.
Copy the password that you need from the .publishsettings file that you downloaded earlier for the staging web site. The password is the value of the
userPWD
attribute for the Web DeploypublishProfile
element.- In the Windows 8 Start page, search for Developer Command Prompt for VS2012, and click the icon to open the command prompt. (You don’t have to open it as administrator this time because you aren’t deploying to IIS on the local computer.)
Enter the following command at the command prompt, replacing the path to the solution file with the path to your solution file and the password with your password:
[!code-consoleMain]
1: msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=Staging /p:Password=hdNSWsbuqno7J5uqnwKafwlfNPt1DSco7J5uqnwKafwlfNPt1DSpKHuYgCco7J5 /p:AllowUntrustedCertificate=true
/p:AllowUntrustedCertificate=true
. As this tutorial is being written, theAllowUntrustedCertificate
property must be set when you publish to Azure from the command line. When the fix for this bug is released, you won’t need that parameter.Open a browser and go to the URL of your staging site, and then click the About page to verify that the deployment was successful.
As you saw earlier for the test environment, you might have to create some students to see statistics on the About page.
Deploy to production
The process for deploying to production is similar to the process for staging.
- Copy the password that you need from the .publishsettings file that you downloaded earlier for the production web site.
- Open Developer Command Prompt for VS2012.
Enter the following command at the command prompt, replacing the path to the solution file with the path to your solution file and the password with your password:
[!code-consoleMain]
1: msbuild C:\ContosoUniversity\ContosoUniversity.sln /p:DeployOnBuild=true /p:PublishProfile=Production /p:Password=hdNSWsbuqnwKafwlo7J5uqnwKafwlfNPt1DSqnwKafwlfNPt1DSpKHuYgCco7J5 /p:AllowUntrustedCertificate=true
Open a browser and go to the URL of your staging site, and then click the About page to verify that the deployment was successful.
Summary
You have now deployed an application update by using the command line.
In the next tutorial, you will see an example of how to extend the web publish pipeline. The example will show you how to deploy files that are not included in the project.
|