CefSharp.WPF 不会 运行 作为独立文件 .NET Core

CefSharp.WPF won't run as a self-contained file .NET Core

我能够使用 CefSharp (84.4.10.0) 在 WPF window 中创建和 运行 浏览器。 运行 在 debug/release 模式下一切正常。如果我尝试发布为独立的 win-x64 可执行文件。该应用程序崩溃并出现以下异常:

Application: App.exe
CoreCLR Version: 4.700.20.47201
.NET Core Version: 3.1.9
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Windows.Markup.XamlParseException: The invocation of the constructor on type 'CefSharp.Wpf.ChromiumWebBrowser' that matches the specified binding constraints threw an exception.
---> System.TypeInitializationException: The type initializer for 'CefSharp.Cef' threw an exception.
---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
File name: 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
at CefSharp.Cef..cctor()

--- End of inner exception stack trace ---
at CefSharp.Cef.get_IsInitialized()
at CefSharp.Wpf.ChromiumWebBrowser.NoInliningConstructor()
at CefSharp.Wpf.ChromiumWebBrowser..ctor()
--- End of inner exception stack trace ---
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at RssFeedTicker.RssFeedWindow.InitializeComponent()
at RssFeedTicker.MainWindow.Button_Click(Object sender, RoutedEventArgs e)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at 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, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run()
at App.App.Main()

我也将这些文件复制到与输出文件相同的目录中:

cef.pak
CefSharp.BrowserSubprocess.Core.dll
CefSharp.BrowserSubprocess.exe
CefSharp.Core.dll
CefSharp.dll
CefSharp.WinForms.dll
cef_100_percent.pak
cef_200_percent.pak
cef_extensions.pak
d3dcompiler_43.dll
d3dcompiler_47.dll
devtools_resources.pak
HAP.dll
icudtl.dat
libcef.dll
libEGL.dll
libGLESv2.dll
loc.txt
natives_blob.bin
snapshot_blob.bin
widevinecdmadapter.dll

该应用程序不 运行 作为独立的 exe。

Instructions building CefSharp

以上link有正确发布所需的说明:

使用运行时标识符 win-x64 或 win-x86 发布独立应用程序时,您需要将平台 属性 设置为 x64 或 x86;否则它将是 AnyCPU,并且 NuGet 包的 .targets 文件中的检查将失败。

示例:

x86: dotnet publish -f netcoreapp3.0 -r win-x86 -p:Platform=x86
x64: dotnet publish -f netcoreapp3.0 -r win-x64 -p:Platform=x64

可以通过添加 -p:PublishSingleFile=true.

将应用程序发布为单个 EXE 文件