Welcome to CefSharp! We are excited about you being here.

So, what is CefSharp, really? You probably have a bit of a clue already, since you've actually managed to download (and perhaps even compile) the software. Nonetheless, let's try to explain it in a few bullet points:

  • A framework for embedding web-browsing-like capabilities to a standard .NET application (WPF or Windows Forms).
  • A set of high-level custom controls to make it reasonably easy to integrate these capabilities in your application.
  • Built on top of CEF, the Chromium Embedded Framework by Marshall A. Greenblatt.
  • CefSharp does little more than package CEF in a nice convenient .Net Wrapper.

When getting started with CefSharp it's important to understand a few basics.

Initialize/Shutdown Cef

It's important to note that it's neccesary to Initialize the underlying CEF library. This can be achieved in one of two ways, explicitly and implicitly. When you create a new instance of ChromiumWebBrowser it will check if CEF has been initialized and if not, initialize it for you with the defaults. For those wishing to specify some custom settings then you can explicitly initialize CEF yourself like below

                        public static void Init()
                        {
                            var settings = new CefSettings();

                            // Increase the log severity so CEF outputs detailed information, useful for debugging
                            settings.LogSeverity = LogSeverity.Verbose;

                            Cef.Initialize(settings);
                        }
                        
CEF also requires that you call Shutdown when your finished with it. If you don't call Shutdown explicitly then it will be called for you on exit. You can call Shutdown explicitly which will be required in some instances.
                        private static void OnApplicationExit(object sender, EventArgs e)
                        {
                            Cef.Shutdown();
                        }
                        
It's important to note CEF that Initialize/Shutdown MUST be called on your main application thread (typically the UI thread). If you call them on different threads, your application will hang.

Settings and Command Line Arguments

There are many settings and command line arguments that can influence the way CEF behaves. Here are some examples

                        public static void Init()
                        {
                            // Specify Global Settings and Command Line Arguments
                            var settings = new CefSettings();

                            // By default CEF uses an in memory cache, to save cached data e.g. passwords you need to specify a cache path
                            // NOTE: The executing user must have sufficent privileges to write to this folder.
                            settings.CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache");

                            // There are many command line arguments that can either be turned on or off

                            // Enable WebRTC
                            settings.CefCommandLineArgs.Add("enable-media-stream");

                            // Don't use a proxy server, always make direct connections. Overrides any other proxy server flags that are passed.
                            // Slightly improves Cef initialize time as it won't attempt to resolve a proxy
                            settings.CefCommandLineArgs.Add("no-proxy-server");


                            Cef.Initialize(settings);
                        }
                        
There are some settings which can be applied to a specific ChromiumWebBrowser instance. Note BrowserSettings are set slightly different in WPF than WinForms see the example projects for more information.
                        var browser = new ChromiumWebBrowser(url)
                        {
                            BrowserSettings =
                            {
                                DefaultEncoding = "UTF-8",
                                WebGl = CefState.Disabled
                            }
                        };
                        

State-of-the-art HTML5 and Javascript support

Perhaps very obvious, but still worth mentioning. Since CefSharp is based on CEF, which is in turn based on Chromium (version 43 for the time being), CefSharp provides one of the best HTML5 rendering experiences available - 470+ points out of 555 on html5test.com. Note: YMMV depending on .DLLs you add and features you enable.

Support for both x86 and x64

Chromium 47 supports both x86 and x64, and so do we as of CefSharp 47.0.0 For the time being, it does not "auto-detect" the platform being used (since this is quite complex), so you have to choose either one for your project. This practically means that you will have to compile & package separate binaries/installers of your app for x86 and x64 respectively if you want/need to support both of them.

Custom Schemes

Declare a factory class like this:

                        internal class CefSharpSchemeHandlerFactory : ISchemeHandlerFactory
                        {
                            public const string SchemeName = "custom";
                            public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
                            {
                                if (schemeName == SchemeName && request.Url.EndsWith("CefSharp.Core.xml", System.StringComparison.OrdinalIgnoreCase))
                                {
                                    //Display the debug.log file in the browser
                                    return ResourceHandler.FromFileName("CefSharp.Core.xml", ".xml");
                                }
                                return new CefSharpSchemeHandler();
                            }
                        }
                        
