部署是项目的选项操作。 Web 项目,例如,支持部署允许项更新 Web 服务器。 同样, Smart Device 项目支持部署复制生成的应用程序到目标计算机。 项目子类型可以通过实现 IVsDeployableProjectCfg 接口提供专用部署行为。 此接口定义一组完整的部署操作:
在单独的线程应该执行实际部署操作使 Visual Studio 更迅速对用户交互。 IVsDeployableProjectCfg 提供的方法。 Visual Studio 在后台异步调用并运行,使环境随时查询部署操作的状态或停止操作,如果需要,。 ,当用户选择部署命令时, IVsDeployableProjectCfg 接口部署操作由环境调用。
若要通知该环境部署操作启动或关闭,项目子类型需要调用 OnStartDeploy 和 OnEndDeploy 方法。
处理特殊的部署
处理由子类型项目的专用的部署
执行 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 扩展性示例。