Creating CefSharp browser component in a hydra plugin throws error

I’ve got a pretty simple hydra plugin developed in C#. The core of the plugin looks like this:

[Plugin, VisualPlugin, NeedsManagedWrapper(typeof(CustomPluginWrapper))]
public partial class CefSharpPlugin : RemObjects.Hydra.WPF.VisualPlugin, ICefSharpPlugin
{
    public void ProcessMessage(string str)
    {
        System.Diagnostics.Debug.WriteLine(str);
    }

    public CefSharpPlugin()
    {
        InitializeComponent();
    }

    ChromiumWebBrowser web;

    public void Startup()
    {
        web = new ChromiumWebBrowser();
    }

    public void Shutdown()
    {
        web = null;
    }
}

The xaml is just an empty hydra:VisualPlugin element and a Background color set so I can tell the plugin is loading.

I have CustomPluginWrapper all set up and working fine and I have a Delphi host app that loads the plugin and can call ProcessMessage without any problems. It gets the background color so the visual plugin loaded. I have also created visual plugins before that worked fine.

The problematic part comes when I call the Startup, which creates a new CefSharp ChromiumWebBrowser component. This causes the hydra plugin to blow up, throwing:

Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01: cefsharphostxe2.exe
Problem Signature 02: 1.0.0.0
Problem Signature 03: 55674119
Problem Signature 04: mscorlib
Problem Signature 05: 4.0.30319.18444
Problem Signature 06: 52717edc
Problem Signature 07: 3de7
Problem Signature 08: 1f
Problem Signature 09: System.ArgumentException
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

This happens in both a Delphi 2007 and Xe2 host app. All my testing is on Win32, btw.

I’ve used CefSharp’s ChromiumWebBrowser just fine outside of Hydra. I wonder what it is that could be causing Hydra problems.

FYI, in startup, I can do:

CefSettings a = new CefSettings();
CefSharp.Wpf.RenderingEventArgs b = new CefSharp.Wpf.RenderingEventArgs(null);

and those go off without an error, and I can see in procexp that the exe is loading all the same CefSharp.dll, CefSharp.Core.dll and CefSharp.Wpf.dll that it should be.

It’s just creating the browser object that causes it to blow.

Unfortunately, I think this might be a limitation of CefSharp. I don’t THINK RemObjects can do anything about it:

But would be happy to hear if there is something that could be done in my code to work around it.

I don’t think it’s app domains, we run hydra in the main app domain afaik; I suggest you open your Delphi .exe as a host for your plugin, run it and break on ArgumentException, see what the exception says.

Could you put the

code line into trycatch and see where exactly the problem arises? F.e write down the exception message and stacktrace to a text file.

Well, the exception is:

System.ArgumentException occurred
Message: A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Cannot pass a GCHandle across AppDomains.

Googling that is how I managed to get to that report about it not working as a plugin. I’m not much of an expert of .NET stuff (or CEF, for that matter).

The call stack is:

mscorlib.dll!System.Runtime.InteropServices.GCHandle.FromIntPtr(System.IntPtr value)
mscorlib.dll!System.Runtime.InteropServices.GCHandle.explicit operator System.Runtime.InteropServices.GCHandle(System.IntPtr value)
CefSharp.Core.dll!<Module>.gcroot<System::Action ^>..P$AAVAction@System@@()
CefSharp.Core.dll!<Module>.CefSharp.CefSharpApp.OnContextInitialized()

And here’s what shows up in the event log:

Application: CefSharpHostXe2.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentException
Stack:
   at System.Runtime.InteropServices.GCHandle.InternalCheckDomain(IntPtr)
   at System.Runtime.InteropServices.GCHandle.FromIntPtr(IntPtr)
   at <Module>.gcroot<System::Action ^>..P$AAVAction@System@@(gcroot<System::Action ^>*)
   at <Module>.CefSharp.CefSharpApp.OnContextInitialized(CefSharp.CefSharpApp*)

Hello

We’ll try to expose the default AppDomain of the plugin host process. This might help to avoid the cross-domain calls in the plugin internals

Regards

Thanks, it would be great if we could make it work. If you like, I could package you up some proof of concept code so you could test the changes.

Any idea of timeline of such a change? Are we talking likely “soon-ish” or “not anytime soon”?

pls create a simple testcase that reproduces this issue.

I have one. Do you want me to zip it and post it somewhere or mail it? Don’t see any way to attach on this forum. I could also just post the MainWindow.xaml.cs for the plugin and the form source for the Delphi app.

you can attach it here or send directly to support@

for attaching here, use

CefSharpPluginTest.zip (97.8 KB)

Ah, there it is! I flew over those icons but somehow missed it.

I’ve included both the Delphi Xe2 host and the CefSharp plugin. You’ll need to add the nuget package for CefSharp (the current version). I’m knew to the C# thing, so I’m not sure if I should include the actual packages dir or not. I left it off because you can just add them. If you need me to do it different, just explain in small words.

Thanks!

I’ve launched your testcase and saw colored panel after host was started. should plugin do something else?
pressing startup button doesn’t change situation

Note: I’m Delphi guy only.

Press the StartupBtn.

I’ve exposed default AppDomain and am trying to test it with your sample…

already did. also I’ve updated your sample as

        public void Startup()
        {
                web = new ChromiumWebBrowser();
                web.Load("http://www.remobjects.com");
 
        }

but nothing happened: no errors in delphi and no errors in VS :frowning:

can you create plugin that works with .NET host as expected, pls ?

What version of Delphi are you testing it against? I’ve tested against Delphi 2007 and Xe2 (that’s what we have licenses for).

Also, what do you mean “can you create plugin that works with .NET host as expected, pls ?” Are you asking me to create a .NET HOST instead of a Delphi host? Is there actually any page or help file to explain how to do that? I’m trying and struggling. The instructions for creating a Delphi host are pretty clear but there’s several things that are tripping me up in building a .NET one.

Also, what does this mean? Is that something I am supposed to do? I have to be honest and say I have no idea if you’re talking about Delphi, .NET, or what. It’s all beyond my current understanding. :smiley:

Hello

In short words (and with very less details):
Surely term ‘default AppDomain’ sounds very strange when applied to a Win32 Delphi app.
When .NET runtime gets loaded into a Delphi app (to later load the .NET plugins) this .NET runtime creates a default Application Domain. But by default all .NET plugins are (were) loaded into a separate specially created AppDomain. This separate AppDomain is used because plugins loaded into the default AppDomain cannot be unloaded (this is .NET restriction, not Hydra’s one).
Unability to unload .NET plugins vs using the default AppDomain is a very significant tradeoff.So it will be (probably) controlled by a separate host option.

Regards

Drop email to support@ .
we will send fix to you