LinqPad是我的goto,没有什么是我无法处理的。
然而,在我的一生中,我无法让CefSharp (特别是OffScreen)运行。
我经常遇到以下任何一个错误
无法加载文件或程序集“CefSharp.Core.Runtime、Version=95.7.141.0、Culture=neutral、PublicKeyToken=40c4b6fc221f4138”或其依赖项之一。系统找不到指定的文件。 BadImageFormatException:无法加载文件或程序集'CefSharp.Core.Runtime,Version=95.7.141.0,Culture=neutral,PublicKeyToken=40c4b6fc221f4138‘。试图加载格式不正确的程序。
我试过了
上面的每一个组合。
我不理解Visual在nuget包中使用的程序集解析过程,但是无论它做了什么,我都想至少在Linqpad中进行模拟,这样我就可以在测试一些简单的东西时避免VS仪式。
我假设手动引用正确的..dll并可能在某个地方设置路径就足够了,但我是ideas=>EOF。
CefSharp可以在VS / MSBuild之外运行吗?
发布于 2021-11-17 00:47:40
找到答案的动机来自@Sasha的帖子和@amaitland的备注关于BadImageFormatException不仅仅是不正确的架构。
以下都是关于LP6和CefSharp.Offscreen.NetCore的内容。我并没有将这些努力推入LP5,但过程应该是类似的。
经过一些尝试和错误之后,我缩小了所有必要的依赖项,并找出了为什么CefSharp不会在LinqPad中运行。
以下是让它运行的步骤-
OnInit()和queryPath代码,如下所示BrowserSubprocessPath这是密码。
async Task Main()
{
var are = new AutoResetEvent(false);//my technique for waiting for the browser
var sett = new CefSettings();
sett.BrowserSubprocessPath = this.queryPath + @"\CefSharp.BrowserSubprocess.exe"; //CefSharp will complain it cant find it
if (!Cef.IsInitialized)
Cef.Initialize(sett);
var browser = new ChromiumWebBrowser("http://www.google.com");
browser.LoadingStateChanged += (sender, args) => { if (!args.IsLoading) are.Set(); };
are.WaitOne();
await browser.WaitForInitialLoadAsync();
var html = await browser.GetBrowser().MainFrame.GetSourceAsync();
html.Dump("winner winner chicken dinner");
}
//this is the location of the queries shaddow folder
string queryPath = Path.GetDirectoryName(typeof(CefSettings).Assembly.Location);
void OnInit() // Executes when the query process first loads
{
if (!Directory.Exists(queryPath + @"\locales")) //subdirectory of cef.redist
{
var nugetPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var sources = new[] {
/*paths here are hardcoded version dependant. Can get cefsharp.common.netcore version
from Util.CurrentQuery.NuGetReferences, but cef.redist not available via that method. */
@"cef.redist.x64\95.7.14\CEF", //contans all the Cef dependencies needed
@"cefsharp.common.netcore\95.7.141\runtimes\win-x64\lib\netcoreapp3.1", //mainly for ijwhost.dll
@"cefsharp.common.netcore\95.7.141\runtimes\win-x64\native"}; //contains BrowserSubprocess & friends
var dst = new DirectoryInfo(queryPath);
foreach (var path in sources)
{
var src = new DirectoryInfo($@"{nugetPath}\.nuget\packages\{path}");
CopyFilesRecursively(src, dst);
}
}
}
//curtesy of https://stackoverflow.com/a/58779/2738249 with slight mod
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
{
var dst = Path.Combine(target.FullName, file.Name);
if (!File.Exists(dst))
file.CopyTo(dst);
}
}对那些有兴趣的人来说-
CefSharp需要每个依赖项都位于同一个目录中,这样它们就可以在运行时被解析,但是Linqpad只从NuGet包复制几个键dll。没有cef.redist文件,ijwhost.dll或BrowserSubprocess.exe等。来看看。依赖关系分散在NuGet包之间,试图直接从.nuget缓存中解析它们只是不起作用。因此,所有这些都需要手动进入正在运行的查询阴影路径。
我最初确实将所有文件复制到Assembly.GetExecutingAssembly().Location路径中,但是这种方法需要将程序集目录添加到" path“环境变量中。在内部,Linqpad似乎设置了影子路径,因此将依赖项复制到影子文件夹将跳过设置环境变量的需要。
发布于 2021-11-15 01:14:43
由于LinqPad正在使用的影子复制,它无法工作。这里有一个让你的问题消失的方法(扰流板警告:不完全是,继续读):
代表LinqPad v5
CefSharp库复制到一个单独的文件夹中(不要忘记cef.redist)。Do not shadow assembly references设置为True,重新启动LinqPad。CefSharp库。对于以前的LinqPad (早于v5)
CefSharp库,所以从您的问题中可以得到一个异常C:\Users\<user>\AppData\Local\Temp\LINQPad5\_yyigmhzg)。CefSharp库复制到此文件夹(不要忘记cef.redist)。Ctrl + Shift + F5;这将重置查询状态。现在,所有引用的库都应该加载。但在那之后,你可能会面临更多的问题。
我不能让CefSharp.MinimalExample工作。LinqPad一直在为我提供一条神秘的信息,Query ended because an uncatchable exception was thrown和一个垃圾堆。
虽然我不确定您是否会让CefSharp按照LinqPad的意图工作,但这可能会使您做得更深入一些。
https://stackoverflow.com/questions/69923244
复制相似问题