3

I have following code to launch webview

var env = await Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(userDataFolder: Path.Combine(Path.GetTempPath(), $"{Environment.UserName}_Browser"));
await webView21.EnsureCoreWebView2Async(env);
           
 var url = $"Field1=A2&TypeId=1&ItemId=1234";

 var postData = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(url));
 var request = webView21.CoreWebView2.Environment.CreateWebResourceRequest("https://xxxx/xxxx", "POST", postData, "Content-Type: application/x-www-form-urlencoded");
 webView21.CoreWebView2.NavigateWithWebResourceRequest(request);       

As for my understanding, providing unique userDataFolder should not create problem when multiple users using the application on a same server. It works for some but for some users throws the exception. Is there anything Im missing?

Using Microsoft.Web.WebView2 -Version 1.0.1108.44

Exception

System.Runtime.InteropServices.COMException (0x800700AA): The requested resource is in use. (Exception from HRESULT: 0x800700AA)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateCoreWebView2ControllerAsync>d__54.MoveNext()
user12073359
  • 289
  • 1
  • 11

1 Answers1

3

By looking at the exception, you included, it indicates that the same userdatafolder is used by more than one WebView2 instance. To get around that problem, I suggest you create a new folder for each instance of WebView2 you run.

To do that you can simply create a random name for the folder instead of using one per user. Something like this:

Random rnd = new Random();
string subFolder = rnd.Next().toString();
var env = await Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(userDataFolder: Path.Combine(Path.GetTempPath(), $"{Environment.UserName}", subFolder));

However, you should consider deleting that folder again, when the program shuts down (or you will get a lot of temp folders).

Poul Bak
  • 10,450
  • 5
  • 32
  • 57