共用方式為


How to: 部署專用的控制代碼

部署是專案的選擇性作業。 Web 專案時,例如,支援,讓專案更新 Web 伺服器的部署。 同樣地, 智慧型裝置專案支援複製到目標裝置已建置的應用程式的部署。 專案子類型可以提供特殊的部署行為,藉由實作IVsDeployableProjectCfg介面。 這個介面會定義一組完整的部署作業:

實際的部署作業應進行個別的執行緒中執行Visual Studio甚至更快回應使用者互動。 提供的方法IVsDeployableProjectCfg會非同步地呼叫Visual Studio ,並在背景中,如有必要,可讓查詢在任何時候部署作業的狀態,或是立即停止操作,在環境中運作。 IVsDeployableProjectCfg介面部署作業呼叫環境,當使用者選取 [部署] 命令。

若要通知的部署作業已經開始或結束的環境,專案子類型需要以呼叫OnStartDeployOnEndDeploy方法。

處理特定的部署

子專案處理特定的部署

  • 實作AdviseDeployStatusCallback註冊以接收通知的部署狀態活動環境的方法。

    Private adviseSink As Microsoft.VisualStudio.Shell.EventSinkCollection = New Microsoft.VisualStudio.Shell.EventSinkCollection()
    Public Function AdviseDeployStatusCallback(ByVal pIVsDeployStatusCallback As IVsDeployStatusCallback, _
                                               ByRef pdwCookie As UInteger) As Integer
    
        If pIVsDeployStatusCallback Is Nothing Then
            Throw New ArgumentNullException("pIVsDeployStatusCallback")
        End If
    
        pdwCookie = adviseSink.Add(pIVsDeployStatusCallback)
        Return VSConstants.S_OK
    
    End Function
    
    private Microsoft.VisualStudio.Shell.EventSinkCollection adviseSink = new Microsoft.VisualStudio.Shell.EventSinkCollection();
    public int AdviseDeployStatusCallback(IVsDeployStatusCallback pIVsDeployStatusCallback, 
                                          out uint pdwCookie)
    {
        if (pIVsDeployStatusCallback == null)
            throw new ArgumentNullException("pIVsDeployStatusCallback");
    
        pdwCookie = adviseSink.Add(pIVsDeployStatusCallback);
        return VSConstants.S_OK;
    }
    
  • 實作UnadviseDeployStatusCallback方法,以取消環境的註冊以接收通知的部署狀態事件。

    Public Function UnadviseDeployStatusCallback(ByVal dwCookie As UInteger) As Integer
        adviseSink.RemoveAt(dwCookie)
        Return VSConstants.S_OK
    End Function
    
    public int UnadviseDeployStatusCallback(uint dwCookie)
    {
        adviseSink.RemoveAt(dwCookie);
        return VSConstants.S_OK;
    }
    
  • 實作Commit方法,以執行應用程式相關的認可作業。 這個方法主要用於資料庫部署使用。

    Public Function Commit(ByVal dwReserved As UInteger) As Integer
        'Implement commit operation here.
        Return VSConstants.S_OK
    End Function
    
    public int Commit(uint dwReserved)
    {
         //Implement commit operation here.
         return VSConstants.S_OK;
    }
    
  • 實作Rollback方法,以執行復原作業。 呼叫這個方法時,必須執行項目,即適用於復原變更部署專案,並將其還原專案的狀態。 這個方法主要用於資料庫部署使用。

    Public Function Commit(ByVal dwReserved As UInteger) As Integer
        'Implement commit operation here.
        Return VSConstants.S_OK
    End Function
    
    public int Rollback(uint dwReserved)
    {
        //Implement Rollback operation here.
        return VSConstants.S_OK;
    }
    
  • 實作QueryStartDeploy方法,以判斷專案是否能夠啟動部署作業。

    Public Function QueryStartDeploy(ByVal dwOptions As UInteger, ByVal pfSupported As Integer(), ByVal pfReady As Integer()) As Integer
        If Not pfSupported Is Nothing AndAlso pfSupported.Length > 0 Then
            pfSupported(0) = 1
        End If
        If Not pfReady Is Nothing AndAlso pfReady.Length > 0 Then
            pfReady(0) = 0
            If Not deploymentThread Is Nothing AndAlso (Not deploymentThread.IsAlive) Then
                pfReady(0) = 1
            End If
        End If
        Return VSConstants.S_OK
    End Function
    
    public int QueryStartDeploy(uint dwOptions, int[] pfSupported, int[] pfReady)
    {
        if (pfSupported != null && pfSupported.Length >0)
            pfSupported[0] = 1;
        if (pfReady != null && pfReady.Length >0)
        {
            pfReady[0] = 0;
            if (deploymentThread != null && !deploymentThread.IsAlive)
                pfReady[0] = 1;
        }
        return VSConstants.S_OK;
    }
    
  • 實作QueryStatusDeploy方法,以判斷是否已順利完成部署作業。

    Public Function QueryStatusDeploy(ByRef pfDeployDone As Integer) As Integer
        pfDeployDone = 1
        If Not deploymentThread Is Nothing AndAlso deploymentThread.IsAlive Then
            pfDeployDone = 0
        End If
        Return VSConstants.S_OK
    End Function
    
    public int QueryStatusDeploy(out int pfDeployDone)
    {
        pfDeployDone = 1;
        if (deploymentThread != null && deploymentThread.IsAlive)
            pfDeployDone = 0;
        return VSConstants.S_OK;
    }
    
  • 實作StartDeploy開始的另一個執行緒的部署作業的方法。 將程式碼適用於您的應用程式部署在Deploy方法。

    Public Function StartDeploy(ByVal pIVsOutputWindowPane As IVsOutputWindowPane, ByVal dwOptions As UInteger) As Integer
        If pIVsOutputWindowPane Is Nothing Then
            Throw New ArgumentNullException("pIVsOutputWindowPane")
        End If
    
        If Not deploymentThread Is Nothing AndAlso deploymentThread.IsAlive Then
            Throw New NotSupportedException("Cannot start deployment operation when it is already started; Call QueryStartDeploy first")
        End If
    
        outputWindow = pIVsOutputWindowPane
    
        ' Notify that deployment is about to begin and see if any user wants to cancel.
        If (Not NotifyStart()) Then
            Return VSConstants.E_ABORT
        End If
    
        operationCanceled = False
    
        ' Create and start our thread
        deploymentThread = New Thread(AddressOf Me.Deploy)
        deploymentThread.Name = "Deployment Thread"
        deploymentThread.Start()
    
        Return VSConstants.S_OK
    End Function
    
    public int StartDeploy(IVsOutputWindowPane pIVsOutputWindowPane, uint dwOptions)
    {
        if (pIVsOutputWindowPane == null)
            throw new ArgumentNullException("pIVsOutputWindowPane");
    
        if (deploymentThread != null && deploymentThread.IsAlive)
            throw new NotSupportedException("Cannot start deployment operation when it is already started; Call QueryStartDeploy first");
    
        outputWindow = pIVsOutputWindowPane;
    
        // Notify that deployment is about to begin and see if any user wants to cancel.
        if (!NotifyStart())
            return VSConstants.E_ABORT;
    
        operationCanceled = false;
    
        // Create and start our thread
        deploymentThread = new Thread(new ThreadStart(this.Deploy));
        deploymentThread.Name = "Deployment Thread";
        deploymentThread.Start();
    
        return VSConstants.S_OK;
    }
    
  • 實作StopDeploy以停止部署作業的方法。 當使用者按下時,會呼叫這個方法取消在部署過程中的按鈕。

    Public Function StopDeploy(ByVal fSync As Integer) As Integer
        If Not deploymentThread Is Nothing AndAlso deploymentThread.IsAlive Then
            Return VSConstants.S_OK
        End If
    
        outputWindow.OutputStringThreadSafe("Canceling deployment")
        operationCanceled = True
        If fSync <> 0 Then
            ' Synchronous request, wait for the thread to terminate.
            If (Not deploymentThread.Join(10000)) Then
                Debug.Fail("Deployment thread did not terminate before the timeout, Aborting thread")
                deploymentThread.Abort()
            End If
        End If
    
        Return VSConstants.S_OK
    End Function
    
    public int StopDeploy(int fSync)
    {
        if (deploymentThread != null && deploymentThread.IsAlive)
            return VSConstants.S_OK;
    
        outputWindow.OutputStringThreadSafe("Canceling deployment");
        operationCanceled = true;
        if (fSync != 0)
        {
            // Synchronous request, wait for the thread to terminate.
            if (!deploymentThread.Join(10000))
            {
                Debug.Fail("Deployment thread did not terminate before the timeout, Aborting thread");
                deploymentThread.Abort();
            }
        }
    
        return VSConstants.S_OK;
    }
    
注意事項注意事項

本主題提供的所有程式碼範例是 「 組件的較大的範例中, Visual Studio 的擴充性範例

請參閱

其他資源

專案子類型