WebView2 #xpa #dotnet


Frederik Soete
 

Hi, Adrian,

Isn't edge canary for experimental features? So do you install that on a non-developer computer? Or what do you do install there? The W11 appears to already have the Evergreen channel edge. (At least so far as we can tell, beginners that we are...)

Strange thing is: the version number IS detected on the W11. Both via registry and via SDK function. So you'd expect everything to work there, but alas!

We plan to have another go on another W11 computer, to see if that gives us a different outcome.

Anyhow, thx

FS

Op wo 25 jan. 2023 19:14 schreef Adrian Wick <adrian.wick2015@...>:

Hi Frederik,

did you install edge canary (https://www.microsoftedgeinsider.com/en-us/download) on windows 11 where you are running the app for the user logged into the windows?

Regards,
A


Frederik Soete
 

Hi everybody,

I've got some more info now:
- Test app works flawlessly on W10, but not on W11.
- Test app also does not work on a W2022 terminal server.
- Edge WebView2 runtime is recent on every computer inspected.
- WebView2 SDK version we used is "Microsoft.Web.WebView2.1.0.1518.46".

Weird. I must be overlooking something crucial. I should not have to install an Edge Canary in a production environment, I think.

Bye,

FS


Frederik Soete
 

Hi everybody,

I have figured out my issue.

Initializing the browser will fail if our "user data folder location" is not writable.

By default the browser will try to put this location inside the Magic folder, but on our W11 computer this folder was not writable.

Solution in C# code (to put before the update of the source property of the brws object):

        // Initializing the browser will fail if our user data folder location (app folder) is not writable.
        // Prepare the browser environment by creating a custom app folder in the user's roaming profile directory.
        // If all goes well, brws.CoreWebView2 will not be null any more at end of this code segment (= browser has been init'ed).
        if (brws.CoreWebView2 == null) {
           
            // Get location of special folder (user's roaming profile directory).
            string app_data_roaming_folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            //System.Windows.Forms.MessageBox.Show(app_data_roaming_folder);
           
            // Append custom string to special folder location (subdirectory "MY_APP_FOLDER")
            string custom_app_folder = System.IO.Path.Combine(app_data_roaming_folder, "MY_APP_FOLDER");
            //System.Windows.Forms.MessageBox.Show("Wanted user data folder: " + custom_app_folder);
           
            // If necessary, create our custom app's folder. Ignore errors.
            try {
                System.IO.Directory.CreateDirectory(custom_app_folder);
            } catch {}
    
            // We create a WebView2 Environment using the installed runtime, and using our custom app's folder.
            var T1 = Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(null, custom_app_folder);
            // Wait for the result of this call.
            var env = T1.Result;
           
            // Initialize browser with this environment.
            var T2 = brws.EnsureCoreWebView2Async(env);
           
        }

        // Set Source property (in theory, this could also initialize the browser, but we have already done that).
        var uri = new Uri(new_url);
        brws.Source = uri;


Frederik Soete
 

Hi,

And the really nice part of this quest is discovering Microsoft has a "unique" way of telling us the app folder cannot be written. The error log just spits out a hair-raising message about a NullReferenceException.

Thanks for listening and sympathizing,

Frederik Soete


Frederik Soete
 

Hi group,

FYI. One little icky bug with my code: at some times the browser is not init'ed fast enough, so setting the Source property fails with a null reference exception. In order to solve that, I've recuperated the "CustomWait" function from Craig Martin's WebView2 project. I don't really understand its hocus pocus completely, but it seems to function as a good enough "await" alternative. So hats off to Craig.

Bye,

FS