I have created a class library project with WPF windows in it. In one WPF window I want to get a CefSharp browser. My project should be with configuration AnyCPU. In different tutorials I saw that one of the points to tune AnyCPU configuration in an executable project with CefSharp is to set (csproj)
<Prefer32Bit>true</Prefer32Bit>
But in class library projects, this property is disabled. How can I enable AnyCPU Support for CefSharp in my class library?
See the Documentation: General Usage Guide
There are several solutions to enable AnyCPU Support. I have used the following:
First, install the dependencies via NuGet.
Then, add <CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>
to the first PropertyGroup
of the .csproj
file containing the CefSharp.Wpf
PackageReference for the CefSharp.Wpf.ChromiumWebBrowser
Control.
Now, write an Assembly Resolver to find the correct unmanaged DLLs depending on the current architecture:
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.StartsWith("CefSharp"))
{
string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
string architectureSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
assemblyName);
return File.Exists(architectureSpecificPath)
? Assembly.LoadFile(architectureSpecificPath)
: null;
}
return null;
}
Finally, initialize CefSharp with at least these settings:
var settings = new CefSettings()
{
BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
"CefSharp.BrowserSubprocess.exe")
};
Cef.Initialize(settings);