【问题标题】:Exception while debugging using windows MAF (AddIn pipeline)使用 Windows MAF(插件管道)调试时出现异常
【发布时间】:2019-04-07 00:36:33
【问题描述】:

我一直在尝试构建 AddIn 架构并使用 MAF。我所遵循的基础知识给出了here on Microsoft docs

申请背景简介:

  1. 我有一个 wpf 宿主应用程序,它将在运行时加载 addIn 程序集。插件实现了一个契约(接口),并用一个帮助主机发现它的属性进行了装饰。这些是 MAF 下的标准内容,因此不想在此处添加更多内容,但如果它有助于清晰,那么很乐意添加更多内容。应用程序主机/插件都在我的笔记本电脑上,我在管理员配置文件下运行。

加载插件时,主机中的主要代码是:

    string appPath = Environment.CurrentDirectory + "\\Pipeline";
    string[] warnings = AddInStore.Rebuild(appPath);
       if (warnings.Length > 0)
          {
             string msg = "Could not rebuild pipeline:";
             foreach (string warning in warnings) msg += "\n" + warning;
             MessageBox.Show(msg);
             return;
          }

   Collection<AddInToken> addInTokens = AddInStore.FindAddIns(typeof(IWPFAddInHostView), appPath);

   AddInToken wpfAddInToken = addInTokens[0];

   // the only line I can see that has something to do with
   // security permissions
   this._wpfAddInHostView = wpfAddInToken.Activate<IWPFAddInHostView>(AddInSecurityLevel.Internet);

   FrameworkElement addInUI = this._wpfAddInHostView.GetAddInUI();
                this.addInUIHostGrid.Children.Add(addInUI);

当我运行它时,此代码工作正常(加载 addIn 并单击 addIn UI 中的按钮会弹出预期的消息框),但在调试模式下它会给出以下异常并崩溃(它会正确加载 addIn 但在单击按钮发生异常),我在上面标记了与安全权限有关的唯一行,不知道为什么这应该只在调试模式下发生,甚至为什么会发生。

另外,我不知道一个问题中有 2 个问题,但我没有看到很多人使用 MAF 并且产品页面显示没有积极支持,想知道我是否做出了错误的选择。

System.Security.SecurityException HResult=0x8013150A
Message=请求类型的权限 'System.Security.Permissions.UIPermission, mscorlib, 版本=4.0.0.0, 文化=中性,PublicKeyToken=b77a5c561934e089' 失败。
来源= StackTrace:

【问题讨论】:

  • 这是在什么权限下执行的?默认情况下,Windows 服务(以及任何像 WebServer 一样运行的服务)都被阻止以任何方式访问桌面。它们在“非交互式会话”中运行。虽然我不知道你到底在做什么,但这闻起来有点 InteractiveSession 问题。
  • 这是在我的笔记本电脑上运行并具有管理员权限,将编辑问题以提供背景....感谢您输入我遗漏了一个关键位。

标签: c# wpf add-in maf


【解决方案1】:

更改安全权限

 this._wpfAddInHostView = wpfAddInToken.Activate<IWPFAddInHostView>(AddInSecurityLevel.Internet);

完全信任作品。

this._wpfAddInHostView = wpfAddInToken.Activate<IWPFAddInHostView>(AddInSecurityLevel.FullTrust);

因此,显然这与互联网安全级别有关,即部分信任。但是我还是很好奇为什么只有在调试模式下运行系统时才会出现这个异常?

【讨论】:

  • 可能导致异常的部分在死代码中? JiT 的死代码检测在 Debug 和 Release 版本中的工作方式不同。甚至在 x32 和 x64 构建或执行之间。它有很多优先级,因为它在一种情况下找到该代码,但在另一种情况下却没有。
猜你喜欢
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多