Use of WebView2 in Studio plugins

Dear developers,

I was wondering if some of you have experience with the WebView2 component in Studio plugins.

I followed the instructions here: Get started with WebView2 in WinForms apps - Microsoft Edge Development | Microsoft Docs and can create a Windows Form app which displays a webpage using webview2. I tried to do the same in a Studio plugin but I am not successful. I have added references to the core and winform dlls of webview2 in the plugin manifest. When running the plugin, the winform window opens but the browser is not displayed (empty winform). Any idea about what is missing?

I first initiated the discussion in another post (see here) and Paul Filkin told me about the cefsharp component used in the Report Viewer Plus. I will have a look at cefsharp, as it sounds interesting.

Thank you for your support.

Laurent

  • Dear community members,

    UPDATE: I made an attempt in CefSharp and could create a working WinForms application. However, as for WebView2, I failed to package CefSharp in a plugin.

    Some guidance on how to use WebView2, CefSharp or any other web browser integration which is more modern than the Webbrowser control implementing IE would be welcome.

    My first impression is that packaging a plugin with CefSharp would make the plugin quite heavy (several megabytes).

    Thanks in advance for your support.

    Regards,

    Laurent

  • Hi ,

    We successfully implemented the CefSharp chromium browser with the project Reports Viewer Plus.  Make reference to the plugin manifest to understand what is required to be packaged with the plugin.

    https://github.com/RWS/Sdl-Community/blob/master/Reports.Viewer/Reports.Viewer/pluginpackage.manifest.xml

    Patrick Andrew Hartnett | Developer Experience | Team Lead | RWS Group

  • Dear ,

    Thank you for your prompt reply. I already had a look at the Report Viewer in Github and still did not manage to make CefSharp work in my plugin. I was going through all steps again to describe them here when I noticed that the package for WPF (used in Report Viewer) is slightly different from the one for Winforms that I want to use. After noticing this and combining it with other information found here and here, I finally managed to use CefSharp as browser in my plugin.

    Here are the steps I followed, in case it is of any use for other community members:

    1. Add CefSharp.WinForms (version 96.0.170) NuGet package to the project
    2. Change pluginmanifest and included the following references (this is based on what is used in Reports Viewer + other references which were missing when running the plugin for the first time). It might be that all references are not required:

    <Include>
    	<File>CefSharp.WinForms.dll</File>
    	<File>CefSharp.Core.dll</File>
    
    	<File>x86\CefSharp.dll</File>
    	<File>x86\CefSharp.Core.Runtime.dll</File>
    	<File>x86\CefSharp.BrowserSubprocess.Core.dll</File>
    	<File>x86\CefSharp.BrowserSubprocess.exe</File>
    
    	<File>x86\chrome_elf.dll</File>
    	<File>x86\libcef.dll</File>
    	<File>x86\icudtl.dat</File>
    	<File>x86\libEGL.dll</File>
    	<File>x86\libGLESv2.dll</File>
    
    	<File>x86\chrome_100_percent.pak</File>
    	<File>x86\chrome_200_percent.pak</File>
    
    	<File>x86\snapshot_blob.bin</File>
    	<File>x86\v8_context_snapshot.bin</File>
    
    	<File>x86\resources.pak</File>
    	<File>x86\d3dcompiler_47.dll</File>
    	<File>x86\CefSharp.Core.dll</File>
    	<File>x86\locales\en-US.pak</File>
    </Include>

    3. Add <CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport> to the first <PropertyGroup> in the csproj file
    4. Add the following code to the form which should display the browser:

    namespace CefSharpPlugin
    {
        public partial class TestForm : Form
        {
            public ChromiumWebBrowser _chromiumBrowser;
    
            public TestForm()
            {
                CefRuntime.SubscribeAnyCpuAssemblyResolver();
    
                InitializeComponent();
    
                InitializeCefSharp();
            }
    
            [MethodImpl(MethodImplOptions.NoInlining)]
            private void InitializeCefSharp()
            {
                var settings = new CefSettings();
                settings.BrowserSubprocessPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"x86\CefSharp.BrowserSubprocess.exe");
    
                Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
    
                _chromiumBrowser = new ChromiumWebBrowser(WebSiteURLasString);
                this.Controls.Add(_chromiumBrowser);
                _chromiumBrowser.Dock = DockStyle.Fill;
            }
    
            private void TestForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                Cef.Shutdown();
            }
        }
    }

    Subscribe to any CPU should be done before any component is initialized. Important is also to set the Browser Suprocess Path. I set the performDependencyCheck to false because it raised an error in debugging.

    I don't know if it is the best way to do it but at least it worked for me.

    Nevertheless, I am still not 100% convinced by CefSharp. My plugin size is over a 130MB and accessing DOM objects does not look straightforward. If anyone has experience with Webview2, which implementation and use seems closer to the one of the old Webbrowser control, I am still interested.

    Thanks.

    Laurent

  • Hi Laurent,

    I have struggled a bit with this as well. One thing that took me a while to find out is that you need to include WebView2Loader.dll in the package. Could this be what you are missing?

    Mikael

  • HI Mikael,

    My plugin package does not include the WebView2Loader.dll. And as you can see from the message above, it works without it.

    Laurent

  • Hi Laurent,

    Sorry, I somehow got the impression that you only managed to get CefSharp to work but not WebView2. If you indeed got WebView2 to work in your plugin without WebView2Loader.dll I guess all is good.

    But fact remains, my plugin where I use WebView2 absolutely does not work without including WebView2Loader.dll in the package and perhaps it can be of help for someone else trying to use WebView2 in a Studio plugin. 

    Mikael

  • Dear Mikael,

    My bad, you are right. I was too quick when reading your message.

    I had tried with the WebView2Loader.dll but it did not work for me. If you managed to make WebView2 work, I am really interested. As mentioned above, I am not 100% convinced by CefSharp. Moreover, it is really heavy!

    Regards, Laurent

  • Hi Laurent,

    As far as I can recall, once I had a working Windows Forms application with WebView2 there were only two things I had to do before I got it working in the Studio plugin:

    - Include WebView2Loader.dll

    - Provide a userDataFolder with read/write access in the call to CoreWebView2Environment.CreateAsync(). The default data folder, if I understand it correctly, will be the Studio installation folder to which the plugin does not have write access. 

    Mikael 

  • Dear Mikael,

    Thank you, I have a closer look when I have some time. However, if the Studio installation needs to have read-write access, I will not be able to change this.

    Regards,

    Laurent

  • Thanks to , I have managed to use WebView2 in a Studio plugin. The error I made was that I dropped the WebView2 control from the Visual Studio toolbox. The control was trying to create the user data folder in the default location, probably in the installation folder of Studio, which cannot be written. The solution is to add the webView2 control when initializing the form. This enables to define the location of the user data folder.

    See here for an example: github.com/.../297