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 well
Cef.EnableHighDPISupport();varexitCode= CefSharp.BrowserSubprocess.SelfHost.Main(args);if(exitCode >= 0){returnexitCode;}varsettings=new CefSettings(){//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 false
Cef.Initialize(settings, performDependencyCheck:false, browserProcessHandler:null);varbrowser=new BrowserForm();
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 well
Cef.EnableHighDPISupport();varexitCode= CefSharp.BrowserSubprocess.SelfHost.Main(args);if(exitCode >= 0){returnexitCode;}varsettings=new CefSettings(){//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 false
Cef.Initialize(settings, performDependencyCheck:false, browserProcessHandler:null);varapp=new App();
app.InitializeComponent();return app.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
-
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.
PlatformTarget
isx86/x64
AnyCPU
requires that youSelfHost
theBrowserSubProcess
see 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
PropertyGroup
in yourcsproj/vbproj
file before the.targets
entries (which are at the bottom).Option2:
Follow the
Microsoft
documentation to Migrate from packages.config to PackageReference.Packages.config with multiple projects (csproj/vbproj)
If you have
CefSharp
installed in a library project that usespacakges.config
things become a little tricky. The None entries will copy theCEF
resources to a directly referring project, changing to Content (changing theCefSharpBuildAction
toContent
changes to using<Content/>
entries) means the files are only copied to the library project, not any referring projects.CefSharp
packages in your main project and adding<CefSharpBuildAction>Content</CefSharpBuildAction>
to the firstPropertyGroup
of your main project file is one option as listed above.PackageReference
should allow for the references to flow through to consuming projects as buildTransitive. Noting that I haven't tested this with a mix ofPackageReference
andpackages.config
, so if you mix and match then option1.
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 therenderer
,gpu
,network
,audio
processes that are typically spawned, you use your own applications executable. You also MUST set performDependencyCheck to false.WinForms
For
WinForms
yourProgram.cs
would look something like:WPF
For
WPF
this is a little more difficult as the compiler generates the Main method for you. You need to:Program.cs
file with astatic Main
method would be the convention)Startup Object
to use your own main method, (Right click Properties on your project, Startup Object is a dropdown).An example of
Program.cs
forWPF
might look like this:Troubleshooting
Add the following to your
csproj/vbproj
file at the endChange your
MSBuild project output verbosity
toNormal
:After you build, check the output window for a block of text that looks like:
You can check
CefSharpBuildAction
was set correctly, confirm thePlatformTarget
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:
CefSharp.MinimalExample.WinForms.csproj
adding<CefSharpBuildAction>Content</CefSharpBuildAction>
to the firstPropertyGroup
(Example at CefSharp.MinimalExample.WinForms.csproj#L31)Microsoft.Windows.Common-Controls
fromapp.manifest
inCefSharp.MinimalExample.WinForms
CefSharp.MinimalExample.WinForms
and publishMake 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 installVisual 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.Beta Was this translation helpful? Give feedback.
All reactions