Lesson 2: Deploying the Order Service Application with Windows PowerShell

Time to complete: 25 minutes

Objective: To learn how to deploy applications that include WCF and/or WF services to IIS.

Purpose: This lesson shows you an example approach to using Windows PowerShell to script deployment with the Web Deployment Tool. You will redeploy the Order Service application to a Web site named OrderService_PS.

Prerequisites

Note the following prerequisites:

Procedure

You will go through the following steps as part of the redeployment process for the Order Service application:

  1. Prepare the configuration files for deployment

  2. Build the deployment packages by using MSBuild with Windows PowerShell.

  3. Create the OrderService_PS Web site and application pool.

  4. Deploy each Web service with the Web Deployment Tool and Windows PowerShell.

  5. Test each Web service with Windows PowerShell.

Preparing the Configuration Files for Deployment

You will use Windows PowerShell to redeploy the Order Service application to a new Web site using port 95. In this section you will update the endpoint addresses in the configuration files with the new port information before deployment.

  1. Run the following command in Windows PowerShell to open the app.config file for the Order Client.

    notepad C:\DublinTutorial\OrderServiceSolution\Completed\OrderClient\app.config
    

    Replace the client section with the following client section.

            <client>
                <endpoint address="https://localhost:95/OrderWorkflowService/OrderWorkflow.xamlx"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Microsoft.Samples.Dublin.Tutorials.OrderService.OrderWorkflowService.IOrderWorkflowService"
                    contract="OrderWorkflowService.MicrosoftSamplesDublinTutorialsOrderServiceOrderWorkflowServiceIOrderWorkflowService"
                    name="BasicHttpBinding_Microsoft.Samples.Dublin.Tutorials.OrderService.OrderWorkflowService.IOrderWorkflowService" />
            </client>
    

    Close Notepad and click Save when prompted to save changes.

  2. Run the following command from Windows PowerShell to open the Web.config file for the Order Processing Service.

    notepad C:\DublinTutorial\OrderServiceSolution\Completed\OrderProcessingService\Web.config
    

    Replace the client section with the following client section.

        <client>
          <endpoint address="https://localhost:95/OrderWorkflowService/OrderWorkflow.xamlx"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Microsoft.Samples.Dublin.Tutorials.OrderService.OrderWorkflowService.IOrderWorkflowService"
            contract="OrderWorkflowService.MicrosoftSamplesDublinTutorialsOrderServiceOrderWorkflowServiceIOrderWorkflowService"
            name="BasicHttpBinding_Microsoft.Samples.Dublin.Tutorials.OrderService.OrderWorkflowService.IOrderWorkflowService" />
        </client>
    

    Close Notepad and click Save when prompted to save changes.

  3. Run the following command from Windows PowerShell to open the Web.config file for the OrderWorkflowService.

    notepad C:\DublinTutorial\OrderServiceSolution\Completed\OrderWorkflowService\Web.config
    

    Replace the client section with the following client section.

        <client>
          <endpoint address="https://localhost:95/OrderProcessingService/OrderProcessing.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IOrderProcessing"
            contract="IOrderProcessing" name="BasicHttpBinding_IOrderProcessing" />
          <endpoint address="https://localhost:95/ShippingService/Shipping.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IShipping" contract="IShipping"
            name="BasicHttpBinding_IShipping" />
        </client>
    

    Close Notepad and click Save when prompted to save changes.

  4. Run the following command from Windows PowerShell to open the Web.config file for the Shipping Service.

    notepad C:\DublinTutorial\OrderServiceSolution\Completed\ShippingService\Web.config
    

    Replace the client section with the following client section.

        <client>
          <endpoint address="https://localhost:95/OrderWorkflowService/OrderWorkflow.xamlx"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Microsoft.Samples.Dublin.Tutorials.OrderService.OrderWorkflowService.IOrderWorkflowService"
            contract="OrderWorkflowService.MicrosoftSamplesDublinTutorialsOrderServiceOrderWorkflowServiceIOrderWorkflowService"
            name="BasicHttpBinding_Microsoft.Samples.Dublin.Tutorials.OrderService.OrderWorkflowService.IOrderWorkflowService" />
        </client>
    

    Close Notepad and click Save when prompted to save changes.

