处理专用部署

部署是项目的可选操作。 例如,Web 项目支持部署,让项目更新 Web 服务器。 同样, 智能设备 项目支持部署将生成的应用程序复制到目标设备。 项目子类型可以通过实现 IVsDeployableProjectCfg 接口来提供专用部署行为。 此接口定义一组完整的部署操作:

处理子类型项目的专用部署

  • AdviseDeployStatusCallback实现注册环境以接收部署状态事件的通知的方法。

    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 int UnadviseDeployStatusCallback(uint dwCookie)
    {
        adviseSink.RemoveAt(dwCookie);
        return VSConstants.S_OK;
    }
    
    
  • 实现特定于 Commit 应用程序的提交操作的方法。 此方法主要用于数据库部署。

    public int Commit(uint dwReserved)
    {
         //Implement commit operation here.
         return VSConstants.S_OK;
    }
    
    
  • Rollback实现执行回滚操作的方法。 调用此方法时,部署项目必须执行任何适当的操作来回滚更改并还原项目的状态。 此方法主要用于数据库部署。

    public int Rollback(uint dwReserved)
    {
        //Implement Rollback operation here.
        return VSConstants.S_OK;
    }
    
    
  • QueryStartDeploy实现该方法以确定项目是否能够启动部署操作。

    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 int QueryStatusDeploy(out int pfDeployDone)
    {
        pfDeployDone = 1;
        if (deploymentThread != null && deploymentThread.IsAlive)
            pfDeployDone = 0;
        return VSConstants.S_OK;
    }
    
    
  • StartDeploy实现该方法以在单独的线程中开始部署操作。 将特定于应用程序部署的代码放在方法中 Deploy

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

注意

本主题中提供的所有代码示例都是 VSSDK 示例中较大示例一部分。