自动更新Visual Studio扩展

时间:2014-09-23 21:54:20

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

当我们将新版本推送到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我收到服务器的异常:&#34;远程服务器返回错误:(400)错误请求。&#34;

提供堆栈跟踪:

服务器堆栈跟踪:     在System.Runtime.AsyncResult.End [TAsyncResult](IAsyncResult结果)     在System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult结果)     在System.ServiceModel.Channels.ServiceChannel.EndCall(String action,Object [] outs,IAsyncResult result)     在System.ServiceModel.Channels.ServiceChannelProxy.InvokeEndService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)     在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

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

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

2 个答案:

答案 0 :(得分:2)

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

所以我完全放弃了查询IVsExtensionRepository。我不确定为什么,但它构造的查询必然存在一些内部问题。我使用ErikEJ的建议项目查询了相同的服务,它工作正常。

但是,我不想像SQLCeToolbox所做的那样从WSDL构造服务。相反,我使用IVsExtensionRepository,但避免使用CreateQuery()方法。

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

注意以下代码中有一个问题:

请注意,CodeConnectRepositoryEntry仅实现DownloadUrl。更新VSPackage时,这是必须担心的,因为它允许我们下载新包。可以在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 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;
    }
}

答案 1 :(得分:1)

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

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

事实上,我已经托管了RSS Feed服务,如果您想使用它,请告诉我。

相关问题