C# &引用;未能添加事件处理程序";从COM处理事件时
我有一个COM服务器和一个调用它的Silverlight(OOTB)应用程序。我最近对COM服务器进行了一些重构,我设法打破了服务器和客户机之间的事件处理,我不知道我是如何打破它的。以下是我现在拥有的: 服务器C# &引用;未能添加事件处理程序";从COM处理事件时,c#,events,com,C#,Events,Com,我有一个COM服务器和一个调用它的Silverlight(OOTB)应用程序。我最近对COM服务器进行了一些重构,我设法打破了服务器和客户机之间的事件处理,我不知道我是如何打破它的。以下是我现在拥有的: 服务器 namespace ServerNS { [Guid("8FFF7AAE-B162-42C2-9F70-269D285CF622")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] [ComVisib
namespace ServerNS
{
[Guid("8FFF7AAE-B162-42C2-9F70-269D285CF622")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
interface IServerEvents
{
[DispId(1)]
void MyEvent(MyContainer container);
}
[Guid("D2F3738D-DD23-472F-9D36-4136E1196890")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface IServer
{
...
}
[Guid("77253016-1A2A-43CC-A360-04519A4F50F5")]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IServerEvents))]
[ProgId("Server.MyServer")]
[ComVisible(true)]
public class Server : IServer
{
public event MyEventHandler MyEvent;
public void Trigger(MyContainer container)
{
if (MyEvent != null)
{
MyEvent(container);
}
}
}
}
客户端
namespace ClientNS
{
public class Client
{
public void Init()
{
dynamic comObj = AutomationFactory.CreateObject("Server.MyServer");
AutomationEvent evt = AutomationFactory.GetEvent(comObj, "MyEvent");
evt.EventRaised += (sender, e) =>
{
//Do Stuff
}; //Exception occurs here.
}
}
}
错误
当我尝试将事件处理程序添加到AutomationEvent时,会出现此异常。这在我的重构(即添加IServerEvents接口)之前起作用。comObj是一个合法的实例——我可以在它上面调用其他COM方法——所以我不明白为什么它似乎找不到那个事件;或者,如果是错误的“失败”部分,如何找出失败的原因
System.Exception was unhandled by user code
Message=Failed to add event handler. Possible reasons include: the object does not support this or any events, or something failed while adding the event.
StackTrace:
at MS.Internal.Error.MarshalXresultAsException(UInt32 hr, COMExceptionBehavior comExceptionBehavior)
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.ComAutomation.ComAutomationNative.ConnectEvent(IntPtr nativePeer, String eventName, RaiseComAutomationEventDelegate raiseComAutomationEventDelegate)
at MS.Internal.ComAutomation.ComAutomationObject.ConnectEvent(String name)
at System.Runtime.InteropServices.Automation.AutomationEvent.UpdateConnection()
at System.Runtime.InteropServices.Automation.AutomationEvent.add_EventRaised(EventHandler`1 value)
at ClientNS.Client.Init()
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.OnRun(Object argument)
事实证明,问题在于接口没有声明为“public”——因此接口需要如下所示:
public interface IServerEvents
{
[DispId(1)]
void MyEvent(MyContainer container);
}
}
在OnDocumentComplete中
IHTMLElement htmlElement = document.getElementById("ele");
DHTMLEventHandler Handler = new DHTMLEventHandler((IHTMLDocument2)webBrowser.Document);
Handler.Handler += new DHTMLEvent(Dummy_OnClick);
htmlElement.onclick = Handler;
添加新方法以处理单击事件
void Dummy_OnClick(IHTMLEventObj e){
// DO Action
}
void Dummy_OnClick(IHTMLEventObj e){
// DO Action
}