2

I have downloaded and attached the FixedVersionRuntime.88.0.705.81.x64 for WebView2 and attached it to my project.

Using the following it should load the necessary page but when loading the WebView is not crashing but no page is loaded:

public async Task InitializeAsync()
{
      string installPath = @"C:\Program Files (x86)\WebView2Runtime\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x64\";
      var webView2Environment = await CoreWebView2Environment.CreateAsync(installPath);
      await browserControl.EnsureCoreWebView2Async(webView2Environment);
}

I am then setting the source after this:

await InitializeAsync();
me.Source = new Uri(((MainViewModel)this.DataContext).Config.DefaultURL);

When using the evergreen installer it worked fine but when moving to the fixed version it seems to not load correctly when deployed.

  • According to the documentation, the browserExecutableFolder should be a "The relative path to the folder that contains a custom version of WebView2 Runtime". Your path is an absolute path.:https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2environment.createasync?view=webview2-dotnet-1.0.705.50#Microsoft_Web_WebView2_Core_CoreWebView2Environment_CreateAsync_System_String_System_String_Microsoft_Web_WebView2_Core_CoreWebView2EnvironmentOptions_ Also it appears that you're using the x64 fixed version, but placing it in %ProgramFiles(x86)% – Tu deschizi eu inchid Mar 05 '21 at 22:41
  • I believe that the idea is that if you use the fixed version that the WebView2 files will be in a folder relative (at or below) the folder that contains your programs executable. – Tu deschizi eu inchid Mar 05 '21 at 22:42
  • So I changed the path to a relative path and converted back to the x86 version with no luck. Its odd as it can identify the location of the run time and does not error, but just does not seem to load the default page or any page selected. – James Michael Lucas Mar 06 '21 at 00:20
  • Where is your program installed? It can't be installed in %ProgramFiles% or %ProgramFiles(x86)% unless you specify the userDataFolder at another location. Writing to %ProgramFiles% or %ProgramFiles(x86)% requires adminstrative privileges. – Tu deschizi eu inchid Mar 06 '21 at 00:44
  • Is the `WebView2`called 'browserControl' or 'me' ? It can't be both. – Poul Bak Mar 06 '21 at 08:02
  • The answer by @user9938 is comprehensive. But please also note that the version of "WebView2Loader.dll" which is in use is very crucial. I had almost the same problem with "Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64" when I tried to use the WebView2 component in the MMC Snap-Ins with types of "HTMLView" or "FormView". – amirfg May 18 '22 at 07:35
  • I just copied the abovementioned dll file (version 1.0.1248.0, size=157640 bytes) in a proper path that was accessible for the project (you could just put it beside your project output files first to test it) and then WebView2 browser started to function as expected. Microsoft error messages sometimes (at least in my case) was a little bit misleading and did not convey enough and to the point information. – amirfg May 18 '22 at 07:35
  • I received "BadImageFormatException" that normally occurs when you mix platform targets (for example using a dll file compiled in X64 in an application that targeted for x86 or vice versa) or mix native code and .NET but that was not my problem at all. I hope this help one who may stuck in. – amirfg May 18 '22 at 07:49

2 Answers2

6

I've tested the following, which seems to work:

Download WebView2 Fixed Version

Example

Given:

  • WebView2 Fixed Version: Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86.cab
  • Project folder: C:\Projects\WpfTestFixedVersion
  • Output folder: C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug

Project compiled using:

  • Configuration: Debug
  • Platform: Any CPU (Prefer 32-bit)

Extract files from .cab

  • Open a cmd window

cmd window

C:\Users\Test\Downloads> expand Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86.cab -F:* "C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug"

Note: When using expand in the above command, the destination folder must already exist and the name must not end with '\'.

C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug

enter image description here

C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86

enter image description here

Option 1:

InitializeAsync

public async Task InitializeAsync()
{
    string installPath = @".\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86";
    var webView2Environment = await CoreWebView2Environment.CreateAsync(installPath);
    await browserControl.EnsureCoreWebView2Async(webView2Environment);
}

Option 2:

Note: This option allows one to specify the userDataFolder. If it's not specified, it uses the user's temp folder as the location for the userDataFolder.

InitializeAsync

public async Task InitializeAsync(WebView2 wv, string webCacheDir = "")
{
    CoreWebView2EnvironmentOptions options = null;
    string tempWebCacheDir = string.Empty;
    CoreWebView2Environment webView2Environment = null;

    //set value
    tempWebCacheDir = webCacheDir;

    if (String.IsNullOrEmpty(tempWebCacheDir))
    {
        //get fully-qualified path to user's temp folder
        tempWebCacheDir = System.IO.Path.GetTempPath();

        tempWebCacheDir = System.IO.Path.Combine(tempWebCacheDir, System.Guid.NewGuid().ToString("N"));
    }

    //use with WebView2 FixedVersionRuntime
    webView2Environment = await CoreWebView2Environment.CreateAsync(@".\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86", tempWebCacheDir, options);

    //webView2Environment = await CoreWebView2Environment.CreateAsync(@"C:\Program Files (x86)\Microsoft\Edge Dev\Application\90.0.810.1", tempWebCacheDir, options);
    //webView2Environment = await CoreWebView2Environment.CreateAsync(null, tempWebCacheDir, options);

    //wait for CoreWebView2 initialization
    await wv.EnsureCoreWebView2Async(webView2Environment);

}
Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • Thanks a lot for the help in this. The absolute path is OK but I moved it out of program Files (x86) and added a tempDirectory and the sites are again loading as expected. – James Michael Lucas Mar 08 '21 at 14:03
0

The answer by @user9938 is comprehensive. But please also note that the version of "WebView2Loader.dll" which is in use is very crucial. I had almost the same problem with "Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64" when I tried to use the WebView2 component in the MMC Snap-Ins with types of "HTMLView" or "FormView".

I just copied the abovementioned dll file (version 1.0.1248.0, size=157640 bytes) in a proper path that was accessible for the project (you could just put it beside your project output files first to test it) and then WebView2 browser started to function as expected. Microsoft error messages sometimes (at least in my case) was a little bit misleading and did not convey enough and to the point information.

I received "BadImageFormatException" that normally occurs when you mix platform targets (for example using a dll file compiled in X64 in an application that targeted for x86 or vice versa) or mix native code and .NET but that was not my problem at all. I hope this help one who may stuck in.

amirfg
  • 272
  • 2
  • 6
  • 21