使用WCF服务的COM / ActiveX DLL

时间:2018-03-07 08:49:15

标签: c# wcf com wcf-client

我正在尝试扩展支持COM / ActiveX对象的应用程序。 COM dll需要将一些数据发送到本地网络上的其他系统以进行进一步处理和操作。

我已经测试了一个基本的WCF主机 - 客户端设置,它可以从控制台客户端到控制台主机正常工作。但现在我需要通过com-visible dll中的客户端发送数据。

这是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;
        }
    }
}
} 

dll可以作为引用正常工作但不能通过目标应用程序创建对象(它具有访问COM / ActiveX对象的功能)。当我尝试通过以下方式访问dll时:

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

它说:

COM/object handle is null

在二线上没有了!

我使用Remoting以类似的方式创建了一个com-visible dll来实现这一点,它就像一个魅力。但现在它不能作为WCF客户端工作。

如果有人能指出我做错了什么,我们将非常感激。

我已经转移到Remoting,这不是问题,但我被建议远离它并通过WCF实现这一目标。

P.S:我是C#的新手,所以请原谅任何愚蠢的错误。

1 个答案:

答案 0 :(得分:0)

COM不支持静态方法,有关详细信息,请参阅here。您需要从类中删除static关键字,以便让您的客户端创建实例。这也将允许您实现接口,这对于静态类是不可能的。

作为旁注,您的代码甚至不应该编译,因为接口上的static修饰符是非法的。删除它,然后重新编译并重新注册您的DLL。