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.
This commit is contained in:
Alex Maitland
2021-06-04 16:57:09 +10:00
parent d8abfa6035
commit 1fb3f17599
3 changed files with 115 additions and 0 deletions
+113
View File
@@ -0,0 +1,113 @@
// Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.IO;
using System.Reflection;
namespace CefSharp
{
/// <summary>
/// CefRuntime - Used to simplify loading of the CefSharp architecture specific resources.
/// Typical use case would be when you are targeting AnyCPU
/// </summary>
public static class CefRuntime
{
private static ResolveEventHandler currentDomainAssemblyResolveHandler;
/// <summary>
/// When using AnyCPU the architecture specific version of CefSharp.Core.Runtime.dll
/// needs to be loaded (x64/x86).
/// This method subscribes to the <see cref="AppDomain.AssemblyResolve"/> event
/// for <see cref="AppDomain.CurrentDomain"/> and loads the CefSharp.Core.Runtime.dll
/// based on <see cref="Environment.Is64BitProcess"/>.
/// This method MUST be called before you call Cef.Initialize, create your first ChromiumWebBrowser instance, basically
/// before anything CefSharp related happens. This method is part of CefSharp.dll which is an AnyCPU library and
/// doesn't have any references to the CefSharp.Core.Runtime.dll so it's safe to use.
/// </summary>
/// <param name="basePath">
/// The path containing the x64/x86 folders which contain the CefSharp/CEF resources.
/// If null then AppDomain.CurrentDomain.SetupInformation.ApplicationBase will be used as the path.
/// (</param>
public static void SubscribeAnyCpuAssemblyResolver(string basePath = null)
{
if(currentDomainAssemblyResolveHandler != null)
{
throw new Exception("UseAnyCpuAssemblyResolver has already been called, call ");
}
if(basePath == null)
{
basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
}
currentDomainAssemblyResolveHandler = (sender, args) =>
{
if (args.Name.StartsWith("CefSharp.Core.Runtime"))
{
string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
string archSpecificPath = Path.Combine(basePath,
Environment.Is64BitProcess ? "x64" : "x86",
assemblyName);
return File.Exists(archSpecificPath)
? System.Reflection.Assembly.LoadFile(archSpecificPath)
: null;
}
return null;
};
AppDomain.CurrentDomain.AssemblyResolve += currentDomainAssemblyResolveHandler;
}
/// <summary>
/// Unsubscribe from the <see cref="AppDomain.AssemblyResolve"/> event
/// for <see cref="AppDomain.CurrentDomain"/> that was added in <see cref="UseAnyCpuAssemblyResolver"/>
/// </summary>
public static void UnsubscribeAnyCpuAssemblyResolver()
{
AppDomain.CurrentDomain.AssemblyResolve -= currentDomainAssemblyResolveHandler;
currentDomainAssemblyResolveHandler = null;
}
/// <summary>
/// When using AnyCPU the architecture specific version of CefSharp.Core.Runtime.dll
/// needs to be loaded (x64/x86).
/// This method calls <see cref="Assembly.LoadFile(string)"/> to immediately load CefSharp.Core.Runtime.dll
/// based on <see cref="Environment.Is64BitProcess"/>.
/// This method MUST be called before you call Cef.Initialize, create your first ChromiumWebBrowser instance, basically
/// before anything CefSharp related happens. This method is part of CefSharp.dll which is an AnyCPU library and
/// doesn't have any references to the CefSharp.Core.Runtime.dll so it's safe to use.
/// </summary>
/// <param name="basePath">
/// The path containing the x64/x86 folders which contain the CefSharp/CEF resources.
/// If null then AppDomain.CurrentDomain.SetupInformation.ApplicationBase will be used as the path.
/// (</param>
public static void LoadCefSharpCoreRuntimeAnyCpu(string basePath = null)
{
const string assemblyName = "CefSharp.Core.Runtime.dll";
if (basePath == null)
{
basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
}
var env = Environment.Is64BitProcess ? "x64" : "x86";
string archSpecificPath = Path.Combine(basePath,
env,
assemblyName);
if (File.Exists(archSpecificPath))
{
Assembly.LoadFile(archSpecificPath);
}
else
{
throw new FileNotFoundException("Unable to load for arch " + env, archSpecificPath);
}
}
}
}
+1
View File
@@ -86,6 +86,7 @@
<Compile Include="Callback\TaskPrintToPdfCallback.cs" />
<Compile Include="Callback\TaskResolveCallback.cs" />
<Compile Include="CdmRegistration.cs" />
<Compile Include="CefRuntime.cs" />
<Compile Include="DefaultApp.cs" />
<Compile Include="DevToolsExtensions.cs" />
<Compile Include="DevTools\DevToolsClient.cs" />
+1
View File
@@ -42,6 +42,7 @@
</ItemGroup>
<ItemGroup>
<Compile Remove="CefRuntime.cs" />
<Compile Remove="DevTools\DevToolsClient.Generated.cs" />
<Compile Remove="Internals\Partial\ChromiumWebBrowser.Partial.cs" />
</ItemGroup>