Embedding WebView2 Runtimes in .NET Framework Class Library With Fody Weavers

745 Views Asked by At

We have a class library (consumable by third-party apps not under our control, and deployed as a single file dll with embedded references) that previously used the WebBrowser control. That was a very simple, easy-to-use and -deploy control that is now past its prime. But trying to replace it with WebView2 has been quite a struggle. Now we have it working but we can't get the runtimes to be included in the deployed dll. (We would have settled for the WebView control but that has issues running under Admin rights, bizarrely.)

The library is .NET 4.6.2 (for as much backward compatibility in the .NET Framework as possible). We use Fody Weavers to include RestSharp, NLog, etc and have now added Microsoft.Web.WebView2.Core, Microsoft.Web.WebView2.WinForms and Microsoft.Web.WebView2.Wpf.

We also have the Costura directive UseRuntimeReferencePaths='true' which I believe is required for .NET Framework projects.

But when I run the app test form - which consumes the library - I get a path error:

The path is not of a legal form.
at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
at System.IO.Path.InternalGetDirectoryName(String path)
at Microsoft.Web.WebView2.Core.CoreWebView2Environment.LoadWebView2LoaderDll()
at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateAsync>d__3.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.Web.WebView2.WinForms.WebView2.<InitCoreWebView2Async>d__23.MoveNext()

I've tried a few things to ensure that the runtimes are included such as embedding them as resources, to no avail.

This is a deal breaker for us if we can't get this working; we MUST have everything included in a single file library/dll. So my question is, has anyone managed to do this? (Praying that the answer is, "Sure! Just do this ...").

2

There are 2 best solutions below

1
Dark Falcon On

WebView2 makes use of native code loaded with the Win32 function LoadLibrary. To my knowledge, the native DLL has to be on the filesystem. There is no way to load it from a resource.

The exception is being thrown because the managed code is attempting to build the path to the native code DLL:

 string directoryName = Path.GetDirectoryName(typeof(CoreWebView2Environment).Assembly.Location);

The only possible workaround I can see would be to store the required DLL in a resource, write it out to a temporary folder, and set CoreWebView2Environment.LoaderDllFolderPath to that folder. This will bypass the path building logic above.

1
WVV On

WebView2Loader, Microsoft.Web.WebView2.Core are unmanaged assemblies.

You may create a folder named "Costura32" or "Costura64" in your project and add WebView2Loader.dll, Microsoft.Web.WebView2.Core.dll with "Build Action" set as "Embedded Resource".
Then, assuming the dlls are 32-bit, put the following setting into FodyWeavers.xml:

<Costura Unmanaged32Assemblies="Microsoft.Web.WebView2.Core|WebView2Loader"/>