簡體   English   中英

自動更新Visual Studio擴展

[英]Automatically update Visual Studio Extension

當新版本被推送到Visual Studio庫時,我正在嘗試使我的擴展自動更新。 有一些關於如何實現這一目標的指南,但它們已有幾年的歷史,可能不適用。

對於初學者,我正在嘗試查詢IVsExtensionRepository ,如下所示:

var _extensionRepository = (IVsExtensionRepository)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsExtensionRepository));

var query = _extensionRepository.CreateQuery<VSGalleryEntry>(false, true)
                .OrderByDescending(n => n.Ranking)
                .Skip(0)
                .Take(25) as IVsExtensionRepositoryQuery<VSGalleryEntry>;

query.ExecuteCompleted += Query_ExecuteCompleted;
query.ExecuteAsync();

Query_ExecuteCompleted我收到服務器的異常:“遠程服務器返回錯誤:(400)錯誤請求。”

提供堆棧跟蹤:

服務器堆棧跟蹤:位於System.ServiceModel.Channels.ServiceMhannel.SendAsyncResult.End(SendAsyncResult結果)的System.Runtime.AsyncResult.End [TAsyncResult](IAsyncResult結果),位於System.ServiceModel.Channels.ServiceChannel.EndCall(String action,Object)在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)的System.ServiceModel.Channels.ServiceChannelProxy.InvokeEndService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)中的[]出,IAsyncResult結果)

該服務托管在: https//visualstudiogallery.msdn.microsoft.com/services/dev12/extension.svc

有誰知道我如何創建一個Visual Studio擴展,從Visual Studio庫自動更新自己? 通過IVsExtensionRepository還是手動?

編輯:現在在Visual Studio 2015中自動下載擴展。

所以我完全放棄了查詢IVsExtensionRepository 我不確定為什么,但它構造的查詢必然存在一些內部問題。 我使用ErikEJ建議的項目查詢相同的服務,它工作正常。

但是,我不想從WSDL構建服務,因為SQLCeToolbox已經完成了。 相反,我使用了IVsExtensionRepository ,但避免使用CreateQuery()方法。

附件是我更新我的VSPackage的方法。 您需要將任何GUID或Package特定名稱替換為您的包的信息。

注意以下代碼中有一個Gotcha':

請注意, CodeConnectRepositoryEntry僅實現DownloadUrl 更新VSPackage時,這是必須擔心的,因為它允許我們下載新包。 可以在VSPackage的VSGallery頁面上找到此URL。

但是 :您必須按如下方式修剪URL:

http://visualstudiogallery.msdn.microsoft.com/c0c2ad47-957c-4e07-89fc-20996595b6dd/file/140793/4/CodeConnectAlpha.vsix

至:

http://visualstudiogallery.msdn.microsoft.com/c0c2ad47-957c-4e07-89fc-20996595b6dd/file/140793/

上面,/ 4 /代表第四次上傳。 通過完全刪除它,Visual Studio Gallery將下載最新版本。

internal class CodeConnectUpdater
{
    IVsExtensionManager _extensionManager;

    IVsExtensionRepository _extensionRepository;

    //We need only supply the download URL.
    //This can be retrieved from the "Download" button on your extension's page.
    private class CodeConnectRepositoryEntry : IRepositoryEntry
    {
        public string DownloadUpdateUrl
        {
            get; set;
        }

        public string DownloadUrl
        {
            get
            {
                //NOTE: YOU MUST TRIM THE DOWNLOAD URL
                //TO NOT CONTAIN A VERSION. THIS FORCES 
                //THE GALLERY TO DOWNLOAD THE LATEST VERSION
                return "http://visualstudiogallery.msdn.microsoft.com/c0c2ad47-957c-4e07-89fc-20996595b6dd/file/140793/";
            }
            set
            {
                throw new NotImplementedException("Don't overwrite this.");
            }
        }

        public string VsixReferences
        {
            get; set;
        }
    }

    //I have been calling this from the VSPackage's Initilize, passing in the component model
    public bool CheckForUpdates(IComponentModel componentModel)
    {
        _extensionRepository = (IVsExtensionRepository)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsExtensionRepository));
        _extensionManager = (IVsExtensionManager)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsExtensionManager));
        //Find the extension you're after.
        var extension = _extensionManager.GetInstalledExtensions().Where(n => n.Header.Name == "Code Connect Alpha").SingleOrDefault();

        return CheckAndInstallNewVersion(extension);
    }

    private bool CheckAndInstallNewVersion(IInstalledExtension myExtension)
    {
        var needsRestart = false;
        var entry = new CodeConnectRepositoryEntry();
        var newVersion = FetchIfUpdated(myExtension, entry);
        if (newVersion != null)
        {
            Install(myExtension, newVersion);
            needsRestart = true;
        }

        return needsRestart;
    }

    //Checks the version of the extension on the VS Gallery and downloads it if necessary.
    private IInstallableExtension FetchIfUpdated(IInstalledExtension extension, CodeConnectRepositoryEntry entry)
    {
        var version = extension.Header.Version;
        var strNewVersion = _extensionRepository.GetCurrentExtensionVersions("ExtensionManagerQuery", new List<string>() { "6767f237-b6e4-4d95-9982-c9e898f72502" }, 1033).Single();
        var newVersion = Version.Parse(strNewVersion);

        if (newVersion > version)
        {
            var newestVersion = _extensionRepository.Download(entry);
            return newestVersion;
        }

        return null;
    }

    private RestartReason Install(IInstalledExtension currentExtension, IInstallableExtension updatedExtension)
    {
        //Uninstall old extension
        _extensionManager.Disable(currentExtension);
        _extensionManager.Uninstall(currentExtension);

        //Install new version
        var restartReason = _extensionManager.Install(updatedExtension, false);

        //Enable the newly installed version of the extension
        var newlyInstalledVersion = _extensionManager.GetInstalledExtension(updatedExtension.Header.Identifier);
        if (newlyInstalledVersion != null)
        {
            _extensionManager.Enable(newlyInstalledVersion);
        }

        return restartReason;
    }
}

我有一些代碼來訪問該服務並在此處生成RSS源:sqlcetoolbox.codeplex.com/SourceControl/latest - 在NuGetDownloadfedd.zip文件中(與Nuget無關!) - 還包括版本號:

 foundItem.Project.Metadata.TryGetValue("VsixVersion", out version);

事實上,我已經托管了RSS提要服務,如果您想使用它,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM