It seems I've encountered a very old and well known problem, but despite of huge number of search results I haven't found any serious explanation... In most cases COM server was blamed, but I don't think that's correct (see below, it works with EmbedInteropTypes=true with the same server!).
MyCOMAppl = new COMAppl();
MyCOMAppl.event1 += Event1Handler;
MyCOMAppl.event2 += Event2Handler
At second event attachement the following exception is raised:
System.Runtime.InteropServices.COMException (0x80040202): Exception HRESULT: 0x80040202
at System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise(Object pUnkSink, Int32& pdwCookie)
When we change the order of event assignment (first event2, then event1) the same exception is raised.
I discovered a strange/inconsistent behaviour when changing Visual Studio project parameter "Embed Interop Types" to True (it works!) and the same code fails when it is False. Why should it work in a different way when interop.dll is embeded or not?
As my project is based on .Net Framework 3.5, changing EmbedInteropTypes is not a solution as the functionality was introduced since .Net Framework 4.0. For the moment, my workaround is using IConnectionPointContainer and IConnectionPoint instead of delegate approach. This way I create one event sink for all events and single Advise is sent.
Could someone experienced in C#/.Net provide explanation?
Thanks in advance!
User contributions licensed under CC BY-SA 3.0