问题
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.
回答1:
Using CefSharp
directly in an Office VSTO
plugin/addin is not supported.
If multiple VSTO Add-ins are installed for an application, each VSTO Add-in is loaded in a different application domain.
As per https://docs.microsoft.com/en-us/visualstudio/vsto/architecture-of-vsto-add-ins?view=vs-2019 Office VSTO
will load your add-in
using as AppDomain
(I believe even if you only load a single add-in the non-default appdomain will be used).
CefSharp
is built using VC++
which cannot be loaded in the non-default AppDomain
. The error you are seeing is related to the plugin being unable to load libcef.dll
, you could resolve this issue, only to be faced with Cannot 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/AppHostCefSharp
My personal recommendation is to use CefGlue or ChromiumFx. Both are P/Invoke
based so use a fundamentally different approach to interacting with the unmanaged libcef.dll
.
See also
- https://github.com/cefsharp/CefSharp/issues/1191
- https://github.com/cefsharp/CefSharp/issues/351
- Cefsharp in ExcelDNA
- Cannot pass a GCHandle across AppDomains: solution without delegates?
- https://developercommunity.visualstudio.com/content/problem/169359/ccli-dll-is-initialized-in-wrong-appdomain.html
来源:https://stackoverflow.com/questions/58024079/cefsharp-in-vsto-addin