第 2 課:使用 Windows PowerShell 來部署訂單服務應用程式

完成時間: 25 分鐘

目標: 了解如何將包含 WCF 和/或 WF 服務的應用程式部署到 IIS。

用途: 此課程提供一個範例方式,說明如何使用 Windows PowerShell 撰寫指令碼以搭配 Web Deployment Tool 來進行部署。您將會重新部署 Order Service 應用程式至名為 OrderService_PS 的網站。

必要條件

請注意下列必要條件:

程序

重新部署 Order Service 應用程式時,您必須執行下列步驟:

  1. 準備用於部署的組態檔

  2. 使用 MSBuild 搭配 Windows PowerShell 以建置部署套件。

  3. 建立 OrderService_PS 網站與應用程式集區。

  4. 使用 Web Deployment Tool 與 Windows PowerShell 部署每個 Web 服務。

  5. 使用 Windows PowerShell 測試每個 Web 服務。

準備用於部署的組態檔

您會使用 Windows PowerShell 將 Order Service 應用程式重新部署到使用連接埠 95 的新網站。在本節中,您會將組態檔中的端點位址更新為新的連接埠資訊,然後再進行部署。

  1. 在 Windows PowerShell 中執行下列命令,以開啟「訂單用戶端」的 app.config 檔案。

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

    使用下列 client 區段取代 client 區段。

            <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>
    

    關閉 [記事本] 並在系統提示您儲存變更時按一下 [儲存]。

  2. 在 Windows PowerShell 中執行下列命令,以開啟 Order Processing Service 的 Web.config 檔案。

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

    使用下列 client 區段取代 client 區段。

        <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>
    

    關閉 [記事本] 並在系統提示您儲存變更時按一下 [儲存]。

  3. 在 Windows PowerShell 中執行下列命令,以開啟 OrderWorkflowService 的 Web.config 檔案。

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

    使用下列 client 區段取代 client 區段。

        <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>
    

    關閉 [記事本] 並在系統提示您儲存變更時按一下 [儲存]。

  4. 在 Windows PowerShell 中執行下列命令,以開啟「出貨服務」的 Web.config 檔案。

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

    使用下列 client 區段取代 client 區段。

        <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>
    

    關閉 [記事本] 並在系統提示您儲存變更時按一下 [儲存]。

使用 MSBuild 搭配 Windows PowerShell 以建置部署套件

