1

I have an application that uses a CefSharp.OffScreen browser to do a bit of scraping work. It runs in a scripting environment using CSScript to execute a dynamically-loaded class that calls CefSharp to do the work.

CefSharp works perfectly fine in a bin environment. There's even a way to place all of the CefSharp files in their own folder, using ProbingPath. But this only works if CefSharp is running in the same folder as your primary executable, or in the ProbingPath folder you have set up. ProbingPath can only be set up as a subfolder of the executable path.

CefSharp is marshalled from a simple DLL I wrote called MyCompany.Browser. It has a single method in it called Browse that accepts an url string and returns an html string. It does this by spinning up a CefSharp.OffScreen browser and executing it, and this all works perfectly fine in an ordinary bin environment.

In my dynamically-loaded script that CSScript executes, I have code that is functionally equivalent to this (a greatly-simplified, highly-contrived test):

using MyCompany.Browser;

public string GetResult(string url)
{
    using (var browser = new CefSharpHeadlessBrowser())
    {
        return browser.Browse(url);
    }
}

The way CSScript works is, if you give it a using MyCompany.Browser; reference, it will look in the folder from which it was executed, find the dll named MyCompany.Browser.dll, and load it for you. CefSharpHeadlessBrowser is a type in this MyCompany.Browser namespace, in a DLL called MyCompany.Browser.dll, in the primary bin folder on a server somewhere, which CSScript happily instantiates.

However, when I try to execute the browser.Browse() method, this happens:

Unable to locate required Cef/CefSharp dependencies:
Missing:CefSharp.BrowserSubprocess.exe
Missing:CefSharp.BrowserSubprocess.Core.dll
Missing:CefSharp.Core.dll
Missing:CefSharp.dll
Missing:icudtl.dat
Missing:libcef.dll

These messages are coming from a CheckDependencies() method in CefSharp.dll, which MyCompany.Browser.dll has loaded. However, that's as far as the loading process gets. MyCompany.Browser has loaded because it's in the same folder as CSScript; CefSharp.dll loaded long enough to run its dependency checker and give me the above error messages because MyCompany.Browser.dll has a direct reference to it.

But the rest of CefSharp didn't load, because the dynamically-loaded script is not running in the same bin folder. Instead, it's running in:

c:\Users\admin\AppData\Local\Temp\CSSCRIPT\dynamic\879ec55a-f761-4306-a79c-1af6cf08b312.tmp

I would love for someone to be able to tell me that CefSharp has a registry entry, an app.config element, or something that will notify CefSharp that "Here is where you can find all of your files." Is there something like that, and if not, is there some other way to fix this?

Community
  • 1
  • 1
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • `CEF` expects all the executables to be in your executing path, there is no magic `CefSharp` setting to get around this (as `CEF` doesn't expose any sort of configuration options other than subprocesspath). So you'll have to get creative. You can embed them as resources into your dll and extract them into your executing path at runtime or try `LoadLibrary` before any call is made. I'm sure `Google` has some other options. – amaitland May 06 '16 at 06:48
  • No, at Windows CEF can be located in any place. LoadLibrary on libcef.dll on expected location before any CefSharp access should help. – Dmitry Azaraev May 06 '16 at 12:44
  • I.e. dependency checker should obtain base directory of libcef module and check against, or do not check at all, otherwise ouch. – Dmitry Azaraev May 06 '16 at 12:48
  • @amaitland: I got closer by executing `Directory.SetCurrentDirectory("PathToCefsharpFolder");` but now I'm getting the error "Failed to create offscreen browser. Call Cef.Initialize() first." LoadLibrary on libcef.dll didn't help. Do I have to load every library in the CefSharp package manually? – Robert Harvey May 06 '16 at 14:51
  • You shouldn't have to load every library explicitly, try the approach `CefGlue` uses https://bitbucket.org/xilium/xilium.cefglue/src/7d52418166488a9036b162bd178bf17fde67687e/CefGlue/CefRuntime.cs?at=default&fileviewer=file-view-default#CefRuntime.cs-107 The key is `LOAD_WITH_ALTERED_SEARCH_PATH` – amaitland May 09 '16 at 01:03
  • Also set the `BrowserSubProcessPath` and `LocalesDirPath` as absolute paths. If the `pak` files don't load then set `ResourcesDirPath` to the path that contains `cef.pak` etc. – amaitland May 09 '16 at 01:07
  • @amaitland Set `BrowserSubProcessPath` to sub path solved my problem. It deserves an answer. – Val Nov 10 '16 at 06:10

0 Answers0