0

I'm getting 'Catastrophic failure(Exception HRESULT: 0x8000FFFF (E_UNEXPECTED))' run-time error while trying to use MS WebView2 in ASP.NET Web API in production environment (Win10, IIS v.20H2 (OS Build 19042, 1586)) - see details in the code inline.

When the same code is used in a .NET Framework console app or under VS2019/Kestrel(?) (Ctrl+F5 - Start without debugging) everything works well.

private ManualResetEvent _event;
public void StartBrowserThread()
{ 
   var th = new Thread(async () =>
   { 
     BrowserFormsLogger.Log($"th.Context = {contextDescription}");

     var webView = new Microsoft.Web.WebView2.WinForms.WebView2();
 ...
     udf = ...
     var envTask = await Microsoft.Web.WebView2.Core.CoreWebView2Environment
                           .CreateAsync(userDataFolder: udf);

     var ecwTask = webView.EnsureCoreWebView2Async(envTask);
     while (ecwTask.IsCompleted == false)
     {
       Application.DoEvents();
       //
       // Run-time error happens here in ASP.NET environment after .DoEvents called several thousand times. 
       // In a .NET Framework Console App everything works well.
       // 
       // WebView2 creation failed with exception = System.Runtime.InteropServices.COMException (0x8000FFFF): 
       //   Catastrophic failure(Exception HRESULT: 0x8000FFFF (E_UNEXPECTED))
       //    in System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       //    in Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateCoreWebView2ControllerAsync>d__58.MoveNext()
       // --- End of stack trace from previous location where exception occurred ---
       //    in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       //    in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       //    in System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
       //    in Microsoft.Web.WebView2.WinForms.WebView2.<InitCoreWebView2Async>d__18.MoveNext()
     };
     ...
     _event.Set();

     System.Windows.Forms.Application.Run();

  });


  th.SetApartmentState(ApartmentState.STA);
  th.IsBackground  = true;
  _event = new ManualResetEvent(false);
  th.Start();

...

Any clues how this issue can be solved?

ShamilS
  • 1,410
  • 2
  • 20
  • 40
  • Why don't you simply `await webView.EnsureCoreWebView2Async(envTask);` instead of `DoEvents`? – Poul Bak Apr 08 '22 at 15:18
  • What is the value of `udf`? Does it point to a user personal folder? IIS might not load user profiles. – Poul Bak Apr 08 '22 at 15:20
  • @Paul Back: it doesn't work with `await webView.EnsureCoreWebView2Async(envTask);` (even) when running under VS 2019. `udf` points to a user defined folder in `C:\Users\{{user name}}\AppData\Local\Temp\...` where [`EBWebView`](https://github.com/MicrosoftEdge/WebView2Feedback/issues/2023) folder is created. – ShamilS Apr 08 '22 at 22:53
  • Have you checked that the user profile is loaded? If not, then the app can't access that folder. – Poul Bak Apr 08 '22 at 23:07
  • @Paul Bak: Yes, I have checked, the user profile is loaded. – ShamilS Apr 09 '22 at 15:30

0 Answers0