Search code examples
outlookvstooutlook-addinoffice-addinswebview2

WebView2Loader.dll not found is Outlook VSTO addin


I read allot about the WebView2Loader.dll - file not found issue, but couldn't solve the problem in my case:

A simple Outlook VSTO add-in with FormRegion, Inside the FormRegion I placed the WebView2 control, and initialized it:

 private async void FormRegionWebView2_FormRegionShowing(object sender, System.EventArgs e)
        {
            await InitializeCoreWebView2Async(webView2Ctrl, @"C:\temp");
            webView2Ctrl.Source = new Uri("http://www.bing.com");
        }

 public async Task InitializeCoreWebView2Async(WebView2 wv, string webCacheDir)
        {
            CoreWebView2EnvironmentOptions options = null;
            CoreWebView2Environment webView2Environment = null;
           
            webView2Environment = await CoreWebView2Environment.CreateAsync(null, webCacheDir, options);

            await wv.EnsureCoreWebView2Async(webView2Environment);
        }

The native dlls are in the bin\Debug\runtimes folders,

But I still get 'WebView2Loader.dll': The specified module could not be found' in InitializeCoreWebView2Async()

Here is the code: https://github.com/MicrosoftEdge/WebView2Feedback/files/8117191/OutlookAddInWithWebView.zip

Any help would be very appreciated.


Solution

  • It turns out that Outlook is not able to get the WebView2Loader.dll from inside the runtimes folders, it has to be in the output directory itself.

    Specifying the Platform target (to x86) was not enough, the dll was still in runtimes only. (target framework is: .NET framework 4.7.2)

    Only migrating from nuget packages.config to PackageReference (as described here: https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference) and specifying the platform target caused the dll to be copied to the output dir (bin\Debug).

    We will have to prepare two separate ClickOnce installers, one for x86 and one for x64, but now the dll is found by Outlook and WebView2 displays web pages inside FormRegion.

    Update: Actually that wasn't enough for adding the WebView2Loader.dll to ClickOnce application_files too.
    For that I had to define the dll as content in the csproj file:
    In Visual Studio (2022) "Unload Project", then "Edit Project File" to get the .csproj of the add-in as xml, and adding the following:

      <ItemGroup>
        <Content Include="$(MSBuildThisFileDirectory)bin\$(Configuration)\runtimes\win-$(EffectivePlatform)\native\WebView2Loader.dll">
          <Link>%(Filename)%(Extension)</Link>
          <PublishState>Included</PublishState>
          <Visible>False</Visible>
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
          <Pack>false</Pack>
        </Content>
      </ItemGroup>
    

    After reloading and publishing the Loader dll was in the ClickOnce installer application files and the add-in could be distributed.