从@Sasha 的帖子和@amaitland关于' 不仅仅是不正确的架构的注释中找到了答案。BadImageFormatException
以下全部参考LP6和CefSharp.Offscreen.NetCore。我还没有将努力推进到LP5,但过程应该是相似的。
经过反复试验,我缩小了所有必要的依赖项,并找出了CefSharp不能在LinqPad中运行的原因。
以下是使其运行的步骤 -
- 像往常一样添加CefSharp.Offscreen.NetCore包进行查询
- 启用将所有 NuGet 程序集复制到单个本地文件夹(F4-> 高级)
- 在查询中添加
OnInit()
和queryPath
代码如下
- 确保
BrowserSubprocessPath
在初始化 Cef 之前已设置
这是代码。
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
路径中,但是这种方法需要将程序集目录添加到“路径”环境变量中。在内部,Linqpad 似乎设置了影子路径,因此将依赖项复制到影子文件夹可以跳过设置环境变量的需要。