-
Notifications
You must be signed in to change notification settings - Fork 54
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
Comments
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. |
@CocoDico78 can you please details on the scenarios you would use this for? Thanks! |
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. |
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. |
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 |
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. |
Hello, I have encountered a few opened issues regarding this topic, so I was interested if there are any news about this. |
It is possible but it requires a bit of work to make it work as it requires setting up a headless environment for the WebVIew to run in. You can read more about it in this blog post: Programmatic Html to PDF Generation using the WebView2 Control with .NET or you can take a look and use the https://github.com/RickStrahl/Westwind.WebView/blob/master/HtmlToPdf.md |
Thanks a lot @RickStrahl, I will look through your article. |
What happened?
Several issues come to the same conclusions:
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 excludingasync main
for console apps (see this closed issue).It would be beneficial to make this dependency more explicit and less cumbersome, eg:
CoreWebView2
without any reference to a UI framework and without creating an unusedCompositionController
IntPtr hwnd
when creating the webviewCoreWebView2
by passing in aDispatcher
, to make the dependency more explicitImportance
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
:Repros in Edge Browser
Not Applicable
Regression
No, this never worked
Last working version (if regression)
No response
AB#48454374
The text was updated successfully, but these errors were encountered: