2

I created ComVisible .Net (C#) dll which holds service reference. When trying to accessing the dll from external COM client (VBScript) exception is thrown whenever I create the object.

The thrown exception is InvalidOperationException (0x80131509).

After short investigation I noticed it fails on the creation of the service reference object ("new"ing it).

  1. The service reference object used name in the code below is ServiceClient
  2. ServiceClient is private for the C# dll
  3. Trying to create the reference in a constructor also fails
  4. Removing the "new" keyword from both the class or the constructor makes the code pass,
  5. The service is up and running

The dll code:

namespace UIIdentifier.Updater
{
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    public class Client
    {

        [ComVisible(false)]
            //<<--This throws the exception  -->>
        private ServiceClient uiSpySrv = new ServiceClient(); 

        [ComVisible(true)]
        public string hello()
        {
            return "hello";
        }
    }
}

The client code:

Dim oUpdater
Set oUpdater = CreateObject("UIIdentifier.Updater.Client")

MsgBox oUpdater.hello

Any suggestions why this happens?

Roi Shabtai
  • 2,981
  • 2
  • 31
  • 47

2 Answers2

1

The most likely cause is WCF failing to load the service configuration from the app.config file.

When your library is loaded as a COM object no app.config file exists.

Your best bet is to create your WCF client in code instead of using the config file. It's quite simple. You can see how it's done here: WCF Configuration without a config file

Running your example code in the debugger with a WCF service configured in the app.config file generated the following exception which I think is pretty self explanatory.

System.InvalidOperationException occurred
Could not find default endpoint element that references contract 'ServiceReference1.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

Community
  • 1
  • 1
Jared Kells
  • 6,771
  • 4
  • 38
  • 43
  • It seem to solve the issue. But there is a problem with this concept - the hard coded values hurts service flexibility on one hand and on the other a client referring to class lib with service reference which holds app.config file contains the services details strongly types the service implementation to the clients, while the client should not care how things done. – Roi Shabtai Apr 30 '12 at 11:39
  • I'm not sure what you are saying. Either you want the client's to configure the service or you don't. If you don't then they should be hard coded inside your COM dll as per my answer. If you want them to be configured by the client then you need to provide an interface from your COM object that enables clients to configure them. App.config files and manifest files are part of the .NET framework your COM clients have no way of loading them. – Jared Kells Apr 30 '12 at 23:53
0

I have successfully compiled your C# code as a class library and registered it with COM. I ran the example .vbs script and a message box appeared with the text "hello"

Could the constructor for ServiceClient or some code called in the constructor be throwing an exception?

In my sample project ServiceClass was just an empty class.

    [ComVisible(false)]
    public class ServiceClient{}

You can debug your COM library using Visual Studio.

  • Open the properties for the class library
  • Select the Debug tab and change the start action to: Start External Program.
  • Enter "c:\windows\system32\wscript.exe" as the external program.
  • Enter the path to your vbs file as the command line argument.

Break on all exceptions

  • Under the debug menu in Visual Studio select exceptions.
  • Tick the thrown box for each exception type.

Start debugging. Visual Studio should break when the InvalidOperation exception is thrown and you can then examine the stack trace.

Jared Kells
  • 6,771
  • 4
  • 38
  • 43
  • The problem is using Service reference. When using any kind of close object like you did, no exception is thrown. Try to set ServiceClient as instance of external wcf service reference. – Roi Shabtai Apr 30 '12 at 11:09