Como: alça especializados de implantação
Uma implantação é uma operação opcional para projetos. Um projeto Web, por exemplo, oferece suporte a uma implantação para permitir que um projeto de atualização de um servidor Web. Da mesma forma, um Smart Device project oferece suporte a uma implantação para copiar um aplicativo criado para o dispositivo de destino. Os subtipos de projeto podem fornecer comportamento especializado de implantação Implementando o IVsDeployableProjectCfg interface. Essa interface define um conjunto completo de operações de implantação:
A operação de implantação real deve ser executada em um thread separado para fazer Visual Studio ainda mais responsivo para a interação do usuário. Os métodos fornecidos pelo IVsDeployableProjectCfg são chamados de forma assíncrona por Visual Studio e operar em segundo plano, permitindo que o ambiente para consultar o status de uma operação de implantação a qualquer momento ou para parar a operação, se necessário. O IVsDeployableProjectCfg operações de implantação de interface são chamadas pelo ambiente quando o usuário seleciona o comando deploy.
Para notificar o ambiente em que uma operação de implantação iniciada ou finalizada, o subtipo de projeto precisa chamar o OnStartDeploy e o OnEndDeploy métodos.
Tratamento especializado de implantação
Para lidar com uma implantação especializada por um projeto de subtipo
Implementar a AdviseDeployStatusCallback método para registrar o ambiente para receber notificações de eventos de status de implantação.
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; }
Implementar a UnadviseDeployStatusCallback método para cancelar o registro do ambiente para receber notificações de eventos de status de implantação.
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; }
Implementar a Commit método para executar a operação de confirmação específica do aplicativo. Este método é usado principalmente para a implantação de banco de dados.
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; }
Implementar a Rollback método para executar uma operação de reversão. Quando esse método é chamado, o projeto de implantação deve fazer o que for apropriado para desfazer alterações e restaurar o estado do projeto. Este método é usado principalmente para a implantação de banco de dados.
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; }
Implementar a QueryStartDeploy método para determinar se é ou não um projeto capaz de iniciar uma operação de implantação.
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; }
Implementar a QueryStatusDeploy método para determinar se ou não uma operação de implantação foi concluída com êxito.
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; }
Implementar a StartDeploy método para iniciar uma operação de implantação em um thread separado. Coloque o código específico para a implantação do aplicativo dentro do Deploy método.
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; }
Implementar a StopDeploy método para interromper uma operação de implantação. Este método é chamado quando um usuário pressiona o Cancelar botão durante o processo de implantação.
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; }
Dica
Todos os exemplos de código fornecidos neste tópico são partes de um exemplo maior, Exemplos de extensibilidade de Visual Studio.