I'm trying to use CefSharp WindowForm Control in the VSTO Excel Addin. The CefSharp.WinForms version is 75.1.142 and I'm making an addin on Excel 2013 (64 Bit) through VS 2017.
I'm getting FileNotFoundException: 'Could not load file or assembly 'CefSharp, Version=75.1.142.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138' or one of its dependencies. The system cannot find the file specified.' on the below code execution.
public void InitBrowser()
{
var cefSettings = new CefSettings();
cefSettings.MultiThreadedMessageLoop = true;
// I'm setting current directory to D:\\CEF\\cefsharp\\ExcelWinform\\ExcelWinformAddIn\\bin\\x64\\Debug\\ where all the CefSharp dlls and dependencies are present.
cefSettings.BrowserSubprocessPath = "D:\\CEF\\cefsharp\\ExcelWinform\\ExcelWinformAddIn\\bin\\x64\\Debug\\CefSharp.BrowserSubprocess.exe";
if (!Cef.Initialize(cefSettings, performDependencyCheck: true, browserProcessHandler: null))
{
throw new Exception("Unable to Initialize Cef");
}
browser = new ChromiumWebBrowser("http://www.google.com");
{
Dock = DockStyle.Fill;
}
BrowserSettings browserSettings = new BrowserSettings();
//browserSettings.FileAccessFromFileUrls = CefState.Enabled;
//browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
browserSettings.BackgroundColor = 0xFFFF00;
browserSettings.LocalStorage = CefState.Enabled;// enable Local access
browser.BrowserSettings = browserSettings;
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
If instead of VSTO Excel addin, I just create a simple WindowFrom application then the abve code is working fine.
Using
CefSharp
directly in anOffice VSTO
plugin/addin is not supported.As per https://docs.microsoft.com/en-us/visualstudio/vsto/architecture-of-vsto-add-ins?view=vs-2019
Office VSTO
will load youradd-in
using asAppDomain
(I believe even if you only load a single add-in the non-default appdomain will be used).CefSharp
is built usingVC++
which cannot be loaded in the non-defaultAppDomain
. The error you are seeing is related to the plugin being unable to loadlibcef.dll
, you could resolve this issue, only to be faced withCannot pass a GCHandle across AppDomains
.You can run
CefSharp
out of process, for which there is an example. It's a little outdated, the fundamentals should still be relevant. See https://github.com/stever/AppHostCefSharpMy personal recommendation is to use CefGlue or ChromiumFx. Both are
P/Invoke
based so use a fundamentally different approach to interacting with the unmanagedlibcef.dll
.See also