0

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 ...").

SteveCinq
  • 1,920
  • 1
  • 17
  • 22

2 Answers2

1

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.

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • Thanks for your reply. I'm sitting here shaking my head tho; the amount of messing about to "upgrade" from WebBrowser when all we need to do is pop up an oAuth2 authorization form is doing my head in. Especially when doing this with WebBrowser was such a snap. I'll give your suggestion a go, but I have a feeling that the resulting solution is becoming more and more fragile, given that we have no control over consuming applications.. – SteveCinq Aug 01 '22 at 20:34
1

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"/>
WVV
  • 11
  • 1
  • Thanks for your answer. I am not the one who asked the question, but this answer was very helpful. It worked well! – hibara Jun 04 '23 at 07:15