0

I get a SerializationException when I add an EventHandler:

public interface IPlugin
{
   ...
   event EventHandler<Event1Args> Event1;
   event EventHandler<Event2Args> Event2;
}

[Serializable()]
public class Plugin : MarshalByRefObject, IPlugin
{
    [field:NonSerialized]
    public event EventHandler<Event1Args> Event1;
    [field:NonSerialized]
    public event EventHandler<Event2Args> Event2;
}

public class Test
{
   public void Method()
   {
      IPlugin p = Plugins.GetPlugin("test");
      try
      {
          p.Event1 += (sender,args) => Console.Writeline("Test Event");
      }
      catch (Exception e)
      {
          /* here comes the SerializationException: the type
             TestNamespace.Test in Assembly "TestNamespace.Test,Version,..." 
             is not marked serializable.
          */
      }
   }
}

At runtime, the IPlugin is an instance of the class Plugin, I can add the [field:NonSerialized] in that class, but not in the interface. But this did not change anything. I do not have the english VisualStudio so maybe the exception message is not 100% correct. My original exception message is:

System.Runtime.Serialization.SerializationException: Der Typ "TestNamespace.Test+<>c__DisplayClass5_0" in Assembly "TestNamespace, Version=2.1.6376.11505, Culture=neutral, PublicKeyToken=null" ist nicht als serialisierbar gekennzeichnet.

Server stack trace: bei System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) bei System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) bei System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() bei System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) bei System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) bei System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
bei System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
bei System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeMessageParts(ArrayList argsToSerialize) bei System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage..ctor(IMethodCallMessage mcm) bei System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage.SmuggleIfPossible(IMessage msg) bei System.Runtime.Remoting.Channels.CrossAppDomainSink.SyncProcessMessage(IMessage reqMsg)

Exception rethrown at [0]: bei System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) bei System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) bei TestNamespace.Plugins.IPlugin.add_Event1(EventHandler`1 value) bei TestNamespace.Test.d__5.MoveNext() in C:\xxx\Test.cs:Zeile 14.

The plugins are created in another AppDomain and with AggregateCatalog and CompositionContainer, so while debugging I see the IPlugin instance is a remote proxy instance. I don´t know if that could be the problem.

I already tried marking the Test class as [Serializable()], but that did not change anything.

AlteGurke
  • 585
  • 6
  • 20
  • What is IPlugin? I think it's not part of the .Net Framework – Romano Zumbé Jun 16 '17 at 05:47
  • Since you're using `BinaryFormatter` try marking the events as non-serialized as shown [here](https://stackoverflow.com/a/2308629/3744182) and [here](https://stackoverflow.com/a/1173669/3744182). – dbc Jun 16 '17 at 05:48
  • IPlugin is the interace in my example. I do not use `BinaryFormatter` on purpose. Is that the default Formatter for AppDomains? I can´t mark the events, because they are declared in the interface and there I can´t add the attribute. – AlteGurke Jun 16 '17 at 05:55

0 Answers0