Microsoft Build Engine (MSBuild) 是 Microsoft 與 Visual Studio 的建置平台。MSBuild 完全在背景處理與建置軟體,可讓開發人員在未安裝 Visual Studio 的建置實驗室環境中協調與建置產品。MSBuild 是 .NET Framework 的一部分。您將會依照本節中的步驟使用 MSBuild 來建置 Order Service 應用程式的新部署套件。您可以將這些命令新增至 Windows PowerShell 指令碼,以將建置程序自動化。如需有關 MSBuild 的詳細資訊,請參閱 MSBuild 概觀 (https://go.microsoft.com/fwlink/?LinkId=123876) (可能為英文網頁)。

  1. 在 Windows PowerShell 中執行下列命令,將 MSBuildPath 變數的值指定為 MSBuild 可執行檔 (位於 .NET Framework 目錄中) 的完整路徑。此命令會根據 InstallPath 字串 (位於 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full 登錄機碼下) 的值來判斷該路徑。

    $MSBuildPath = "$($(gp -Path HKLM:\Software\Microsoft\'NET Framework Setup'\NDP\v4\Full).InstallPath)MSBuild.exe"
    
  2. 在 Windows PowerShell 中執行下列命令,以建置 OrderProcessingService 專案的部署套件:

    .$MSBuildPath 'C:\DublinTutorial\OrderServiceSolution\Completed\OrderProcessingService\OrderProcessingService.csproj' /T:Package /P:PackageLocation='C:\DublinTutorial\PS_DeploymentPackages\OrderProcessingService.zip'
    
  3. 在 Windows PowerShell 中執行下列命令,以建置 OrderWorkflowService 專案的部署套件:

    .$MSBuildPath 'C:\DublinTutorial\OrderServiceSolution\Completed\OrderWorkflowService\OrderWorkflowService.csproj' /T:Package /P:PackageLocation='C:\DublinTutorial\PS_DeploymentPackages\OrderWorkflowService.zip'
    
  4. 在 Windows PowerShell 中執行下列命令,以建置 ShippingService 專案的部署套件:

    .$MSBuildPath 'C:\DublinTutorial\OrderServiceSolution\Completed\ShippingService\ShippingService.csproj' /T:Package /P:PackageLocation='C:\DublinTutorial\PS_DeploymentPackages\ShippingService.zip'
    
  5. 確定三個 zip 檔案都已建立在 C:\DublinTutorial\PS_DeploymentPackages 目錄中,如下所示:

    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. 若尚未建置 OrderClient 應用程式 (用做為服務的用戶端),請在 Windows PowerShell 中執行下列命令以建置 OrderClient 應用程式:

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

建立 OrderService_PS 網站與應用程式集區

在本節中,您將會使用 IIS 的 Appcmd.exe 系統管理工具來建立網站與應用程式集區。您將會使用 Windows PowerShell 的環境提供者來參照 Appcmd.exe 的路徑 (使用 SystemRoot 環境變數)。若要檢視此運作方式,請在 Windows PowerShell 中輸入下列命令:

$env:SYSTEMROOT

結果輸出應該會顯示系統根目錄。

依照下列步驟建立 OrderService_PS 網站與 OrderServiceAppPool_PS 應用程式集區。您也可以將這些命令新增至 Windows PowerShell 部署指令碼來進行自動化。

  1. 在 Windows PowerShell 中輸入下列命令,以建立 OrderServiceAppPool_PS 應用程式集區 (設定為使用 .NET Framework 4)。

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

    確認下列輸出中的命令結果沒有錯誤。

    APPPOOL object "OrderServiceAppPool_PS" added
    
  2. 在 Windows PowerShell 中輸入下列命令,以建立名為 OrderService_PS 的新網站,此網站被設定為使用連接埠 95 並將其內容儲存在 C:\DublinTutorial\OrderService_PS 目錄中。

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

    確認下列輸出中的命令結果沒有錯誤。

    SITE object "OrderService_PS" added
    APP object "OrderService_PS/" added
    VDIR object "OrderService_PS/" added
    
  3. 在 Windows PowerShell 中輸入下列命令,將 OrderService_PS 網站設定為使用 OrderServiceAppPool_PS 做為預設應用程式集區。

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

    確認下列輸出中的命令結果沒有錯誤。

    SITE object "OrderService_PS" changed
    

使用 Web Deployment Tool 與 Windows PowerShell 來部署 Web 服務

在本節中,您將使用 Web Deployment Tool 將 Web 服務部署至名為 OrderService_PS 的新網站。此教學課程使用 “_PS” 尾碼指出網站是使用 Windows PowerShell 所建立。您可以將本節中顯示的命令新增至 Windows PowerShell 部署指令碼,以部署 Order Service 應用程式。如需有關 Web Deployment Tool 的詳細資訊,請參閱 Web Deployment Tool (https://go.microsoft.com/fwlink/?LinkId=154601) (可能為英文網頁)。

  1. 在 Windows PowerShell 中執行下列命令,將 MSDeployPath 變數的值指定為 Web Deployment Tool 的完整路徑。

    $MSDeployPath = "$env:ProgramFiles\IIS\Microsoft Web Deploy\msdeploy.exe"
    
  2. 在 Windows PowerShell 中執行下列命令,將 OrderProcessingService 套件部署至 OrderService_PS 網站:

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

    確認結果輸出如下,而且沒有任何錯誤。

    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. 在 Windows PowerShell 中執行下列命令,將 OrderWorkflowService 套件部署至 OrderService_PS 網站:

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

    確認結果輸出如下,而且沒有任何錯誤。

    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. 在 Windows PowerShell 中執行下列命令,將 ShippingService 套件部署至 OrderService_PS 網站:

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

    確認結果輸出如下,而且沒有任何錯誤。

    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)
    

使用 Windows PowerShell 來測試 Web 服務

在本節中,您將會使用簡單的指令碼函式來判斷是否可瀏覽 Web 服務。

  1. 在 Windows PowerShell 中輸入下列命令,以使用 [記事本] 來建立名為 Utility.ps1 的新公用程式指令碼。

    NotePad Utility.ps1
    

    當 [記事本] 啟動並詢問您是否要建立檔案時,請按一下 [是]。

  2. 將下列指令碼函式複製並貼到 [記事本] 中。

    #=======================================================#
    #===                                                 ===#
    #=== 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.")
    }
    

    此函式使用 System.Net.WebClient 類別來剖析要求指定之服務位址傳回的文字回應。若回應包含 “You have created a service.”(您已建立服務。),則瀏覽器測試會傳回 $true。否則,它會傳回 $false

  3. 關閉 [記事本] 並在系統提示您儲存 Utility.ps1 時按一下 [儲存]。

  4. 在 Windows PowerShell 中執行下列命令,將該公用程式指令碼匯入為目前 Windows PowerShell 工作階段的模組。

    Import-Module .\Utility.ps1
    
  5. 在 Windows PowerShell 中輸入下列三個命令,測試三個 Order Service Web 服務。

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

    確認每個 BrowseWebService 命令都傳回 True,如下所示。

    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
    

我剛剛做了什麼?

在此課程中,您更新了每個專案的組態檔以使用新的端點位址。接著,您在 Windows PowerShell 建立新網站與應用程式集區。最後,您使用 Windows PowerShell 來建置每個服務專案並部署到新的網站。

後續步驟

第 3 課:使用 Windows PowerShell 來設定 Order Service 中,您將會使用 Windows PowerShell 的 AppFabric Cmdlet 來設定您在此課程中部署之 Order Service 應用程式的監控與持續性資料庫。

另請參閱

概念

第 1 課:開始使用 Windows PowerShell 的 Windows Server AppFabric Cmdlet
第 3 課:使用 Windows PowerShell 來設定 Order Service
第 4 課:使用 Windows PowerShell 來監控 Order Service
第 5 課:使用 Windows PowerShell 來追蹤工作流程

  2011-12-05