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).
ServiceClient
ServiceClient
is private for the C# dllThe 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?
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.
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.
Break on all exceptions
Start debugging. Visual Studio should break when the InvalidOperation exception is thrown and you can then examine the stack trace.
User contributions licensed under CC BY-SA 3.0