【问题标题】:Automatically update Visual Studio Extension自动更新 Visual Studio 扩展
【发布时间】:2014-09-23 21:54:20
【问题描述】:

我正在尝试让我的扩展在新版本推送到 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.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult 结果) 在 System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult 结果) 在 System.ServiceModel.Channels.ServiceChannel.EndCall(字符串操作,对象 [] 输出,IAsyncResult 结果) 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeEndService(IMethodCallMessage 方法调用,ProxyOperationRuntime 操作) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

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

有谁知道我如何创建一个可以从 Visual Studio 库自动更新自身的 Visual Studio 扩展?是通过IVsExtensionRepository还是手动?

【问题讨论】:

  • 嗯,您可能需要设置数据包跟踪并查看正在发送的无效查询。
  • 是的,我想我会尝试一下。一切都在 SSL 上,但我认为这仍然是可能的,只是我需要一些时间来了解如何。

标签: c# visual-studio visual-studio-extensions vspackage visual-studio-package


【解决方案1】:

编辑:现在在 Visual Studio 2015 中会自动下载扩展。

所以我完全放弃了查询IVsExtensionRepository。我不知道为什么,但它构造的查询一定存在一些内部问题。我使用 ErikEJ 建议的项目查询了相同的服务,它运行良好。

但是,我不想从 WSDL 构建服务,因为 SQLCeToolbox 似乎已经完成了。相反,我使用了IVsExtensionRepository,但避免了CreateQuery() 方法。

附件是我更新 VSPackage 的方法。您需要用您的包信息替换任何 GUID 或包特定名称。

注意以下代码中有一个陷阱:

请注意,CodeConnectRepositoryEntry 仅实现 DownloadUrl。更新 VSPackage 时,这都是我们必须担心的,因为它允许我们下载新包。此 URL 可以在您的 VSPackage 的 VSGallery 页面上找到。

然而:您必须按如下方式修剪 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 库将下载最新版本。

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

【讨论】:

    【解决方案2】:

    我有一些代码可以访问该服务并从中生成 RSS 提要:sqlcetoolbox.codeplex.com/SourceControl/latest - 在 NuGetDownloadfedd.zip 文件中(与 Nuget 无关!) - 还包括版本号码:

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

    事实上,我已经在托管一个 RSS 提要服务,如果您想使用它,请告诉我。

    【讨论】:

    • 你知道FeedHandler.cs中LCID为1033的意义吗? (如果不是很好,我只是不知道它代表什么)
    • 你假装从英文版的 Visual Studio 调用 web 服务...
    猜你喜欢
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多