Open CocoDico78 opened 12 months ago
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.
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.
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