首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CefSharp.offscreen in LinqPad

CefSharp.offscreen in LinqPad
EN

Stack Overflow用户
提问于 2021-11-11 04:36:18
回答 2查看 210关注 0票数 1

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‘。试图加载格式不正确的程序。

我试过了

  • LP5/6 32和64位
  • 通过nuget添加头孢夏普
  • 从文件系统手动引用..dll
  • 引用x86或x64 ..dll的
  • 将..dll复制到程序集搜索路径中
  • 在环境路径中添加nuget路径

上面的每一个组合。

我不理解Visual在nuget包中使用的程序集解析过程,但是无论它做了什么,我都想至少在Linqpad中进行模拟,这样我就可以在测试一些简单的东西时避免VS仪式。

我假设手动引用正确的..dll并可能在某个地方设置路径就足够了,但我是ideas=>EOF。

CefSharp可以在VS / MSBuild之外运行吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-17 00:47:40

找到答案的动机来自@Sasha的帖子和@amaitland的备注关于BadImageFormatException不仅仅是不正确的架构。

以下都是关于LP6CefSharp.Offscreen.NetCore的内容。我并没有将这些努力推入LP5,但过程应该是类似的。

经过一些尝试和错误之后,我缩小了所有必要的依赖项,并找出了为什么CefSharp不会在LinqPad中运行。

以下是让它运行的步骤-

  1. CefSharp.Offscreen.NetCore包按常规添加到查询中
  2. 启用将所有NuGet程序集复制到单个本地文件夹(F4->Advanced)
  3. 向查询中添加OnInit()queryPath代码,如下所示
  4. 确保在初始化Cef之前设置了BrowserSubprocessPath

这是密码。

代码语言:javascript
复制
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.dllBrowserSubprocess.exe等。来看看。依赖关系分散在NuGet包之间,试图直接从.nuget缓存中解析它们只是不起作用。因此,所有这些都需要手动进入正在运行的查询阴影路径。

我最初确实将所有文件复制到Assembly.GetExecutingAssembly().Location路径中,但是这种方法需要将程序集目录添加到" path“环境变量中。在内部,Linqpad似乎设置了影子路径,因此将依赖项复制到影子文件夹将跳过设置环境变量的需要。

票数 0
EN

Stack Overflow用户

发布于 2021-11-15 01:14:43

由于LinqPad正在使用的影子复制,它无法工作。这里有一个让你的问题消失的方法(扰流板警告:不完全是,继续读):

代表LinqPad v5

  1. 将所有CefSharp库复制到一个单独的文件夹中(不要忘记cef.redist)。
  2. 在LinqPad首选项对话框(高级/执行)中,将Do not shadow assembly references设置为True,重新启动LinqPad。
  3. 在LinqPad查询中编写代码。
  4. 从步骤1中设置的文件夹中引用CefSharp库。
  5. 运行查询。

对于以前的LinqPad (早于v5)

  1. 在LinqPad查询中编写代码。
  2. 参考CefSharp库,所以从您的问题中可以得到一个异常
  3. 找到一个LinqPad工作目录(通常类似于C:\Users\<user>\AppData\Local\Temp\LINQPad5\_yyigmhzg)。
  4. 将所有CefSharp库复制到此文件夹(不要忘记cef.redist)。
  5. 在LinqPad中,单击Ctrl + Shift + F5;这将重置查询状态。
  6. 重新运行查询。

现在,所有引用的库都应该加载。但在那之后,你可能会面临更多的问题。

我不能让CefSharp.MinimalExample工作。LinqPad一直在为我提供一条神秘的信息,Query ended because an uncatchable exception was thrown和一个垃圾堆。

虽然我不确定您是否会让CefSharp按照LinqPad的意图工作,但这可能会使您做得更深入一些。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69923244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档