Condividi tramite


Procedura: distribuzione specializzata handle

una distribuzione è un'operazione facoltativa per i progetti. Un progetto Web, ad esempio, supporta una distribuzione per rifiutare esplicitamente l'aggiornamento di progetto un server Web. Inoltre, un progetto Smart Device supporta una distribuzione per copiare un'applicazione generata al dispositivo di destinazione. I sottotipi di progetto possono fornire il comportamento specifico di distribuzione implementando l'interfaccia di IVsDeployableProjectCfg . Questa interfaccia definisce un set completo delle operazioni di distribuzione:

Effettiva passaggi di distribuzione deve essere eseguita nel thread separato per rendere Visual Studio ancora più reattiva interazione utente. I metodi forniti da IVsDeployableProjectCfg vengono chiamati in modo asincrono da Visual Studio e eseguiti in background, in modo che l'ambiente eseguire una query sullo stato di un'operazione di distribuzione in qualsiasi momento o interrompere l'operazione, se necessario. Le operazioni di distribuzione dell'interfaccia di IVsDeployableProjectCfg vengono chiamate dall'ambiente quando l'utente seleziona il comando di distribuzione.

Per notificare all'ambiente che un'operazione di distribuzione ha avviato o interrotto, il sottotipo di progetto deve chiamare OnStartDeploy e i metodi di OnEndDeploy .

Gestire distribuzione avanzata

Per la gestione di una distribuzione avanzata da un sottotipo progetto

  • Implementare il metodo di AdviseDeployStatusCallback per registrare l'ambiente per ricevere le notifiche degli eventi dello stato di distribuzione.

    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;
    }
    
  • Implementare il metodo di UnadviseDeployStatusCallback per annullare la registrazione dell'ambiente per ricevere le notifiche degli eventi dello stato di distribuzione.

    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;
    }
    
  • Implementare il metodo di Commit per eseguire l'operazione di commit specifica all'applicazione. Questo metodo viene utilizzato principalmente per la distribuzione del database.

    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;
    }
    
  • Implementare il metodo di Rollback per eseguire un'operazione di rollback. Quando questo metodo viene chiamato, il progetto di distribuzione è necessario eseguire le operazioni appropriate alle modifiche di rollback e ripristinare lo stato del progetto. Questo metodo viene utilizzato principalmente per la distribuzione del database.

    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;
    }
    
  • Implementare il metodo di QueryStartDeploy per determinare se è presente un progetto può iniziare un'operazione di distribuzione.

    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;
    }
    
  • Implementare il metodo di QueryStatusDeploy per verificare la presenza di un'operazione di distribuzione è stata completata.

    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;
    }
    
  • Implementare il metodo di StartDeploy per avviare un'operazione di distribuzione in un thread separato. Inserire il codice specifico per la distribuzione dell'applicazione nel metodo di 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;
    }
    
  • implementare il metodo di StopDeploy per interrompere un'operazione di distribuzione. Questo metodo viene chiamato quando un utente preme il pulsante di Annulla durante il processo di distribuzione.

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

Nota

Tutti gli esempi di codice forniti in questo argomento fanno parte di un esempio più esaustivo Esempi di estensibilità di Visual Studio.

Vedere anche

Altre risorse

sottotipi di progetto