【问题标题】:Programatically updating ClickOnce deployment (VSTO) then results in ApplicationDeployment.IsNetworkDeployed = False以编程方式更新 ClickOnce 部署 (VSTO) 然后导致 ApplicationDeployment.IsNetworkDeployed = False
【发布时间】:2020-05-21 14:06:23
【问题描述】:

我有一个 Excel VSTO 加载项,它通过 ClickOnce 每 24 小时更新一次。这很好用。

我想提供一个按钮,用户可以在其中立即手动检查更新。我关注了instructions provided in the documentation。我的代码如下所示:(暂时忽略注释部分)

Sub TryUpdateApp()

    If (ApplicationDeployment.IsNetworkDeployed) Then

        Dim Deployment As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
        Dim Info As UpdateCheckInfo = Nothing

        'Try
        '    Dim AppIdentity As New ApplicationIdentity(Deployment.UpdatedApplicationFullName)
        '    Dim UnrestrictedPerms As New Security.PermissionSet(Security.Permissions.PermissionState.Unrestricted)
        '    Dim AppTrust As New Security.Policy.ApplicationTrust(AppIdentity) With {
        '          .DefaultGrantSet = New Security.Policy.PolicyStatement(UnrestrictedPerms),
        '          .IsApplicationTrustedToRun = True,
        '          .Persist = True
        '          }
        '    Security.Policy.ApplicationSecurityManager.UserApplicationTrusts.Add(AppTrust)
        'Catch ex As Exception
        '    'log error
        'End Try

        Try
            Info = Deployment.CheckForDetailedUpdate()
        Catch dde As DeploymentDownloadException
            MsgBox($"The new version of App cannot be downloaded at this time.{vbNewLine}Please check your network connection, or try again later. Error: {dde.Message}", vbExclamation Or vbOKOnly)
            Exit Sub
        Catch ioe As InvalidOperationException
            MsgBox($"This application cannot be updated. It is likely not a ClickOnce application. Error: {ioe.Message}", vbCritical Or vbOKOnly)
            Exit Sub
        End Try

        Try

            If (Info.UpdateAvailable) Then
                Try
                    Deployment.Update()
                    MsgBox("App has been upgraded. Please restart Excel to apply changes.", vbInformation Or vbOKOnly)
                Catch dde As DeploymentDownloadException
                    MsgBox($"Unable to install the latest version of App: download failed.{vbNewLine}Please check your network connection, or try again later.", vbCritical Or vbOKOnly)
                    Exit Sub
                Catch tnge As TrustNotGrantedException
                    MsgBox("Unable to install the latest version of App: trust not granted.", vbExclamation Or vbOKOnly)
                    Exit Sub
                End Try
            Else
                MsgBox("The latest version of App is already installed.", vbInformation Or vbOKOnly)
            End If

        Catch ex As Exception

            MsgBox("Unable to install the latest version of App: unknown error.")
            Exit Sub

        End Try

    Else

        Throw New ApplicationException("Application is not network deployed.")

    End If

End Sub

虽然它会在“已安装最新版本的应用程序”时准确指示。如果需要,它将无法更新,并抛出TrustNotGrantedException: User has refused to grant required permissions to the application.

第一个有趣的事情是,这个异常是由“无法安装最新版本的应用程序:未知错误。”捕获的,而不是“无法安装最新版本的应用程序:未授予信任。”,正如人们所期望的那样.

然后找到this thread,对应上面代码的注释部分。当我取消注释并运行子程序时,它似乎可以正常工作,因为我得到“应用程序已升级。请重新启动 Excel 以应用更改”。但是,当我重新启动 Excel 并再次运行 Sub 时,我得到“应用程序未部署网络”。

我该如何解决这个问题? (任何 C# 代码都可以)

【问题讨论】:

    标签: .net vb.net vsto clickonce office-addins


    【解决方案1】:

    首先,尝试将应用程序 URL 添加到受信任站点列表中。

    根据MSDNTrustNotGrantedException 被抛出,如果:

    应用程序使用权限提升,而用户拒绝提升信任的请求;或

    应用程序使用受信任的应用程序部署,并且用于签署应用程序的数字证书未列为本地计算机上的受信任发布者。如果您已将更新部署到应用程序,并且更新使用的权限比以前的版本更多,并且 ClickOnce 抛出 TrustNotGrantedException,新版本将不会安装。

    由于这是一个完全信任的应用程序,您的用户是否具有管理员权限,是否有任何操作需要管理员权限?

    您的证书是来自 CA 还是 VS 生成的测试证书?如果那是测试证书,您需要检查它是否已添加到 Trust Publishers 列表中。

    更多信息请参见How to: Add a Trusted Publisher to a Client Computer for ClickOnce Applications

    【讨论】:

    • 谢谢尤金。我有一个可以正常工作的 EV 证书。当通过ClickOnce update strategy 完成此操作时,VSTO 加载项确实可以毫无问题地安装和更新。什么不起作用是Programatic Update
    • VSTO 加载项是一种特殊的 ClickOnce 部署。据我了解,Office 会打包安装并控制更新。似乎TrustNotGrantedException 来自这样一个事实,即 Office 甚至没有真正要求用户授予或不授予信任:它只是拒绝它。我认为,如果受信任的 URL 或证书等安全设置在起作用,用户将无法安装插件(当他们确实看到授予信任的提示时);加载项也无法通过 ClickOnce 更新策略进行更新。
    • 您所指的页面提供了以下声明:Your application must have full-trust permissions to use programmatic updating.。您有机会在运行时检查权限吗?
    • documentation 表示 VSTO 仅在获得完全信任时才执行,所以我认为是这样。当我在包含列表中明确添加应用程序时,我将检查行为是否发生变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多