You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Packages.config with multiple projects (csproj/vbproj)
If you have CefSharp installed in a library project that uses pacakges.config things become a little tricky. The None entries will copy the CEF resources to a directly referring project, changing to Content (changing the CefSharpBuildAction to Content changes to using <Content/> entries) means the files are only copied to the library project, not any referring projects.
Installing the CefSharp packages in your main project and adding <CefSharpBuildAction>Content</CefSharpBuildAction> to the first PropertyGroup of your main project file is one option as listed above.
Converting your library project to PackageReference should allow for the references to flow through to consuming projects as buildTransitive. Noting that I haven't tested this with a mix of PackageReference and packages.config, so if you mix and match then option 1. is the fall-back if this doesn't work.
AnyCPU
Publishing when targeting AnyCPU behaves strangely, the x64 folder is missing a copy of CefSharp.dll which is required by the BrowserSubprocess. The file is copied correctly when the projects builds normally, so there's some quirk of the Publish process. I have yet to identify the root cause of the issue.
To workaround can self host the BrowserSubProcess to workaround the problem. Self Hosting means that instead of using CefSharp.BrowserSubProcess.exe to run the renderer, gpu, network, audio processes that are typically spawned, you use your own applications executable. You also MUST set performDependencyCheck to false.
WinForms
For WinForms your Program.cs would look something like:
publicstaticclassProgram{[STAThread]publicstaticintMain(string[]args){CefRuntime.SubscribeAnyCpuAssemblyResolver();//For Windows 7 and above, best to include relevant app.manifest entries as wellCef.EnableHighDPISupport();varexitCode=CefSharp.BrowserSubprocess.SelfHost.Main(args);if(exitCode>=0){returnexitCode;}varsettings=newCefSettings(){//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist dataCachePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"CefSharp\\Cache"),BrowserSubprocessPath=System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName};//Perform dependency check to make sure all relevant resources are in our output directory.//IMPORTANT: MUST set performDependencyCheck falseCef.Initialize(settings,performDependencyCheck:false,browserProcessHandler:null);varbrowser=newBrowserForm();Application.Run(browser);return0;}}
WPF
For WPF this is a little more difficult as the compiler generates the Main method for you. You need to:
Create your own custom entry point (typically adding a Program.cs file with a static Main method would be the convention)
Change the Startup Object to use your own main method, (Right click Properties on your project, Startup Object is a dropdown).
An example of Program.cs for WPF might look like this:
publicstaticclassProgram{[STAThread]publicstaticintMain(string[]args){CefRuntime.SubscribeAnyCpuAssemblyResolver();//For Windows 7 and above, best to include relevant app.manifest entries as wellCef.EnableHighDPISupport();varexitCode=CefSharp.BrowserSubprocess.SelfHost.Main(args);if(exitCode>=0){returnexitCode;}varsettings=newCefSettings(){//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist dataCachePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"CefSharp\\Cache"),BrowserSubprocessPath=System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName};//Perform dependency check to make sure all relevant resources are in our output directory.//IMPORTANT: MUST set performDependencyCheck falseCef.Initialize(settings,performDependencyCheck:false,browserProcessHandler:null);varapp=newApp();app.InitializeComponent();returnapp.Run();}}
Troubleshooting
Add the following to your csproj/vbproj file at the end
Change your MSBuild project output verbosity to Normal:
1. Go to Tools->Options menu in VS
2. Open Projects and Solutions->Build and Run
3. Change the value of the MSBuild project build output verbosity. Pick Normal or higher
After you build, check the output window for a block of text that looks like:
You can check CefSharpBuildAction was set correctly, confirm the PlatformTarget is as expected. Post this information when asking for help.
Testing with MinimalExample
The MinimalExample can be deployed using ClickOnce and I suggest anyone having problems follow these instructions and get that working as a reference. Then you can compare your project to the example.
Option 1:
Open CefSharp.MinimalExample.sln in VS2019
Build
Close Solution
Edit CefSharp.MinimalExample.WinForms.csproj adding <CefSharpBuildAction>Content</CefSharpBuildAction> to the first PropertyGroup (Example at CefSharp.MinimalExample.WinForms.csproj#L31)
Open CefSharp.MinimalExample.sln in VS2019
Rebuild
Comment out Microsoft.Windows.Common-Controls from app.manifest in CefSharp.MinimalExample.WinForms
Right click CefSharp.MinimalExample.WinForms and publish
Use default options
Install ClickOnce deployed app.
Make sure that when installing on client machines that Microsoft Visual C++ 2019 or higher is installed.
You can use the Windows Sandbox for testing purposes, making sure to install Visual C++ 2019 or highter
Loads as expected.
A lot of work goes into maintaining supporting this project, if you are using CefSharp in a commercial application please support my efforts by sponsoring the project.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Looking for some help? Get priority support by sponsoring the project
Publishing with ClickOnce may require additional steps depending on your project format and structure.
PlatformTargetisx86/x64AnyCPUrequires that youSelfHosttheBrowserSubProcesssee section below.Packages.config with a single project (csproj/vbproj)
If you are using the older Nuget packages.config format then you have two options
Option 1:
Add the following to a
PropertyGroupin yourcsproj/vbprojfile before the.targetsentries (which are at the bottom).Option2:
Follow the
Microsoftdocumentation to Migrate from packages.config to PackageReference.Packages.config with multiple projects (csproj/vbproj)
If you have
CefSharpinstalled in a library project that usespacakges.configthings become a little tricky. The None entries will copy theCEFresources to a directly referring project, changing to Content (changing theCefSharpBuildActiontoContentchanges to using<Content/>entries) means the files are only copied to the library project, not any referring projects.CefSharppackages in your main project and adding<CefSharpBuildAction>Content</CefSharpBuildAction>to the firstPropertyGroupof your main project file is one option as listed above.PackageReferenceshould allow for the references to flow through to consuming projects as buildTransitive. Noting that I haven't tested this with a mix ofPackageReferenceandpackages.config, so if you mix and match then option1.is the fall-back if this doesn't work.AnyCPU
Publishing when targeting
AnyCPUbehaves strangely, the x64 folder is missing a copy of CefSharp.dll which is required by the BrowserSubprocess. The file is copied correctly when the projects builds normally, so there's some quirk of the Publish process. I have yet to identify the root cause of the issue.To workaround can self host the BrowserSubProcess to workaround the problem. Self Hosting means that instead of using
CefSharp.BrowserSubProcess.exeto run therenderer,gpu,network,audioprocesses that are typically spawned, you use your own applications executable. You also MUST set performDependencyCheck to false.WinForms
For
WinFormsyourProgram.cswould look something like:WPF
For
WPFthis is a little more difficult as the compiler generates the Main method for you. You need to:Program.csfile with astatic Mainmethod would be the convention)Startup Objectto use your own main method, (Right click Properties on your project, Startup Object is a dropdown).An example of
Program.csforWPFmight look like this:Troubleshooting
Add the following to your
csproj/vbprojfile at the endChange your
MSBuild project output verbositytoNormal:After you build, check the output window for a block of text that looks like:
You can check
CefSharpBuildActionwas set correctly, confirm thePlatformTargetis as expected. Post this information when asking for help.Testing with MinimalExample
The MinimalExample can be deployed using
ClickOnceand I suggest anyone having problems follow these instructions and get that working as a reference. Then you can compare your project to the example.Option 1:
CefSharp.MinimalExample.WinForms.csprojadding<CefSharpBuildAction>Content</CefSharpBuildAction>to the firstPropertyGroup(Example at CefSharp.MinimalExample.WinForms.csproj#L31)Microsoft.Windows.Common-Controlsfromapp.manifestinCefSharp.MinimalExample.WinFormsCefSharp.MinimalExample.WinFormsand publishMake sure that when installing on client machines that
Microsoft Visual C++ 2019 or higheris installed.You can use the
Windows Sandboxfor testing purposes, making sure to installVisual C++ 2019 or highterLoads as expected.
A lot of work goes into maintaining supporting this project, if you are using
CefSharpin a commercial application please support my efforts by sponsoring the project.Beta Was this translation helpful? Give feedback.
All reactions