Search code examples
winformsblazorwindows-server

Error when launching blazor +winforms app on windows server


When I install and try to launch my blazor app on windows server 2019 i get this error and i cant seem to work out whats missing for it. Its a simple blazor app that runs with winforms. i have tried installing the relevant .NET versions (7) along with enabling anything .NET related in features.

Error

this is also my .csproj file: enter image description here


Solution

  • I think you has the same problem like I had. Your server has no Edge Browser installed and therefore has no WebView2 Engine available.

    You have the following options now:

    • Install WebView2 on your server or the Edge Browser.
    • Better: make a release of your app which contains WebView2 as part of your application, maybe together with an self-contained .NET Core app. Then you don´t need any software installed on your server (no .NET SDK or Browser or WebView2, because your app brings everything with it).

    For the last bullet point, you need to add the following Code to your WinForms constructor:

    public Form()
    {
        InitializeComponent();
         
        Environment
          .SetEnvironmentVariable("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", @".\WebView2");
        Environment
          .SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", @".\WebView2UserData");
    
        ServiceCollection services = new();
    
        // From: Microsoft.Extensions.DependencyInjection
        services.AddWindowsFormsBlazorWebView();
     
        blazorWebView1.HostPage = "wwwroot/index.html";
        blazorWebView1.Services = services.BuildServiceProvider();
        blazorWebView1.RootComponents.Add<App>("#app");
    }