部署是一个项目的可选操作。 例如,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;
}
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
UnadviseDeployStatusCallback实现取消环境注册以接收部署状态事件的通知的方法。
public int UnadviseDeployStatusCallback(uint dwCookie)
{
adviseSink.RemoveAt(dwCookie);
return VSConstants.S_OK;
}
Public Function UnadviseDeployStatusCallback(ByVal dwCookie As UInteger) As Integer
adviseSink.RemoveAt(dwCookie)
Return VSConstants.S_OK
End Function
实现 Commit 方法以执行特定于您的应用程序的提交操作。 此方法主要用于数据库部署。
public int Commit(uint dwReserved)
{
//Implement commit operation here.
return VSConstants.S_OK;
}
Public Function Commit(ByVal dwReserved As UInteger) As Integer
'Implement commit operation here.
Return VSConstants.S_OK
End Function
实现Rollback方法以执行回滚操作。 调用此方法时,部署项目必须采取所有必要措施来回滚更改并还原项目的状态。 此方法主要用于数据库部署。
public int Rollback(uint dwReserved)
{
//Implement Rollback operation here.
return VSConstants.S_OK;
}
Public Function Commit(ByVal dwReserved As UInteger) As Integer
'Implement commit operation here.
Return VSConstants.S_OK
End Function
实现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;
}
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
实现QueryStatusDeploy方法以确定部署操作是否已成功完成。
public int QueryStatusDeploy(out int pfDeployDone)
{
pfDeployDone = 1;
if (deploymentThread != null && deploymentThread.IsAlive)
pfDeployDone = 0;
return VSConstants.S_OK;
}
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
应实现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;
}
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
实现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;
}
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
相关内容