...and the actual scheme handler class like this:
                        //This Resource handler (scheme handler factory returns IResourceHandler) downloads a file
                        // and servces the content as the response, you could just as easily read from a database or disk
                        public class FlashResourceHandler : ResourceHandler
                        {
                            public override bool ProcessRequestAsync(IRequest request, ICallback callback)
                            {
                                Task.Run(() =>
                                {
                                    using (callback)
                                    {
                                        var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://samples.mplayerhq.hu/SWF/zeldaADPCM5bit.swf");

                                        var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

                                        // Get the stream associated with the response.
                                        var receiveStream = httpWebResponse.GetResponseStream();
                                        var mime = httpWebResponse.ContentType;

                                        var stream = new MemoryStream();
                                        receiveStream.CopyTo(stream);
                                        httpWebResponse.Close();

                                        //Reset the stream position to 0 so the stream can be copied into the underlying unmanaged buffer
                                        stream.Position = 0;

                                        //Populate the response values - No longer need to implement GetResponseHeaders (unless you need to perform a redirect)
                                        ResponseLength = stream.Length;
                                        MimeType = mime;
                                        StatusCode = (int)HttpStatusCode.OK;
                                        Stream = stream;

                                        callback.Continue();
                                    }
                                });

                                return true;
                            }
                        }
                        
Finally, you have to register this scheme handler using some code like this:
                        public static void Init()
                        {
                            // Pseudo code; you probably need more in your CefSettings also.
                            var settings = new CefSettings();

                            settings.RegisterScheme(new CefCustomScheme
                            {
                                SchemeName = CefSharpSchemeHandlerFactory.SchemeName,
                                SchemeHandlerFactory = new CefSharpSchemeHandlerFactory()
                            });

                            Cef.Initialize(settings);
                        }
                        
It's important that the scheme registration takes place before the Cef.Initialize() gets called. (This is different to how things was being done in CefSharp version 1.)

Asynchrony

CefSharp supports custom schemes using an asynchronous pattern, which is desirable if your custom scheme handler performs e.g. network requests or other long-running operations. In other, more trivial cases (e.g. if you are just serving out static content which is already loaded in memory), you can just signal using the requestCompletedCallback() straight away, as in the example above.

JavaScript integration

If you are reading this page in either one of the CefSharp.Wpf.Example or CefSharp.WinForms.Example sample applications, you can use the boxes on the right side of the screen to run arbitrary JavaScript code towards the context of this page. By default, the content of the block below will be modified/inspected by the script code.

You can modify the value of this text field using JavaScript!
For more details and further examples read General Usage Guide - Javascript Integration The C# code for performing these kinds of interactions is quite simple. Like this:

                        using CefSharp; //EvaluateScriptAsync is an extension method
                        webBrowser.ExecuteScriptAsync(someScriptCode);
                    

The code above will run the provided JavaScript snippet (which may do interesting things, like interrogating or modifying the DOM of the page, just to name one example out of many potential ones). The execution is of the "fire-and-forget" style; any result of the execution is silently disregarded. The execution is also asynchronous in nature, a term which means that (among other things) the method may return before the actual code has actually been executed.

This is the preferrably approach if possible, since it does not deadlock the UI in any way. However, we realize that it's not suitable for all scenarios. Have faith — there is a solution even for cases where you do need to return a value. Just write your code like this:

                        using CefSharp; //EvaluateScriptAsync is an extension method
                        var result = await webBrowser.EvaluateScriptAsync("10 + 20");
                    

Please note that only a limited number of data types are supported when returning the result above. Simple value types (int, float, etc) and strings all work, but do not expect to be able to return other JavaScript objects.

Here are a few pointers that might help you further explore the wonderful world of CefSharp:

  • The GitHub project page
  • The current list of open issues. Please check this list before reporting in new issues, to ensure that your problem isn't already covered.
  • Our list of releases. Perhaps we've released something new since the version you are using? (Your current version is $foo.)