Skip to content

Commit 1fb3f17

Browse files
committed
AnyCPU - Add CefRuntime.SubscribeAnyCpuAssemblyResolver/LoadCefSharpCoreRuntimeAnyCpu
Simplify using CefSharp when targeting AnyCPU, it's now possible to include this as library code now that CefSharp.dll targets AnyCPU. There is no .Net Core/.Net 5.0 implementation of this class as the CefSharp.Core.Runtime.dll is already loaded dynamically based on architecture. - Call CefRuntime.LoadCefSharpCoreRuntimeAnyCpu to immediately load the CefSharp.Core.Runtime.dll and all dependant resources will be loaded based on this instance - Call LoadCefSharpCoreRuntimeAnyCpu.SubscribeAnyCpuAssemblyResolver to use load assembly when required.
1 parent d8abfa6 commit 1fb3f17

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

CefSharp/CefRuntime.cs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright © 2021 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System;
6+
using System.IO;
7+
using System.Reflection;
8+
9+
namespace CefSharp
10+
{
11+
/// <summary>
12+
/// CefRuntime - Used to simplify loading of the CefSharp architecture specific resources.
13+
/// Typical use case would be when you are targeting AnyCPU
14+
/// </summary>
15+
public static class CefRuntime
16+
{
17+
private static ResolveEventHandler currentDomainAssemblyResolveHandler;
18+
19+
/// <summary>
20+
/// When using AnyCPU the architecture specific version of CefSharp.Core.Runtime.dll
21+
/// needs to be loaded (x64/x86).
22+
/// This method subscribes to the <see cref="AppDomain.AssemblyResolve"/> event
23+
/// for <see cref="AppDomain.CurrentDomain"/> and loads the CefSharp.Core.Runtime.dll
24+
/// based on <see cref="Environment.Is64BitProcess"/>.
25+
/// This method MUST be called before you call Cef.Initialize, create your first ChromiumWebBrowser instance, basically
26+
/// before anything CefSharp related happens. This method is part of CefSharp.dll which is an AnyCPU library and
27+
/// doesn't have any references to the CefSharp.Core.Runtime.dll so it's safe to use.
28+
/// </summary>
29+
/// <param name="basePath">
30+
/// The path containing the x64/x86 folders which contain the CefSharp/CEF resources.
31+
/// If null then AppDomain.CurrentDomain.SetupInformation.ApplicationBase will be used as the path.
32+
/// (</param>
33+
public static void SubscribeAnyCpuAssemblyResolver(string basePath = null)
34+
{
35+
if(currentDomainAssemblyResolveHandler != null)
36+
{
37+
throw new Exception("UseAnyCpuAssemblyResolver has already been called, call ");
38+
}
39+
40+
if(basePath == null)
41+
{
42+
basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
43+
}
44+
45+
currentDomainAssemblyResolveHandler = (sender, args) =>
46+
{
47+
if (args.Name.StartsWith("CefSharp.Core.Runtime"))
48+
{
49+
string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
50+
string archSpecificPath = Path.Combine(basePath,
51+
Environment.Is64BitProcess ? "x64" : "x86",
52+
assemblyName);
53+
54+
return File.Exists(archSpecificPath)
55+
? System.Reflection.Assembly.LoadFile(archSpecificPath)
56+
: null;
57+
}
58+
59+
return null;
60+
};
61+
62+
AppDomain.CurrentDomain.AssemblyResolve += currentDomainAssemblyResolveHandler;
63+
}
64+
65+
/// <summary>
66+
/// Unsubscribe from the <see cref="AppDomain.AssemblyResolve"/> event
67+
/// for <see cref="AppDomain.CurrentDomain"/> that was added in <see cref="UseAnyCpuAssemblyResolver"/>
68+
/// </summary>
69+
public static void UnsubscribeAnyCpuAssemblyResolver()
70+
{
71+
AppDomain.CurrentDomain.AssemblyResolve -= currentDomainAssemblyResolveHandler;
72+
73+
currentDomainAssemblyResolveHandler = null;
74+
}
75+
76+
/// <summary>
77+
/// When using AnyCPU the architecture specific version of CefSharp.Core.Runtime.dll
78+
/// needs to be loaded (x64/x86).
79+
/// This method calls <see cref="Assembly.LoadFile(string)"/> to immediately load CefSharp.Core.Runtime.dll
80+
/// based on <see cref="Environment.Is64BitProcess"/>.
81+
/// This method MUST be called before you call Cef.Initialize, create your first ChromiumWebBrowser instance, basically
82+
/// before anything CefSharp related happens. This method is part of CefSharp.dll which is an AnyCPU library and
83+
/// doesn't have any references to the CefSharp.Core.Runtime.dll so it's safe to use.
84+
/// </summary>
85+
/// <param name="basePath">
86+
/// The path containing the x64/x86 folders which contain the CefSharp/CEF resources.
87+
/// If null then AppDomain.CurrentDomain.SetupInformation.ApplicationBase will be used as the path.
88+
/// (</param>
89+
public static void LoadCefSharpCoreRuntimeAnyCpu(string basePath = null)
90+
{
91+
const string assemblyName = "CefSharp.Core.Runtime.dll";
92+
93+
if (basePath == null)
94+
{
95+
basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
96+
}
97+
98+
var env = Environment.Is64BitProcess ? "x64" : "x86";
99+
string archSpecificPath = Path.Combine(basePath,
100+
env,
101+
assemblyName);
102+
103+
if (File.Exists(archSpecificPath))
104+
{
105+
Assembly.LoadFile(archSpecificPath);
106+
}
107+
else
108+
{
109+
throw new FileNotFoundException("Unable to load for arch " + env, archSpecificPath);
110+
}
111+
}
112+
}
113+
}

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<Compile Include="Callback\TaskPrintToPdfCallback.cs" />
8787
<Compile Include="Callback\TaskResolveCallback.cs" />
8888
<Compile Include="CdmRegistration.cs" />
89+
<Compile Include="CefRuntime.cs" />
8990
<Compile Include="DefaultApp.cs" />
9091
<Compile Include="DevToolsExtensions.cs" />
9192
<Compile Include="DevTools\DevToolsClient.cs" />

CefSharp/CefSharp.netcore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
</ItemGroup>
4343

4444
<ItemGroup>
45+
<Compile Remove="CefRuntime.cs" />
4546
<Compile Remove="DevTools\DevToolsClient.Generated.cs" />
4647
<Compile Remove="Internals\Partial\ChromiumWebBrowser.Partial.cs" />
4748
</ItemGroup>

0 commit comments

Comments
 (0)