The new Keyword on dll's Service Reference

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?

c#
service-reference
asked on Stack Overflow Apr 30, 2012 by Saturn Technologies • edited Apr 30, 2012 by Saturn Technologies

2 Answers

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.

answered on Stack Overflow Apr 30, 2012 by Jared Kells • edited May 23, 2017 by Community
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.

answered on Stack Overflow Apr 30, 2012 by Jared Kells • edited Apr 30, 2012 by Jared Kells

User contributions licensed under CC BY-SA 3.0