MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
437 stars 51 forks source link

Deployed C# app using WebView2 cannot find the Runtime #1476

Closed kgranroth-tm closed 3 years ago

kgranroth-tm commented 3 years ago

I created a C# DLL based on .NET 4.6.1 that has a form containing an element of type Microsoft.Web.WebView2.WinForms.WebView2. It uses the latest (as of 6/21) release version of the SDK, pulled automatically by NuGet:

   <ItemGroup>
     <PackageReference Include="Microsoft.Web.WebView2">
       <Version>1.0.864.35</Version>
     </PackageReference>
   </ItemGroup>

This works like a charm when run directly from Visual Studio 17 as well as directly clicking on the executable in the Release or Debug folders.

My end users won't have any of that installed, though, so I am installing the WebView2 runtime via a WiX installer Custom Action. The app installer runs the MicrosoftEdgeWebview2Setup.exe bootstrap installer which is installing the runtime. I do see the 'Microsoft Edge WebView2 Runtime' version 91.0.864.59 installed in the Windows 'Apps & features' panel.

With all this, the installed app can no longer run the WebView2 component. It ignores the runtime entirely and when the code invokes the form containing WebView2, I see the following exception:

Could not load file or assembly 'Microsoft.Web.WebView2.WinForms, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' or one of its dependencies. The system cannot find the file specified.

The exception is clearly referencing the SDK and not the runtime. Looking at the release notes for SDK 1.0.864.35 for runtime support, I see: "Minimum Runtime version to load: 86.0.616.0 or newer". I have 91.0.864.59 installed so that should work.

I have only limited experience developing in Windows and this is my first C# app anywhere, so I am likely missing something fundamental. In my flailing around, I did see a few references to the WebView2Loader.dll. It's not clear to me if that is only applicable to C++ apps or if it also has meaning for C# -- the vast majority of sample code and question and answers as of this date still focus heavily on C++ (all the deployment docs do, for instance). On the off chance that this DLL does matter for C#, I copied it to my target folder, but that did nothing.

To sum it up, how do I deploy a C# application using a WebView2 WinForm component so that it uses the Runtime rather than a NuGet-installed SDK?

champnic commented 3 years ago

Hey @kgranroth-tm - that error message indicates that it's the Microsoft.Web.WebView2.Winforms.dll assembly that's missing when your app gets deployed. I think you are correctly identifying that this is an issue with the SDK and deployment of the app, and not the runtime - that looks like it's working fine.

What are the SDK files that are getting included in your app deployment?

For example, when I publish using ClickOnce, these are the files I see (note: Microsoft.Web.WebView2.WinForms.dll): image

kgranroth-tm commented 3 years ago

Hey @champnic - Thank you, with your suggestion and one on docs.microsoft.com, we finally hit on the full solution.

What is apparently needed to run a WebView2 app using the runtime is to install the following to the directory containing the exe:

image

I did come across the reference to WebView2Loader.dll in some doc, but it wasn't clear if it was c++ specific or not and when I added just that file to the release directory, it wasn't enough. It turns out that I need that file and I also need the three other Microsoft.Web.WebView2.*.dll files. All FOUR are necessary.

The WebView2Loader.dll apparently can be either in the same root directory as the other three DLLs or it can be in a runtimes\win-x86\native sub directory -- both work for me.

So again, thank you as this does answer my question and it now works! But, if you have access to the docs, can you update at least https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution to stress that IN ADDITION to installing the Runtime, any deployed app will ALSO need those four DLLs from the SDK? I could not find that in any doc or example in my search.

champnic commented 3 years ago

@kgranroth-tm Glad you got it working! Yes, we should definitely improve our docs here - I think we had this missing because Visual Studio typically does this automatically, but clearly folks need this info.

The only thing I would caution is that for WebView2Loader.dll, that's a native and architecture specific dll, so you'll want to test different configurations your users may be running in and/or what you are compiling for (x86 vs x64 vs AnyCPU). If you find issues you can use the runtimes path to specify different architectures of the dll (so, for example, you might ship all three x86/x64/arm64 versions of WebView2Loader.dll with your app).