WebBrowser claims "object type is not visible to COM" on ObjectForScripting

Viewed 3730

I'm trying to set up an interop between C# and JavaScript using the WPF WebBrowser control. The C#->JavaScript calls work great so far, but I can't get the JavaScript->C# operational.

I've created a class for the object:

[ComVisible(true)]
class BrowserClient
{
    private MainWindow owner;

    public string id = "browser-client";

    public BrowserClient(MainWindow owner)
    {
        this.owner = owner;
    }

    public void sendMessage(string date)
    {
        owner.OnReceiveMessage(date);
    }
}

Note that the ComVisible attribute.

But when I set the ObjectForScripting property in the Window_Loaded event:

webBrowser.ObjectForScripting = new BrowserClient(this);

I get the following (very confusing) exception:

An unhandled exception of type 'System.ArgumentException' occurred in PresentationFramework.dll

Additional information: The object type is not visible to COM. You need to set ComVisibleAttribute attribute to True.

Needless to say, I'm pretty baffled. What is happening here?

Update: It might be worth mentioning that I'm using .NET 4.0 on Windows 8.1 with Visual Studio 2013 Express for Desktop.

1 Answers
Related