简体   繁体   English

COM / ActiveX DLL使用WCF服务

[英]COM/ActiveX DLL to consume WCF service

I am trying to extend an application which supports COM/ActiveX objects. 我正在尝试扩展支持COM / ActiveX对象的应用程序。 The COM dll needs to send some data to other system on local network for further processing and actions. COM dll需要向本地网络上的其他系统发送一些数据,以进行进一步的处理和操作。

I have tested a basic WCF Host-Client setup and it works fine from console client to console host. 我已经测试了基本的WCF主机-客户端设置,并且从控制台客户端到控制台主机都可以正常工作。 But now I need to send data through a client in com-visible dll. 但是现在我需要通过客户端在com-visible dll中发送数据。

This is the code of the 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;
        }
    }
}
} 

The dll works fine as a reference but cannot create object through target application(It has the functionality to access COM/ActiveX objects). dll可以很好地用作参考,但是不能通过目标应用程序创建对象(它具有访问COM / ActiveX对象的功能)。 When I try to access the dll by : 当我尝试通过以下方式访问dll时:

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. 我使用Remoting以类似的方式创建了一个com-visible dll,以实现此目的,并且它像一个魅力一样工作。 But now its not working as a WCF Client. 但是现在它不能用作WCF客户端。

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. 我已经搬到Remoting了,这不是问题,但建议我远离它,并通过WCF实现这一目标。

PS : I am new to C# so please excuse any stupid mistakes. PS:我是C#的新手,所以请原谅任何愚蠢的错误。

COM does not support static methods, see here for further details. COM不支持静态方法,请参见此处以获取更多详细信息。 You'll need to remove the static keyword from the class in order to let your client create an instance. 您需要从类中删除static关键字,以便让您的客户端创建实例。 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. 附带说明一下,您的代码甚至都不应编译,因为接口上的static修饰符是非法的。 Remove it as well, then recompile and re-register your DLL. 也将其删除,然后重新编译并重新注册您的DLL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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