MicrosoftEdge / MicrosoftEdge/WebView2Feedback

WebView2 initialization in headless mode not work when is invoked by windows service

Open
#3,458 7 comments 6 reactions 1 assignee View on GitHub

@LiangTheDev is already working on this.

Since May 8, 2023.

bug
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Hi,
i'm trying to use WebView2 in headless mode to export in pdf a page but when is invoked by a Windows Service doesn't work.

This is the scenario.

I have a web application named "WebAppService" in ASP.NET Core 6.0. running an html page.

var options = new WebApplicationOptions() {
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};

var builder = WebApplication.CreateBuilder(options);

builder.Services.AddControllers();

if (!Debugger.IsAttached) {
    builder.Host.UseWindowsService();
}

var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });


app.Run("http://localhost:5000");

In the HTML page there is a button and on click is called a web API that execute the console application used for the initialization of WebView2 and the export in pdf.

[Route("[controller]")]
    public class ExportWebViewController:Controller {

        [HttpGet("ExecProcess")]
        public ActionResult ExecProcess() {
            Process.Start("C:\\Sources\\WebView2Headless\\ConsoleApp1\\bin\\Debug\\net6.0-windows\\ConsoleApp1.exe");
            return Ok();
        }
    }

The executable invoked "ConsoleApp1" contains the methods to initialize WebView2 in headless mode, navigate and export in pdf.

static void CreateWebView2() {
    System.Diagnostics.Debugger.Launch();
    CoreWebView2Controller browserController;
    string WEBVIEW2_RUNTIME_FOLDER = @"C:\Sources\WebView2Headless\ConsoleApp1\bin\Debug\WebView2Runtime.110.0.1587.63.x64";
    IntPtr HWND_MESSAGE = new IntPtr(-3);
    var uiThread = new Thread(() => {

        Dispatcher.CurrentDispatcher.Invoke(async () => {
            var environment = await CoreWebView2Environment.CreateAsync(WEBVIEW2_RUNTIME_FOLDER, null, null);

            browserController = await environment.CreateCoreWebView2ControllerAsync(HWND_MESSAGE);
            browserController.CoreWebView2.NavigationCompleted += NavigationComplete;

            browserController.CoreWebView2.Navigate("https://developer.microsoft.com/it-it/microsoft-edge/webview2/");

        });

        Dispatcher.Run();
    });

    uiThread.SetApartmentState(ApartmentState.STA);
    uiThread.Start();
    uiThread.Join();

    void NavigationComplete(object sender, CoreWebView2NavigationCompletedEventArgs args) {
        browserController.CoreWebView2.PrintToPdfAsync(@"C:\Temp\ExportFromWebView2.pdf");
        Console.WriteLine("Export complete!");
    }
}


[STAThread]
static void Main(string[] args) {
    Console.WriteLine("START -> CreateWebView2 Environment...");
    CreateWebView2();
}

Main(args);

When the web application "WebAppService" is running in debug mode in Visual Studio (or invoked by a Console Application), the export works.

When the web application is invoked by a Windows Service, the export doesn't work: the "ConsoleApplication1" is executed, but it stucks at method "CreateCoreWebView2ControllerAsync" (no exceptions are returned).

I'm using WebView2Runtime.110.0.1587.63.x64

Thanks for your help!

Fabio

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.