MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
450 stars 55 forks source link

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

Open CocoDico78 opened 11 months ago

CocoDico78 commented 11 months ago

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:

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

david-risney commented 9 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.

nishitha-burman commented 8 months ago

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

Thanks!

CocoDico78 commented 8 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 6 months ago

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 commented 4 months ago

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.