WebView2 throws System.Runtime.InteropServices.COMException: 'The requested resource is in use. (0x800700AA)'

WebView2 throws System.Runtime.InteropServices.COMException: 'The requested resource is in use. (0x800700AA)'

christev7HTEL
Explorer Explorer
531 Views
2 Replies
Message 1 of 3

WebView2 throws System.Runtime.InteropServices.COMException: 'The requested resource is in use. (0x800700AA)'

christev7HTEL
Explorer
Explorer

Just wanted to post some info on a _bug_ I've come across + fix. Maybe no one will run into this problem, but it took me a while to get to the bottom of.

 

It started with trying to use Auth0 in a Revit application, but the default implementation throws the exception and Revit crashes:

System.Runtime.InteropServices.COMException: 'The requested resource is in use. (0x800700AA)'

Now it is possible to fix this by instantiating your client with a `WebBrowserBrowser`:

IBrowser browser = new WebBrowserBrowser();

client = new Auth0Client(new Auth0ClientOptions
{
Domain = domain,
ClientId = clientId,
RedirectUri = "http://localhost:3003",
PostLogoutRedirectUri = "http://localhost:3003",
Browser = browser
});

 

or even creating an implementation to try to use the default `SystemBrowser`

but I still wanted WebView2 here. And it turns out the prickly bit of code boils down to the default environment that is created. Generally, when you are generating a WebView2 within an application, you should call the following code:

string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
System.IO.Directory.CreateDirectory(appDataFolder);
var env = await CoreWebView2Environment.CreateAsync(null, appDataFolderACAD);
await webView.EnsureCoreWebView2Async(env);


It's important to create the environment or else the default implementation will register a data folder running out of a secured folder that can't write like Program Files..

And this was what happened to me. WebView2 in Revit was trying to create a directory at:
`C:\Program Files\Autodesk\Revit 2025\Revit.exe.WebView2\`

 

which aside from being an absolute eye-sore, requires admin privileges to write to, and thus the resource cannot be used.

 

As someone new to WebView2 this really tripped me up, so here's to hoping it will help someone else at some point too.

 

So I created a custom `UserEnvironmentWebViewBrowser` where the `UserDataFolder` can be set, and defaults to appdata. (This is by and large identical to the `WebViewBrowser` implementation, with the addition of the aforementioned):

public class UserEnvironmentWebViewBrowser : IBrowser
{
private readonly Func<Window> _windowFactory;

private readonly bool _shouldCloseWindow;

public UserEnvironmentWebViewBrowser(Func<Window> windowFactory, bool shouldCloseWindow = true)
{
_windowFactory = windowFactory;
_shouldCloseWindow = shouldCloseWindow;
}

public UserEnvironmentWebViewBrowser(string title = "Authenticating...", string? userDataFolder = null, int width = 1024, int height = 768)
: this(() => new Window
{
Name = "WebAuthentication",
Title = title,
Width = width,
Height = height
})
{
if (userDataFolder != null && Directory.Exists(userDataFolder))
{
UserDataFolder = userDataFolder;
}
}

public string UserDataFolder { get; set; } = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
TaskCompletionSource<BrowserResult> tcs = new TaskCompletionSource<BrowserResult>();
Window window = _windowFactory();
WebView2 webView = new WebView2();
window.Content = webView;
webView.NavigationStarting += delegate (object? sender, CoreWebView2NavigationStartingEventArgs e)
{
if (e.Uri.StartsWith(options.EndUrl))
{
tcs.SetResult(new BrowserResult
{
ResultType = BrowserResultType.Success,
Response = e.Uri.ToString()
});
if (_shouldCloseWindow)
{
window.Close();
}
else
{
window.Content = null;
}
}
};
window.Closing += delegate
{
webView.Dispose();
if (!tcs.Task.IsCompleted)
{
tcs.SetResult(new BrowserResult
{
ResultType = BrowserResultType.UserCancel
});
}
};
window.Show();

var webView2Environment = await CoreWebView2Environment.CreateAsync(null, UserDataFolder);
await webView.EnsureCoreWebView2Async(webView2Environment);
webView.CoreWebView2.Navigate(options.StartUrl);
return await tcs.Task;
}
}

 

You can also check out the solution at my github.

 

shoutout to @grahamcook as I found the answer within his post.

0 Likes
532 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni

Thank you for sharing this interesting challenge and solution. I added it to the blog and hope it helps others as well:

  

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

tnx2u2
Explorer
Explorer
@christev7HTEL Thank you for sharing your solution. It saved my time 🙂
userDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MonacoEditorTest");