简体   繁体   中英

COM/ActiveX DLL to consume WCF service

I am trying to extend an application which supports COM/ActiveX objects. The COM dll needs to send some data to other system on local network for further processing and actions.

I have tested a basic WCF Host-Client setup and it works fine from console client to console host. But now I need to send data through a client in com-visible dll.

This is the code of the dll :

namespace Client
{
[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
[ComVisible(true)]
public interface ISend
{
    [DispId(1)]
    bool SendData(string msg);
}
[ClassInterface(ClassInterfaceType.None), Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), ProgId("Client.Send")]
[ComVisible(true)]
public class Send : ISend
{
    static BasicHttpBinding binding = new BasicHttpBinding();
    static EndpointAddress endpoint = new EndpointAddress(new Uri("http://192.168.1.6:8000/WCFHost/Service/GetData"));

    GetDataClient client = new GetDataClient(binding, endpoint);
    [ComVisible(true)]
    public bool SendData(string msg)
    {       
        try
        {
            if (client.getData(msg))
            {
                client.Close();
                return true;
            }
            else
            {
                client.Close();
                return false;
            }
        }
        catch (Exception e)
        {
            client.Abort();
            return false;
        }
    }
}
} 

The dll works fine as a reference but cannot create object through target application(It has the functionality to access COM/ActiveX objects). When I try to access the dll by :

obj = CreateObject ("Client.Send");
obj.SendData("Hello")

It says :

COM/object handle is null

on second line nothing more!

I created a com-visible dll in similar way using Remoting to achieve this and it worked like a charm. But now its not working as a WCF Client.

It would be really appreciated if someone could point out what I am doing wrong.

I had moved to Remoting where this was not a problem, but I was suggested to stay away from it and achieve this through WCF.

PS : I am new to C# so please excuse any stupid mistakes.

COM does not support static methods, see here for further details. You'll need to remove the static keyword from the class in order to let your client create an instance. This will also allow you to implement the interface, which is not possible for static classes.

As a side note, your code shouldn't even compile, since the static modifier on an interface is illegal. Remove it as well, then recompile and re-register your DLL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM