【发布时间】:2019-11-17 11:49:13
【问题描述】:
我已经在这个主题System.NullReferenceException in AppServiceResponse and System.BadImageFormatException of UWP with WPF desktop bridge package 中全面详细地描述了问题和问题,但仍然没有找到答案,是什么导致了这个失败。
上面Windows 开发中心的主题链接描述了程序集,显示了所有使用的代码和示例,包括解决问题的尝试和其他详细信息。因此,我将仅将任务的基本部分放在此 stackoverflow 主题中,以避免帖子中的文本过载,并仅关注 System.BadImageFormatException,这会在 App.Connection 中引发 null >Windows 应用程序包项目,具有从 UWP 到 WPF 应用程序的连接,根据 UWP with Desktop Extension – Part 3 lesson 创建,(向桌面扩展发送请求,读取注册表项值,以及从桌面扩展向 UWP 发送请求,处理来自桌面代码的请求)。
我尝试了不同的方法来修复它(也如上所述),但找不到任何原因的线索,至少知道在哪里寻找问题,也许我错过了一些东西,但我找不到互联网上任何地方的相关有用答案
这是消息,但 System.Runtime.WindowsRuntime.dll 已在引用中:
System.BadImageFormatException HResult=0x80131058 消息=可能 不加载文件或程序集'System.Runtime.WindowsRuntime, 版本=4.0.10.0,文化=中性,PublicKeyToken=...' 或 它的依赖项之一。不应加载参考程序集 执行。它们只能在 Reflection-only loader 中加载 语境。 (来自 HRESULT 的异常:0x80131058)来源=WpfApp1
堆栈跟踪:在 WpfApp1.MainWindow.d__4.MoveNext() 在 C:\Users\User\Desktop\UWP\App\WpfApp1\MainWindow.xaml.cs:line 58 在 System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine](TStateMachine& 状态机)在 WpfApp1.MainWindow.InitializeAppServiceConnection() 在 WpfApp1.MainWindow..ctor() 在 C:\Users\User\Desktop\UWP\App\WpfApp1\MainWindow.xaml.cs:line 37 内部异常 1:BadImageFormatException:无法加载引用 执行程序集。
任何建议、指南或示例都会非常有帮助
编辑 1: ************************************* ****************************************************** ***********
我已经尝试了以下两种方式Debug和Release的所有这些组合,以及从VS目标的菜单栏中更改解决方案平台“编译器设置”从“任何 CPU”到“X86”,反之亦然,如 System.BadImageFormatException: Could not load file or assembly [duplicate]、Solution->Properties->Configuration Properties->Configuration->Project contexts 和 Project->Properties->Build->Platform target 所示,但功能和故障在所有情况下均相同:
当前配置属性:
Console Launcher - AnyCPU
UWP - x86
Package - x86
WPF - AnyCPU
WinForms - AnyCPU
将所有解决方案应用设置为x86:
Console Launcher - x86
UWP - x86
Package - x86
WPF - x86
WinForms - x86
或x64:
Console Launcher - x64
UWP - x64
Package - x64
WPF - x64
WinForms - x64
按场景执行相同的结果:
Lessons Part 1 "Run WinForms from UWP" 和 Part 2 "Passing parameters" 成功运行,运行应用程序,发送参数并加载 WPF 或 WinForms。
lesson Part 3 的场景同样失败,InitializeAppServiceConnection(); 抛出 System.BadImageFormatException 直接调试,尝试运行 WPF 或 WinForms 来自 UWP,对请求的应用程序不执行任何操作,只显示 Console 启动器一秒钟,没有任何消息,调用桌面代码以读取 reg 密钥 抛出相同的 System.NullReferenceException
将所有应用程序设置为AnyCPU,除了 UWP,它将调试输出路径作为平台目标,解决方案或应用程序属性中的下拉菜单仅包括 x64、x86、@ 987654376@,ARM64:
Console Launcher - AnyCPU
UWP - x86
Package - AnyCPU
WPF - AnyCPU
WinForms - AnyCPU
抛出:
严重性代码描述项目文件行抑制状态 错误 MSB3270 处理器架构不匹配 正在建设的项目“x86”和处理器架构 参考 "C:\Users\User\Desktop\UWP_BD\UWP_BD\Launcher\bin\Debug\Launcher.exe", “AMD64”。这种不匹配可能会导致运行时失败。请考虑 通过更改项目的目标处理器架构 配置管理器以调整处理器架构 在您的项目和参考之间,或依赖于 具有与目标相匹配的处理器架构的参考 项目的处理器架构。包 C:\Program 文件 (x86)\微软视觉 Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2106
和:
严重性代码描述项目文件行抑制状态 警告命名空间中的元素“PropertyGroup” 'http://schemas.microsoft.com/developer/msbuild/2003' 无效 命名空间中的子元素“WapProjPath” 'http://schemas.microsoft.com/developer/msbuild/2003'。列表 预期的可能元素:'属性,VisualStudioVersion, MinimumVisualStudioVersion、AdditionalFileItemNames、 AllowUnsafeBlocks、AppConfigForCompiler、ApplicationIcon、 ApplicationRevision、ApplicationVersion、AppDesignerFolder、 AspNetConfiguration、AspNetCoreHostingModel、AspNetCoreModuleName、 AssemblyKeyContainerName、AssemblyKeyProviderName、AssemblyName、 AssemblyOriginatorKeyFile、AssemblyOriginatorKeyFileType、 AssemblyOriginatorKeyMode、AssemblyType、AutoGenerateBindingRedirects、 AutorunEnabled、BaseAddress、BootstrapperComponentsLocation、 BootstrapperComponentsUrl、BootstrapperEnabled、CharacterSet、 CheckForOverflowUnderflow、CLRSupport、UseDebugLibraries、CodePage、 配置,配置名称,配置覆盖文件, CreateDesktopShortcut, CreateWebPageOnPublish, CurrentSolutionConfigurationContents、DebugSecurityZoneURL、 DebugSymbols、DebugType、DefaultClientScript、DefaultHTMLPageLayout、 DefaultTargetSchema、DefineConstants、DefineDebug、DefineTrace、 DelaySign、DisableLangXtns、DisallowUrlActivation、 CodeAnalysisAd.... 包 C:\Users\User\Desktop\UWP_BD\UWP_BD\Package\Package.wapproj 49
和x64:
Console Launcher - AnyCPU
UWP - x64
Package - AnyCPU
WPF - AnyCPU
WinForms - AnyCPU
严重性代码描述项目文件行抑制状态 错误 MSB3270 处理器架构不匹配 正在建设的项目“MSIL”和处理器架构 参考资料 "C:\Users\User\Desktop\UWP_BD\UWP_BD\UWP_Control\bin\x64\Debug\UWP_Control.exe", “AMD64”。这种不匹配可能会导致运行时失败。请考虑 通过更改项目的目标处理器架构 配置管理器以调整处理器架构 在您的项目和参考之间,或依赖于 具有与目标相匹配的处理器架构的参考 项目的处理器架构。包 C:\Program 文件 (x86)\微软视觉 Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2106
我不确定,也许我应该将 最低版本 更改为 周年纪念版 或 Windows 10 (10.0; Build 10214)创建 UWP,默认为 Windows 10 (10.0; Build 10586):
编辑 2: ************************************* ****************************************************** ***********
我正在使用Windows 10 Pro 64-bit OS, x64-basesd processor 和Visual Studio 2019 Version 16.1.4,目标框架:.NET Framework 4.7.2 Version 4.7.03056、Microsoft.NETCore.Universal Windows Platform 6.2.8 路径文件夹C:\Program Files (x86)\Microsoft SDKs\Windows 包括文件夹:v7.0A、v8.1、v8.1A、v10.0A
在.csproj:
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.14393.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.14393.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
如果我将 Minimum Version 更改为 Windows 10 Anniversary Edition (10.0; Build 14393),我会遇到同样的错误 System.BadImageFormatException至于目标版本,以前和新的程序集都没有Part 1 和Part 2 代码。
我还遵循了Windows 开发中心上System.NullReferenceException in AppServiceResponse and System.BadImageFormatException of UWP with WPF desktop bridge package 中的Roy Li 建议,“创建一个更简单的演示来重现问题”。因此,我只根据 UWP with Desktop Extension – Part 3 和 UWP_FullTrust_3 使用 UWP 和 WPF 创建了新包。
在以前的程序集中,我在 WPF 应用程序中错过了SingleInstance.cs,但是将此代码包含到以前的程序集或新程序集中(没有“运行应用程序”和“传递参数”),会引发错误:
严重性代码描述项目文件行抑制状态 错误 CS0017 程序定义了多个入口点。编译 用 /main 指定包含条目的类型 观点。 FullTrust C:\Users\User\Desktop\UWP_BD2\Solution1\FullTrust\SingleInstance.cs 57 活动
关于这部分代码:
[STAThread]
public static void Main(string[] args)
{
SingleInstanceManager manager = new SingleInstanceManager();
manager.Run(args);
}
编辑 3: ************************************* ****************************************************** ***********
所以,对于Windows 10 Pro 64-bit OS, Version 1803 (OS Build 17134.829),合适的目标版本应该是17134,我不确定最低版本:
我在几个小时前更新了Windows。在创建 Windows Application Package Project 或不包含 17134 的 UWP, 时,目标菜单出现问题,使用更高版本时,我收到此消息:
由于某种原因,目标选择菜单不包含17134,Windows Application Package Project 也消失了:
编辑 4: ************************************* ****************************************************** ***********
我已经安装了 SDK 版本 17134,并选择了 17134 作为目标版本以及 Windows 应用程序包项目和 UWP 创建。
我已经添加了代码、引用、设置,但我得到了同样的错误!我不确定我是否能够修复它。也许完整的测试项目,带有实际代码会有所帮助。
所以,这是一个完整的解决方案repository UWP_desktop_bridge。它是 WPF 的 UWP 和控制台启动器,基于 Stefan Wick MSFT 的 UWP-FullTrust 课程的 Part 1、Part 2、Part 3
如果没有来自Part 1 的InitializeAppServiceConnection(); 场景,Part 2 可以工作,但包含InitializeAppServiceConnection(); 到 WPF InitializeComponent(); 以获得Part 3 功能,我得到了:
System.BadImageFormatException HResult=0x80131058 消息=可能 不加载文件或程序集'System.Runtime.WindowsRuntime, 版本=4.0.10.0,文化=中性,PublicKeyToken=...' 或 它的依赖项之一。不应加载参考程序集 执行。它们只能在 Reflection-only loader 中加载 语境。 (来自 HRESULT 的异常:0x80131058)来源=WpfApp1
堆栈跟踪:在 WpfApp1.MainWindow.d__4.MoveNext() 在 C:\Users\User\Desktop\UWP_desktop-bridge\Solution1\WpfApp1\MainWindow.xaml.cs:line 59 在 System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine](TStateMachine& 状态机)在 WpfApp1.MainWindow.InitializeAppServiceConnection() 在 WpfApp1.MainWindow..ctor() 在 C:\Users\User\Desktop\UWP_desktop-bridge\Solution1\WpfApp1\MainWindow.xaml.cs:line 38 内部异常 1:BadImageFormatException:无法加载引用 执行程序集。
如果使用这部分代码,则与SingleInstance.cs 相同:
[STAThread]
public static void Main(string[] args)
{
SingleInstanceManager manager = new SingleInstanceManager();
manager.Run(args);
}
严重性代码描述项目文件行抑制状态错误 CS0017 程序定义了多个入口点。编译 /main 指定包含入口点的类型。
它的功能和失败与我之前的项目相似。
编辑 5:************************************* ****************************************************** ***********
此时,我已经根据Nico Zhu - MSFT 对相关问题Send speech recognition args.Result as parameter in UWP desktop-bridge package 的回答给出的ZhuMingHao/AppSerViceTest demo 精心创建了复制粘贴版本:
调试成功加载UWP 和WPF。单击Click 中的UWP AppSerViceTest/AppServiceTest/MainPage.xaml.cs 按钮:
我也遇到了同样的异常:
System.NullReferenceException: '对象引用未设置为 对象的实例。
点击WPFSend Message按钮,它只是关闭WPF应用程序,UWPStatusBlock和InfoBlock什么都不显示。
直接调试WPF并点击Send message按钮,抛出相同System.BadImageFormatException的不同内容:
System.BadImageFormatException HResult=0x80131058 消息=可能 不加载文件或程序集'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=...' 或其其中之一 依赖关系。不应加载引用程序集以供执行。 它们只能在 Reflection-only loader 上下文中加载。 (来自 HRESULT 的异常:0x80131058)来源=WpfApp1 StackTrace:
在 WpfApp1.MainWindow.d__5.MoveNext() 中 C:\Users\User\Desktop\UWP\UWP_DB_SR\WpfApp1\MainWindow.xaml.cs:line 91 在 System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) 在 WpfApp1.MainWindow.Button_Click_1(Object sender, RoutedEventArgs e) 在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标, RoutedEventArgs routedEventArgs) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象源, RoutedEventArgs 参数,布尔 reRaised)在 System.Windows.UIElement.RaiseEventImpl(DependencyObject 发送者, RoutedEventArgs 参数)在 System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) 在 System.Windows.Controls.Primitives.ButtonBase.OnClick() 在 System.Windows.Controls.Button.OnClick() 在 System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 在 System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object 发件人,MouseButtonEventArgs e) 在 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(委托 genericHandler, 对象 genericTarget) 在 System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象 目标)在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标, RoutedEventArgs routedEventArgs) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象源, RoutedEventArgs 参数,布尔 reRaised)在 System.Windows.UIElement.ReRaiseEventAs(DependencyObject 发件人, RoutedEventArgs 参数,RoutedEvent 新事件)在 System.Windows.UIElement.OnMouseUpThunk(对象发送者, MouseButtonEventArgs e) 在 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(委托 genericHandler, 对象 genericTarget) 在 System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象 目标)在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标, RoutedEventArgs routedEventArgs) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象源, RoutedEventArgs 参数,布尔 reRaised)在 System.Windows.UIElement.RaiseEventImpl(DependencyObject 发送者, RoutedEventArgs 参数)在 System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) 在 System.Windows.UIElement.RaiseEvent(RoutedEventArgs 参数,布尔值 受信任)在 System.Windows.Input.InputManager.ProcessStagingArea() 在 System.Windows.Input.InputManager.ProcessInput(InputEventArgs 输入)在 System.Windows.Input.InputProviderSite.ReportInput(InputReport 输入报告)在 System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode 模式,Int32 时间戳,RawMouseActions 动作,Int32 x, Int32 y,Int32 轮)在 System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& 处理)在 System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam, IntPtr lParam,布尔值&处理)在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调,对象参数,Int32 numArgs)在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源, 委托回调、对象 args、Int32 numArgs、委托 catchHandler) 在 System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority 优先级、TimeSpan 超时、委托方法、对象参数、Int32 numArgs) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 消息,IntPtr wParam,IntPtr lParam)在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 帧)在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame 框架)
在 System.Windows.Application.RunDispatcher(对象忽略) System.Windows.Application.RunInternal(窗口窗口)在 System.Windows.Application.Run(窗口窗口)在 WpfApp1.App.Main() 内部的 System.Windows.Application.Run() 异常 1:BadImageFormatException:无法加载参考程序集 执行。
解决方案:****************************************** ****************************************************** **********
Stefan Wick MSFT 对 WDC 主题 System.NullReferenceException in AppServiceResponse and System.BadImageFormatException of UWP with WPF desktop bridge package 上 System.BadImageFormatException 上描述的问题给出的解决方案答案 particular UWP desktop-bridge app communication project,基于 UWP with Desktop Extension – Part 3 示例 UWP-FullTrust/UWP_FullTrust_3/ 以及求解器 my Nico Zhu - MSFT 解决方案ZhuMingHao/AppSerViceTest 的副本:
我的错误:
- 程序文件 (x86)\参考 程序集\Microsoft\Framework.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll
- Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd
必须是正确的参考设置:
- ProgramFiles(x86)%\参考 程序集\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
- ProgramFiles(x86)%\Windows 工具包\10\UnionMetadata\10.0.17134.0\Windows.winmd
【问题讨论】:
-
您是否尝试过将解决方案中的所有项目始终设置为相同的架构(x86 或 x64)?
-
@Stefan Wick MSFT 您好,首先,感谢您的反馈、指导和示例!请在上面添加的“编辑”部分检查我的结果。
-
请从developer.microsoft.com/en-us/windows/downloads/sdk-archive安装Windows SDK 17134版
-
我从您的仓库中调试了项目。 WPF 项目中的引用未正确设置。我使用完全相同的代码向解决方案添加了一个新项目 WpfApp2 并添加了以下参考,现在它工作正常:1) "%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System .Runtime.WindowsRuntime.dll" 2) "%ProgramFiles(x86)%\Windows Kits\10\UnionMetadata\10.0.17134.0\Windows.winmd"
标签: c# wpf uwp desktop-bridge badimageformatexception