Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3k
Frequently asked questions
This FAQ tries to showcase some coolCefSharp features and some of the most common gotcha's.For an in-depth guide to many of the features please readGeneral Usage Guide.
For more hints see the growing list of issues labelled asfaq-able!
- 1. How do you call a JavaScript method from .NET?
- 2. How do you call a JavaScript method that return a result?
- 3. How do you expose a .NET class to JavaScript?
- 4. "Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies.
- 5. Why does the Visual Studio WPF designer not work when I add a ChromiumWebBrowser to my app?
- 6. How do I include the Visual Studio C++ 2012/2013/2015 redistributables with my application?
- 7. How do I override JavaScript
window.alertactions and similar? - 8. Where are the CefSharp3 binaries?
- 9. Windows XP/2003 Support?
- 10. What files do I need to include when I redistribute an application that uses CefSharp?
- 11. Build process can't copy CEF files
- 12. Why does IndexedDB return a QuotaExceededError?
- 13. How do you handle a Javascript event in C#?
- 14. How can I implement WinForms drag & drop?
- 15. WPF blurry rendering
- 16. .Net Core 3.1/.Net 5/6/7+ BadImageFormatException
SeeGeneral Usage Guide - How do you call a JavaScript method
SeeGeneral Usage Guide - When can I start executing JavaScript
SeeGeneral Usage Guide - How do you call a Javascript method that return a result?
SeeGeneral Usage Guide - How do I expose a .Net class
4. Why do I get an error about "Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies. The specified module could not be found." when trying to run my CefSharp-based application? It compiles successfully, but does not run? It runs on my developer machine, though throws an exception when I copy it to another computer?
This is a common error, typically one of the following
- VC++ 2015/2019/2022 Redistributable Package is required in order to run CefSharp on non developer machines. See FAQ #6 below for more information. You can include the required dlls as part of your application.
- Not all dependencies are present in the executing folder.
CefSharpincludesunmanageddll's and resources, these are copied to the executing folder via two.propsfile which are included in your project when you install theNugetpackages. See list of required files below, make sure the required files are present. - You packaged your application for distribution via an installer and it doesn't run on the target machine. Installers don't include the
unmanagedresources by default, you'll need to add them manually. ForClickOnce, see #1314 for some pointers and solutions other users have come up with. - "Generate serialization assembly" is set to "On" or "Auto" in Properties->Build tab. Setting this to "Off" seems to resolve the issue.
- If run on Windows Server Core, some Direct X dependencies may not be installed. Install theServer Core App Compatibility Feature on Demand to resolve this issue.
A list of required files can be found here:Output files description (Redistribution)
NOTE: This also applies if you get aFileNotFoundException when initializing theWPF control inXAML.
NOTE 2: If compiling from source (not recommended, use theNuget packages) and you notice that you can no longer build in debug mode, but release builds work just fine you may need to repair your version of Visual Studio. This happens in rare cases where you will get the same exact message as a missing unmanaged .dll file as shown above.
Version57.0.0 adds minimal designer support, seehttps://github.com/cefsharp/CefSharp/pull/1989 for details.
6. How do I include theVisual Studio C++ 2012/2013/2015 redistributables with my application?
CefSharp requires theMicrosoft Visual C++ Runtime.
For versions138.0.170 and above
| CefSharp Nuget Package | VC++ Version | .Net Version |
|---|---|---|
| WinForms/WPF/OffScreen | 2022 | 4.6.2 or higher |
| WinForms.NETCore/WPF.NETCore/OffScreen.NETCore | 2022 | Net 6.0 or higher |
For versions115.3.110 and above
| CefSharp Nuget Package | VC++ Version | .Net Version |
|---|---|---|
| WinForms/WPF/OffScreen | 2019 | 4.6.2 or higher |
| WinForms.NETCore/WPF.NETCore/OffScreen.NETCore | 2019 | .Net Core 3.1 or Net 5.0 |
For versions91.1.111 and above
| CefSharp Nuget Package | VC++ Version | .Net Version |
|---|---|---|
| WinForms/WPF/OffScreen | 2019 | 4.5.2 or higher |
| WinForms.NETCore/WPF.NETCore/OffScreen.NETCore | 2019 | .Net Core 3.1 or Net 5.0 |
For versions87.1.132 and above
| CefSharp Nuget Package | VC++ Version | .Net Version |
|---|---|---|
| WinForms/WPF/OffScreen | 2015 or higher | 4.5.2 or higher |
| WinForms.NETCore/WPF.NETCore/OffScreen.NETCore | 2019 | .Net Core 3.1 or Net 5.0 |
For versions older than87.1.132
| CefSharp Version | VC++ Version | .Net Version |
|---|---|---|
| 65.0.0 and above | 2015* | 4.5.2 |
| 51.0.0 to 63.0.0 | 2013 | 4.5.2 |
| 45.0.0 to 49.0.0 | 2013 | 4.0.0 |
| 43.0.0 and below | 2012 | 4.0.0 |
* Visual C++ 2015 is the minimum version, VC++ 2017/2019/2022 are backwards compatible.
ForMicrosoft's official guide seeRedistributing Visual C++ Files onMSDN. To download visitVisual Studio C++ redistributables.
A brief summary of your options for installing/includingVC++ with your application are:
- Install the
Microsoft Visual C++ Redistributable Packageon every machine on which you wish to run yourCefSharpbased application. Once installed updates can then be managed viaWindows Update. - You can either set the
Visual Studio C++redistributables as pre-requisites of the installer (i.e. ClickOnce or WiX Toolset) - Copying over to your project the contents of this folder (Only present if you have the matching version of Visual Studio installed on your computer and if you have chosen the option
Desktop development with C++during the installation process):
# Visual Studio 2022# Example for VC++ 2022 (x64) using VS 2022 Community would look something like:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Redist\MSVC\14.38.33135\x64\Microsoft.VC143.CRT# Example for VC++ 2022 (x86) using VS 2022 Community would look something like:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Redist\MSVC\14.38.33135\x86\Microsoft.VC143.CRT# Example for VC++ 2022 (arm64) using VS 2022 Community would look something like:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Redist\MSVC\14.38.33135\arm64\Microsoft.VC143.CRT# Visual Studio 2019# Example for VC++ 2019 (x86) using VS 2019 Community would look something like:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Redist\MSVC\14.29.30133\x64\Microsoft.VC142.CRT# Example for VC++ 2019 (x64) using VS 2019 Community would look something like:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Redist\MSVC\14.29.30133\x64\Microsoft.VC142.CRT# Visual Studio 2015# For VC++ 2015 (x86)C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\redist\x86\Microsoft.VC140.CRT# For VC++ 2015 (x64)C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\redist\x64\Microsoft.VC140.CRTWith the 3rd approach you won't need to install the prerequisiteVisual C++ 2015/2019/2022 Runtime Files to your client. If you build your own version ofCefSharp from source what you deploy of course has to match your build environment. For the officialNuget releases see theReleases Branches table for details.
ForVC++ 2015/2019/2022 you may also need to package theUniversal CRT, see theDistributing Software that uses the Universal CRT section ofhttps://blogs.msdn.microsoft.com/vcblog/2015/03/03/introducing-the-universal-crt/ the last dot point titledApp-local deployment of the Universal CRT is supported has details. Windows 10 includes theUniversal CRT as an operating system component and there is no need to copy those files. UPDATE: See alsohttps://docs.microsoft.com/en-us/cpp/windows/universal-crt-deployment?view=vs-2019
Note When building from source make sure you compile inRelease mode when deploying to machines that don't haveVisual Studio installed.Visual C++ uses a different set of run-time libraries for Debug and Release builds. TheDebug run-time libraries are only installed withVisual Studio. If you use the officialNuget packages they're already built inRelease mode and you can subsequently build your app in debug mode as only theVisual C++ projects need to be compiled inRelease mode.
ImplementIJsDialogHandler and assign your instance to thebrowser.JsDialogHandler property of theChromiumWebBrowser instance to override the default behaviour.
CefSharp3 is released asNuget packages. Please seehttps://github.com/cefsharp/CefSharp/blob/master/README.md#nuget-packages for the latest stable and pre-release versions.
For a very simple example projects using the NuGet packages, see theCefSharp.MinimalExample repository. Clone it/download the source if you want a really small and simple example of how CefSharp3 can be used.
_Please Note: Platform TargetCefSharp supportsAnyCPU for version51.0.0 and above, seehttps://github.com/cefsharp/CefSharp/issues/1714 for details.
The simplest option to get up and running quickly is to either selectx86 orx64. SeeSteps to configure your solution here.
Chromium has removed support forWindows XP/2003. It is no longer possible to runCefSharp on a computer runningWindows XP/2003. Please don't createnew issues related toWindows XP. The followinghttps://github.com/cefsharp/CefSharp/wiki/Windows-XP-No-Longer-SupportedWiki page may be useful and can be edited by anyone with aGitHub account, so feel free to contribute any information you think may prove useful.
See:Output files description (Redistribution)
Sometimes the build process can't copy CEF files and will retry many times before it finally fails. This happens when CefSharp.BrowserSubprocess.exe is still running even though the application was closed.
The solution is to manually kill the CefSharp.BrowserSubprocess.exe using i.e. Task Manager.
When working with IndexedDB you must set the CefSettings CachePath to a directory where the current user has write access. In most cases you can use code like this to create a cache directory and pass the setting to Cef during Initialize():
// On Win7 this will create a directory in AppData.varcache=System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),System.IO.Path.Combine("MyApplication","cache"));if(!System.IO.Directory.Exists(cache))System.IO.Directory.CreateDirectory(cache);// Set the CachePath during initialization.varsettings=newCefSettings(){CachePath=cache};Cef.Initialize(settings);
For basic communication you can useCefSharp.PostMessage(message); in Javascript to send a message to .Net which triggers thebrowser.JavascriptMessageReceived event.
// After your ChromiumWebBrowser instance has been instantiated (for WPF directly after `InitializeComponent();` in the control constructor).// Subscribe to the following eventsbrowser.JavascriptMessageReceived+=OnBrowserJavascriptMessageReceived;browser.FrameLoadEnd+=OnFrameLoadEnd;publicvoidOnFrameLoadEnd(objectsender,FrameLoadEndEventArgse){if(e.Frame.IsMain){//In the main frame we inject some javascript that's run on mouseUp//You can hook any javascript event you like.browser.ExecuteScriptAsync(@" document.body.onmouseup = function() {//CefSharp.PostMessage can be used to communicate between the browser//and .Net, in this case we pass a simple string,//complex objects are supported, passing a reference to Javascript methods//is also supported.//See https://github.com/cefsharp/CefSharp/issues/2775#issuecomment-498454221 for detailsCefSharp.PostMessage(window.getSelection().toString()); }");}}privatevoidOnBrowserJavascriptMessageReceived(objectsender,JavascriptMessageReceivedEventArgse){varwindowSelection=(string)e.Message;//DO SOMETHING WITH THIS MESSAGE//This event is called on the threads pool, to access your UI thread//You can cast sender to ChromiumWebBrowser//use Control.BeginInvoke/Dispatcher.BeginInvoke}
Seehttps://stackoverflow.com/a/66431326/4583726 for another detailed example.
One possible solution is outlined inhttps://github.com/cefsharp/CefSharp/issues/1593#issuecomment-304451518
There are two common reasons the browser will render incorrectly
If your app is running on aHighDPI enabled display/monitor. You will need to make your applicationDPI Aware, seehttps://github.com/cefsharp/CefSharp/wiki/General-Usage#high-dpi-displayssupport
WPF representswidth/height using thedouble data type, this causes problems asCEF usesint forwidth/height values. It's necessary to cast double to int,UseLayoutRounding is enabledby default on theChromiumWebBrowser control to have the values rounded to whole pixel values byWPF.UseLayoutRounding is inherited by child elements, so it's possible for a parent elementto override theUseLayoutRounding setting.
Try setting
UseLayoutRounding=trueon the parent of theChromiumWebBrowseror even on an ancestor up the visual tree as this has helped others in the past.UseLayoutRounding disables anti-aliasing for children, so set it to
falsefor the children of the container where you want to keep the anti-aliasing, see#166Every frame is rendered as a bitmap, the defaultBitmapScalingMode used is
HighQualityChanging RenderOptions.BitmapScalingMode
toNearestNeighbor` may also work
Visual StudiosLive XAML Debugger is very useful in determining the root cause of the problem.
When using.Net Core 3.1/Net 5/6/7/8/9+ aBadImageFormatException can be very misleading.
Our diagnostics for mixed-mode assembly loading need a bit of work since they throw a BadImageFormatException for basically every failure.
As perhttps://github.com/dotnet/runtime/issues/31743#issuecomment-582168696
Basically any failure from the.Net Runtime when loading a mixed mode assembly (VC++ dll like CefSharp.Core.Runtime.dll) will cause aBadImageFormatException. Hopefully one dayMicrosoft will improve the error messaging.
Most common cause are:
- MissingMicrosoft Visual C++ Runtime
- Forgetting to distribute
ijwhost.dll(must be next toCefSharp.Core.Runtime.dll). - The bitness of the dlls doesn't match, for instance you are trying to load an
x64dll from anx86executable.