Home > Mobile >  WebView2 works in debug but not when published with ClickOnce
WebView2 works in debug but not when published with ClickOnce

Time:03-02

I have published a C# WPF MVVM(Caliburn Micro) application locally and am having the following Event Viewer message when I try to display a web page using WebView2. The web page shows fine in debug.

Description: The process was terminated due to an unhandled exception. Exception Info: System.DllNotFoundException: Dll was not found. at Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateCoreWebView2EnvironmentWithOptions(String browserExecutableFolder, String userDataFolder, ICoreWebView2EnvironmentOptions options, ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler environment_created_handler) at Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(String browserExecutableFolder, String userDataFolder, CoreWebView2EnvironmentOptions options) at IGPC2WPF.Views.BrowserView.InitializeAsync()

I see it relates to userDataFolder and have tried some solutions to this from stack overflow but still no luck.

What I don't understand is an EBWebView folder seems to be created which I think is the userData folder so why is it erroring is there some permissions issue?

WebView2Loader.dll is in the publish directory

Location of EBWebView is: C:\Users*****\AppData\Local\Temp\IGPC2WPF\EBWebView

Here is my xaml and code behind for the file that displays the website.

XAML View

<UserControl x:Class="IGPC2WPF.Views.BrowserView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:IGPC2WPF.Views"             
         xmlns:cal="http://caliburnmicro.com"
         xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
         mc:Ignorable="d" Background="#3b3b3b"
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>      
    <wv2:WebView2 x:Name="WebView" Source="{Binding PdfAddress, Mode=TwoWay}" />
</Grid>

ViewModel

private string _address;
private readonly IEventAggregator _events;
private readonly IConfiguration _config;

public string Address
{
    get { return _address; }
    set { _address = value; NotifyOfPropertyChange(() => Address); }
}

public BrowserViewModel(IEventAggregator events, IConfiguration config)
{
    _events = events;
    _config = config;

    _address = _config.GetValue<string>("MiscURLs:WorkToListReport");
}

public void Close()
{
    _events.PublishOnUIThreadAsync(new GenerateExcelDataEvent());
    TryCloseAsync();
}

Code Behind:

public partial class BrowserView : UserControl
{
    public WebPageView()
    {
        InitializeComponent();
        InitializeAsync();
    }

    async void InitializeAsync()
    {
        string userDataFolder = Path.Combine(Path.GetTempPath(), "IGPC2WPF");
        WebView.CreationProperties = new()
        {
            UserDataFolder = userDataFolder
        };

        await WebView.EnsureCoreWebView2Async(null);

    }
}

EDIT OK it appears the issue is when I run setup.exe in the publish directory the WebView2Loader.dll that is present is not being copied to the installation directory. If I manually move it to the installed location the web pages display fine.

How do I make sure this dll is copied over in ClickOnce?

CodePudding user response:

I found a similar issue which seems to have fixed the problem of the WebView2Loader.dll not being installed to the root final installation folder.

In the .csproj file I added:

<PropertyGroup>
    <WebView2LoaderPreference>Static</WebView2LoaderPreference>
</PropertyGroup>

<ItemGroup>
    <PublishFile Include="runtimes\win-x64\native\WebView2Loader.dll">
        <Visible>False</Visible>
        <Group>
        </Group>
        <TargetPath>WebView2Loader.dll</TargetPath>
        <PublishState>Include</PublishState>
        <IncludeHash>True</IncludeHash>
        <FileType>File</FileType>
    </PublishFile>
</ItemGroup>

This now copies the required file into root and the application webview pages work.

  • Related