Building Deployment Packages by Using MSBuild with Windows PowerShell

The Microsoft Build Engine (MSBuild) is the build platform for Microsoft and Visual Studio. MSBuild is completely transparent with regard to how it processes and builds software, enabling developers to orchestrate and build products in build lab environments where Visual Studio is not installed. MSBuild is included as part of the .NET Framework. You will follow the steps in this section to use MSBuild to build new deployment packages for the Order Service application. These commands could be added to a Windows PowerShell script to automate the build. For more information about MSBuild see MSBuild Overview (https://go.microsoft.com/fwlink/?LinkId=123876).

  1. In Windows PowerShell, execute the following command to assign a variable named MSBuildPath to the full path of the MSBuild executable in the .NET Framework directory. This command determines the path based on the value of the InstallPath string value under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full registry key.

    $MSBuildPath = "$($(gp -Path HKLM:\Software\Microsoft\'NET Framework Setup'\NDP\v4\Full).InstallPath)MSBuild.exe"
    
  2. Execute the following command in Windows PowerShell to build the deployment package for the OrderProcessingService project:

    .$MSBuildPath 'C:\DublinTutorial\OrderServiceSolution\Completed\OrderProcessingService\OrderProcessingService.csproj' /T:Package /P:PackageLocation='C:\DublinTutorial\PS_DeploymentPackages\OrderProcessingService.zip'
    
  3. Execute the following command in Windows PowerShell to build the deployment package for the OrderWorkflowService project:

    .$MSBuildPath 'C:\DublinTutorial\OrderServiceSolution\Completed\OrderWorkflowService\OrderWorkflowService.csproj' /T:Package /P:PackageLocation='C:\DublinTutorial\PS_DeploymentPackages\OrderWorkflowService.zip'
    
  4. Execute the following command in Windows PowerShell to build the deployment package for the ShippingService project:

    .$MSBuildPath 'C:\DublinTutorial\OrderServiceSolution\Completed\ShippingService\ShippingService.csproj' /T:Package /P:PackageLocation='C:\DublinTutorial\PS_DeploymentPackages\ShippingService.zip'
    
  5. Verify that all three zip files were created in the C:\DublinTutorial\PS_DeploymentPackages directory as shown below:

    PS C:\> dir C:\DublinTutorial\PS_DeploymentPackages\*.zip
    
    
        Directory: C:\DublinTutorial\PS_DeploymentPackages
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---        11/11/2009   5:40 PM      13836 OrderProcessingService.zip
    -a---        11/11/2009   5:41 PM      22717 OrderWorkflowService.zip
    -a---        11/11/2009   5:41 PM      25177 ShippingService.zip
    
  6. If you have not built the OrderClient application, which is used as a client to the services, execute the following command in Windows PowerShell to build the OrderClient application:

    .$MSBuildPath 'C:\DublinTutorial\OrderServiceSolution\Completed\OrderClient\OrderClient.csproj'
    

Create the OrderService_PS Web Site and Application Pool

In this section you will use the Appcmd.exe administration tool for IIS to create the Web site and application pool. You will use the environment provider for Windows PowerShell to reference the path to Appcmd.exe using the SystemRoot environment variable. To see how this works, enter the following command in Windows PowerShell:

$env:SYSTEMROOT

The resulting output should show the system root directory.

Follow the steps below to create the OrderService_PS Web site and the OrderServiceAppPool_PS application pool. These commands can also be added into a Windows PowerShell deployment script for automation.

  1. In Windows PowerShell enter the following command to create the OrderServiceAppPool_PS application pool configured to use version 4 of the .NET Framework.

    .$env:SystemRoot\System32\inetsrv\appcmd.exe add apppool /Name:OrderServiceAppPool_PS -managedRuntimeVersion:v4.0
    

    Verify that the command results in the following output with no errors.

    APPPOOL object "OrderServiceAppPool_PS" added
    
  2. Enter the following command in Windows PowerShell to create a new Web site named OrderService_PS, which will be configured to use port 95 and store its content in the C:\DublinTutorial\OrderService_PS directory.

    .$env:SystemRoot\System32\inetsrv\appcmd.exe add site /name:OrderService_PS /bindings:http/*:95: /physicalPath:C:\DublinTutorial\OrderService_PS
    

    Verify that the command results in the following output with no errors.

    SITE object "OrderService_PS" added
    APP object "OrderService_PS/" added
    VDIR object "OrderService_PS/" added
    
  3. Enter the following command in Windows PowerShell to configure the OrderService_PS Web site to use OrderServiceAppPool_PS as the default application pool.

    .$env:SystemRoot\System32\inetsrv\appcmd.exe set site OrderService_PS /applicationDefaults.applicationPool:OrderServiceAppPool_PS
    

    Verify that the command results in the following output with no errors.

    SITE object "OrderService_PS" changed
    

Deploying Web Services with the Web Deployment Tool and Windows PowerShell

In this section you will use the Web Deployment Tool to deploy the Web services to a new Web site named OrderService_PS. This tutorial uses the “_PS” suffix to indicate that the Web site was created with Windows PowerShell. The commands shown in this section could be added to a Windows PowerShell deployment script for deploying the Order Service application. For more information about the Web Deployment Tool, see Web Deployment Tool (https://go.microsoft.com/fwlink/?LinkId=154601).

  1. Execute the following command in Windows PowerShell to assign a variable named MSDeployPath to the full path of the Web Deployment Tool.

    $MSDeployPath = "$env:ProgramFiles\IIS\Microsoft Web Deploy\msdeploy.exe"
    
  2. Execute the following command in Windows PowerShell to deploy the OrderProcessingService package to the OrderService_PS Web site:

    .$MSDeployPath -verb:sync -source:package=C:\DublinTutorial\PS_DeploymentPackages\OrderProcessingService.zip -dest:auto -setParam:Name=`"IIS Web Application Name`"`,value=OrderService_PS/OrderProcessingService
    

    Verify that the resulting output is similar to the following with no errors.

    Info: Updating createApp (OrderService_PS/OrderProcessingService).
    Info: Adding contentPath (OrderService_PS/OrderProcessingService).
    Info: Adding dirPath (OrderService_PS/OrderProcessingService).
    Info: Adding child dirPath (OrderService_PS/OrderProcessingService\bin).
    Info: Adding child filePath (OrderService_PS/OrderProcessingService\bin\OrderProcessingService.dll).
    Info: Adding child filePath (OrderService_PS/OrderProcessingService\OrderProcessing.svc).
    Info: Adding child filePath (OrderService_PS/OrderProcessingService\Web.config).
    
    Total changes: 7 (6 added, 0 deleted, 1 updated, 0 parameters changed, 22099 bytes copied)
    
  3. Execute the following command in Windows PowerShell to deploy the OrderWorkflowService package to the OrderService_PS Web site:

    .$MSDeployPath -verb:sync -source:package=C:\DublinTutorial\PS_DeploymentPackages\OrderWorkflowService.zip -dest:auto -setParam:Name=`"IIS Web Application Name`"`,value=OrderService_PS/OrderWorkflowService
    

    Verify that the resulting output is similar to the following with no errors.

    Info: Updating createApp (OrderService_PS/OrderWorkflowService).
    Info: Adding contentPath (OrderService_PS/OrderWorkflowService).
    Info: Adding dirPath (OrderService_PS/OrderWorkflowService).
    Info: Adding child dirPath (OrderService_PS/OrderWorkflowService\bin).
    Info: Adding child filePath (OrderService_PS/OrderWorkflowService\bin\OrderWorkflowService.dll).
    Info: Adding child filePath (OrderService_PS/OrderWorkflowService\OrderWorkflow.xamlx).
    Info: Adding child filePath (OrderService_PS/OrderWorkflowService\Web.config).
    
    Total changes: 7 (6 added, 0 deleted, 1 updated, 0 parameters changed, 84412 bytes copied)
    
  4. Execute the following command in Windows PowerShell to deploy the ShippingService package to the OrderService_PS Web site:

    .$MSDeployPath -verb:sync -source:package=C:\DublinTutorial\PS_DeploymentPackages\ShippingService.zip -dest:auto -setParam:Name=`"IIS Web Application Name`"`,value=OrderService_PS/ShippingService
    

    Verify that the resulting output is similar to the following with no errors.

    Info: Updating createApp (OrderService_PS/ShippingService).
    Info: Adding contentPath (OrderService_PS/ShippingService).
    Info: Adding dirPath (OrderService_PS/ShippingService).
    Info: Adding child dirPath (OrderService_PS/ShippingService\bin).
    Info: Adding child filePath (OrderService_PS/ShippingService\bin\ShippingService.dll).
    Info: Adding child filePath (OrderService_PS/ShippingService\bin\ShippingService.pdb).
    Info: Adding child filePath (OrderService_PS/ShippingService\Shipping.svc).
    Info: Adding child filePath (OrderService_PS/ShippingService\Web.config).
    
    Total changes: 8 (7 added, 0 deleted, 1 updated, 0 parameters changed, 68029 bytes copied)
    

Testing Web Services with Windows PowerShell

In this section you will use a simple script function to see if you are able to browse the Web services.

  1. In Windows PowerShell enter the following command to use Notepad to create a new utility script named Utility.ps1.

    NotePad Utility.ps1
    

    When Notepad starts and asks you if you want to create the file, click Yes.

  2. Copy and paste the following script function into Notepad.

    #=======================================================#
    #===                                                 ===#
    #=== Performs a simple browse test to a web service. ===#
    #===                                                 ===#
    #=======================================================#
    
    function BrowseWebService($address)
    {
      trap {return $false;}
    
      $WebClient = New-Object System.Net.WebClient
    
      $content = $WebClient.DownloadString($address)
    
      $content.Contains("You have created a service.")
    }
    

    This function uses the System.Net.WebClient class to parse the text response from requesting the specified service address. If the response contains the string “You have created a service.” then the browse test returns $true. Otherwise it returns $false.

  3. Close Notepad and click Save when prompted to save Utility.ps1.

  4. Import the utility script as a module for the current Windows PowerShell session by executing the following command in Windows PowerShell.

    Import-Module .\Utility.ps1
    
  5. Test all three of the Order Service Web services by entering the following three commands in Windows PowerShell.

    BrowseWebService "https://localhost:95/OrderProcessingService/OrderProcessing.svc"
    
    BrowseWebService "https://localhost:95/OrderWorkflowService/OrderWorkflow.xamlx"
    
    BrowseWebService "https://localhost:95/ShippingService/Shipping.svc"
    

    Verify that each BrowseWebService command reported True as shown below.

    PS C:\> BrowseWebService "https://localhost:95/OrderWorkflowService/OrderWorkflow.xamlx"
    True
    PS C:\> BrowseWebService "https://localhost:95/OrderProcessingService/OrderProcessing.svc"
    True
    PS C:\> BrowseWebService "https://localhost:95/ShippingService/Shipping.svc"
    True
    

What Did I Just Do?

In this lesson you updated the configuration files for each project to use the new endpoint addresses. Then you created a new Web site and application pool in Windows PowerShell. Finally, you used Windows PowerShell to build and deploy each service project to the new Web site.

Next Steps

In Lesson 3: Configuring the Order Service with Windows PowerShell, you will use the AppFabric cmdlets for Windows PowerShell to configure the monitoring and persistence databases for the Order Service application that you deployed in this lesson.

See Also

Concepts

Lesson 1: Getting Started with Windows Server AppFabric Cmdlets for Windows PowerShell
Lesson 3: Configuring the Order Service with Windows PowerShell
Lesson 4: Monitoring the Order Service with Windows PowerShell
Lesson 5: Tracking the Workflow with Windows PowerShell