Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Problem]: WebView2 won't work outside of a UI thread #4198

Open
CocoDico78 opened this issue Nov 28, 2023 · 7 comments
Open

[Problem]: WebView2 won't work outside of a UI thread #4198

CocoDico78 opened this issue Nov 28, 2023 · 7 comments
Assignees
Labels
feature request feature request tracked We are tracking this work internally.

Comments

@CocoDico78
Copy link

CocoDico78 commented Nov 28, 2023

What happened?

Several issues come to the same conclusions:

The WV2's API need to run in a UI thread [from #3617]

there was no UI thread in my application, causing the await to hang [from #202 ]

This causes a hard dependency on heavy Windows DLLs to use a Dispatcher, and forces the host app to run as STA (Single-Threaded Apartment), de facto excluding async main for console apps (see this closed issue).

It would be beneficial to make this dependency more explicit and less cumbersome, eg:

  • expose a way to create a CoreWebView2 without any reference to a UI framework and without creating an unused CompositionController
  • remove dependency to a IntPtr hwnd when creating the webview
  • expose a way to initialize a CoreWebView2 by passing in a Dispatcher, to make the dependency more explicit
  • remove WPF/Winforms from the NuGet package for purely headless uses
  • document the issue more clearly and document the headless mode more clearly

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

No response

SDK Version

No response

Framework

Other

Operating System

Windows 11

OS Version

No response

Repro steps

in a blank console app Program.cs:

var env = await CoreWebView2Environment.CreateAsync();
var controller = await env.CreateCoreWebView2ControllerAsync(new IntPtr(-3)); // doesn't complete
var coreWebView = controller.CoreWebView2; // never reached

Repros in Edge Browser

Not Applicable

Regression

No, this never worked

Last working version (if regression)

No response

AB#48454374

@CocoDico78 CocoDico78 added the bug Something isn't working label Nov 28, 2023
@david-risney david-risney added feature request feature request tracked We are tracking this work internally. and removed bug Something isn't working labels Jan 10, 2024
@david-risney
Copy link
Contributor

One can as you note, create a separate STA thread and use that to run the WebView2 but its not easy from the API to create a headless WebView2. I've created an internal item to track this. Thanks.

@nishitha-burman
Copy link
Collaborator

@CocoDico78 can you please details on the scenarios you would use this for?

Thanks!

@CocoDico78
Copy link
Author

CocoDico78 commented Feb 26, 2024

I am wrapping a JS API inside a .NET class library. Calls to that JS API require a Chrome extension to be running in the web browser (because it internally uses firebreath to communicate with a native app). The API doesn't have any UI so I don't need any UI either, and it can be used in console apps.

@rh185056
Copy link

I am replacing an existing C# implementation using the InternetExplorer object which navigates to a local HTML file; then uses the ExecWB method to pass in a COM OLECMDID.OLECMDID_PRINT command. With IE, this window was not visible to the end user and worked seamlessly. So, I'm now looking for an alternative not using Internet Explorer, since it's deprecated/disabled through a recent Windows update.

@RickStrahl
Copy link

RickStrahl commented Mar 27, 2024

Trying to use this for headless HTML to PDF conversion. I can get this to work with a desktop context active and using an offscreen Windows form. But it doesn't work if there's no desktop context (ie. from within IIS or a service for example).

HWND_MESSAGE (IntPtr(-3)) just hangs even if running off an STA thread. IntPtr.Zero gives invalid value.

@lucaswhob
Copy link

Trying to use this for headless HTML to PDF conversion. I can get this to work with a desktop context active and using an offscreen Windows form. But it doesn't work if there's no desktop context (ie. from within IIS or a service for example).

HWND_MESSAGE (IntPtr(-3)) just hangs even if running off an STA thread. IntPtr.Zero gives invalid value.

I Have Same Exactly Problem

@Dean-NC
Copy link

Dean-NC commented Jun 8, 2024

I ran across the blog post by @RickStrahl on this, where he does a great job as always explaining the issue. I'd also like to use WebView2 in non-interactive mode for PDF generation and other features. I'd use it in .NET hosted Windows Services. Microsoft did a great job with making .NET cross-platform by segmenting-off things that need Windows features. It would be awesome to see that same mindset here, not by making it cross-platform, but a way to use this without needing STA, Windows Forms, event loop, etc. A reliable worry-free browser engine that could be used by .NET server apps. That would give me an even stronger desire to host my web apps on Windows than I already have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request feature request tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

7 participants