Problems with WebView2 distribution

Plan_OK

Member
Joined
Aug 21, 2023
Messages
24
Programming Experience
1-3
Visual Studio 2022 Community Edition
Development tools for .NET Framework 4.7.2
Visual Basic

Hello,
I have been looking for similar problems with little result,
I have inserted a webview2 control that works fine with the Visual Studio interface, when I create the deployment program the control is no longer visible and therefore neither is the web link (even on the same computer).
I added (manually the webview2loader.dll library, but not webview2loaderruntime.dll because I can't find it anywhere on the whole computer.
I think the problem lies here or is something else still missing? (what do you think?). But how to locate the library location or is it necessary to use a different approach?
Thank you.
 
I created the program with Windows Visual Studio Project Installer and have no error messages in response to the creation.
Is it normal that the control works correctly under Visual Studio and, on the same computer, the executable no longer works?
 
I tried uninstalling and reinstalling from nuGet Package Manager the WebView2 control and Runtimes and reinstalled everything again (I installed the runtimes for all processors: Arm 64, X64 and X86: is that correct? Or do I only need the one for my processor?).
Things have not changed since the program distribution, but now I get in compilation numerous warning messages:
WARNING: Two or more objects have the same target location ('[targetdir]\microsoft.web.webview2.winforms.dll')
and the like; the messages also affect the naudio control.
However, the SetUp compilation terminates with:
========== Compilation: 1 completed(s), 0 failed(s), 1 updated(s), 0 ignored(s) ==========
========== Compilation started at 08:59 and took 07.827 seconds ==========
Are these warning messages important to the formation of the installation file?
Thank you for your attention, I hope for your help.
 
It is very likely you haven't set a data folder for WebView2, then it will try to create one in application folder.
A Setup project will install to Program Files or Program Files (x86) and that is not a writable location at runtime so browser will not initialize.
Here's an example initializing to app data folder:
VB.NET:
Public Async Function InitializeWebViewEnvironment(browser As Microsoft.Web.WebView2.WinForms.WebView2) As Task
    Dim userDataFolder = Path.Combine(Application.LocalUserAppDataPath, "WebView2")
    Dim env = Await CoreWebView2Environment.CreateAsync(Nothing, userDataFolder, Nothing)
    Await browser.EnsureCoreWebView2Async(env)
End Function
So you will call for example Await InitializeWebViewEnvironment(MyWebView2) before first naviation.

The only additional file that need to be added in output folder except for primary output (all managed dlls will follow automatically) is the native WebView2Loader.dll (x86 or x64 as appropriate).
 
Thank you for your response.
I understand the problem and the intervention from a theoretical point of view, but although I have tried to learn more about the topic from the links you sent me, I have not been able to organize operationally.
In particular to understand what it means to call the procedure "Initialize" before the first navigation and what to do in case of subsequent changes to the rest of the code.

If I understand you correctly, the procedure creates (on the user's computer, I believe during installation) a user folder with write permissions, allowing interaction with webview2 data, but I don't understand what it means and how to execute it before the first navigation.

Are you kind enough to use some of your time for this?
Thank you.
 
It's about initializing the WebView2 browser control instance, not about installing the app.

If you just have a WebView2 control on a form added in designer then you can initialize the environment in forms Load event handler, before any call that may cause a navigation to a page.
If you create WebView2 controls dynamically at runtime then initialize them as they are created.
 
Okay, thank you for the new suggestion and explanation. If I may still take advantage of your courtesy:
I had tried to put the initialization call in the load event of the form, but I got an error reported in Quick Actions, so I thought it was wrong.
I have two WebView2 controls in two different forms created statically: I repeated the function declaration in each of the forms and the call in the two form loads (is right?).
The system message, in both forms, is:
Error BC37058 'Await' can only be used inside an asynchronous method. Try marking the method with the 'Async' modifier and set the returned type to 'Task'.

erroreAsync.jpg


It is suggested to make the form or sub function asynchronous. I tried the two modes: it does not resolve.
I don't know if this is important, but I noticed one item that might be helpful in identifying the problem.
1 - Debug output:
[0910/142531.759:ERROR:check.cc(315)] Check failed: false. NOTREACHED log messages are omitted in official builds. Sorry!
The program '[2576] "Our Business.exe"' is terminated with the code 4294967295 (0xffffff).

If it does not take up too much of your time, I would be happy if you would try to find a solution to my problem. Many thanks, Vic.
 
Yes, you must make the event handler Async Sub in order to use the Await call.
 
Thank you for your intervention, although very quick and I am not at all clear on how to continue. If the requests seem excessive, you tell me: I will immediately stop asking further questions.
If I can I will continue to describe the problems I have encountered, and if you are so kind, I will read your answers or those of those who wish to intervene. Thank you.

I continued to make attempts, since I do not possess the specific tools to adequately address the problem (I would not have asked for help).
Upon highlighting the problem, I accepted the system's suggestions in the Quick Actions:
- make the function asynchronous.
This, however, highlighted a new problem:
BC30451 'MyWebView2' is not declared.
There are many options proposed to deal with this situation. Which one should I choose?

- Do I choose the first one? MyWebView2 method, done.
Many problems on restart: layout has changed and it seems that variables and arrays used are altered. Hard to look for the problem (if you don't know what to look for). I abandon the attempt.


- I'll try the second way: Make the sub function asynchronous.
as above: BC30451 'MyWebView2' is not declared.
Proposed second option: Generate the variable MyWebView2. Public Property MyWebView2 As WebView2.
Exception generated in function Async:
Await browser.EnsureCoreWebView2Async(env).
System.NullReferenceException: 'Object reference not set to an object instance.'
System.NullReferenceException: 'Reference to an object not set to an object instance.'

I think it's time to give up and ask for help.
 
"MyWebView2" is a sample name for the WebView2 control, substitute it with your actual name for the control. The intention is to initialize the environment for your browser control.
 
Thank you for your umpteenth response, but I still have several problems.
Sorry for the previous misunderstanding, but I don't think in English so sometimes I miss a detail in the translation.

I did some testing: the system works fine on a new project, in fact I made a simple WebView2 test with properly working deployment app.

But I couldn't get my existing project working because, as I told you the previous time, as soon as I made the form_load asynchronous, some elements of the graphics are stavolted and variables are no longer recognized (I don't know if all of them, but nothing can be used anymore).

Is there anything I can do to fix the problem or is it mandatory to start with a form_load initially made asynchronous?
Thanks, Vic.
 
The project code exceeds 4000 lines: it consists of 5 modules plus a Globals module.
I would like to insert two WebView2 controls: one in a small module that would do only the management of live TV with the connection to a TV-Web server and the other in the main module that has about 3000 lines of code.
I think it is impossible to send it all for various reasons.
At the limit, I can send the Form load and the class name and variable declarations, or you tell me what would be most useful to have.
But I think analyzing the code is also a tough job because I often wrote it in my own way with little care for conventions.
(I come from Visual Basic 3.0; I gave up programming for many years, attracted by something else, but now I have resumed with an average project, having to contend with the enormous evolution the language has undergone: I hope you understand me).
If you provide me the way I will also send you, privately, a link with the presentation of the project, which is practically finished (all that is missing is the possibility of building the installer (not before this time tomorrow).
Let me know what I need to do.
 
I didn't ask for your whole project. Show us the code relevant to your specific issue and ONLY that code. If you need to create a new test project to isolate that functionality then that's what you do.
 
Back
Top