top of page
Search
todefultiisitel

Type In Assembly Is Not Marked As Serializable Appdomain: Examples and Solutions from Stack Overflow



Assuming that the problem is that a field/property on your object is of the problem type, you need to either mark the field/property as NonSerialized or create a derivative of the type which is marked as Serializable




Type In Assembly Is Not Marked As Serializable Appdomain



.NET Remoting needs to access PluginAppDomain_UnhandledException from the child AppDomain. PluginAppDomain_UnhandledException is an instance method and so the child AppDomain needs to access it through the current object (this) from this class. There are two ways to do this. One is to have the class derive from MarshalByRefObject which will allow it's instances to be accessed from other AppDomains via proxies. The other way it to decorate the class with the SerializableAttribute and let .NET Remoting know that instance of this class can be serialized to other AppDomains. This is why you get the serializable error. Your class does not 1) derive from MarshalByRefObject and 2) is not marked as Serializable.


Some types are explicitly added to the LogicalCallContext by calling a method such as LogicalCallContext.SetData or CallContext.LogicalSetData. These types are not marked as serializable and are not stored in the global assembly cache.


During the serialization process, public and private fields of the object andthe name of the class and the containing assembly are converted to a stream ofbytes written to a data stream. During deserialization, an exact replica of theobject along with type information is reconstructed.


The CLR manages how objects are laid out in memory and provides an automatedserialization mechanism using reflection. When an object is serialized, the nameof the class, its containing assembly and all fields are written to storage.When an object is serialized, the CLR also keeps track of all referenced objectsalready serialized to ensure that the same object is not serialized more thanonce. The serialization architecture provided with the .NET Framework handlesobject graphs and circular references automatically. The only requirement forobjects referenced from the serialized object is that those referenced objectsbe marked as [Serializable]. Else, the CLR willthrow an exception when the serialize attempts to serialize an un-serializableobject.


When formatters serialize an object, the full name of the type (i.e., MyNamespace.MySubNamespace.MyClass)and the name of the assembly containing the type (i.e., MyAssembly.dll)are written to the byte stream. By default, BinaryFormatterand SoapFormatter types output the assembly's fullidentity (name, version, public key, and culture). To make these formatteroutput just the simple assembly name for each serialized type, set theformatter's AssemblyFormat property to FormatterAssemblyStyle.Simple(the default is FormatterAssemblyStyle.Full.)


After an assembly has been loaded, the formatter looks in the assembly for atype matching that of the object being serialized. If there is no matching type,an exception is thrown and no more objects can be serialized. If a matching typeis found, an instance type is created (constructor not called) and its fieldsare initialized from the values present in the stream.


Some application may use Assembly.LoadFrom toload an assembly and then construct objects from types defined in the assembly.Such object can be serialized with no problems. However, when deserializing suchobjects, the formatter attempts to load the assembly using Assembly.Loador Assembly.LoadWithPartialName instead of calling Assembly.LoadFrommethod. In most cases, the assembly will fail to load (most likely not found)and an exception will be thrown.


In general it is recommended that most types be made serializable since thisgrants a lot of flexibility to your users. However, recall that serializationreads all of an object's fields regardless of whether the fields are declared aspublic, internal, protected, or private. You might want to use selectiveserialization if the type contains sensitive data or if the data had nomeaning if transferred (i.e., windows handles, thread ids, etc. )


To allows objects of a serializable type to be serialized as different types,consider having this type implement the IConvertibleinterface. The IFormatterConverter is only used whendeserializing objects and when calling a Get method whose type does not matchthe type of the value in the stream.


During deserialization, the formatter determines that a binder has been set.As each object is about to be deserialized, the formatter calls the binder's BindToTypemethod, passing it the assembly name and the type that the formatter want todeserialize. At this point, BindToType decides whattype should actually be constructed and returns this type. Note that if the newtype uses simple serialization via the [Serializable]attribute, then the original type and the new type must have the same exactfield names and types. This restriction disappears if the new type implements ISerializableinterface. This approach is shown below:


When you create an application domain (and one is created for you when you launch any .NET program), you're creating an isolated process (usually called the "program") that manages static variables, additional required assemblies, and so forth. Application domains do not share anything. .NET uses "remoting" to communicate between application domains, but it can only do this if the classes that need to be shared between domains are marked as serializable, otherwise the remoting mechanism will not serialize the class.


Of course, this might seem strange when you're instantiating a class -- why does it need to be marked as serializable when we're only calling methods (even properties are syntactical sugar for get/set methods)? Of course, .NET doesn't "know" that you're only accessing methods -- you could very well be accessing fields as well, and therefore the class that you instantiate in the plug-in assembly must be serializable.


Working with application domains is not trivial -- classes must be serializable, there are design considerations and constraints as to whether to pass by value or by reference, and the performance is terrible. Is it worth making sure you have all your ducks in a row so that you can hot-swap an assembly? Well, "it depends" is the answer!


Contonuing on from my other comment, if anyone wants to have a go at getting the Dump() working, I get get the "plain text" output happening ok but if I add the description parameter I get an error that "Type 'LINQPad.ObjectGraph.HeadingPresenter' in (...linqpad assembly..) is not marked as serializable".


Looks like TestExecutaionContext is calling CallContext.LogicalSetData("Nunit.Framework.TestContext",current.contextDictionary)however contextDictionary=> TestExecutaionContext is not Serializable. This prevents remote calls. You will get aSystem.Runtime.Serialization.SerializationException : Type 'NUnit.Core.TestExecutionContext' in Assembly 'nunit.core, Version=2.6.2.12296, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' is not marked as serializableexception.You can work around this by callingCallContext.FreeNamedDataSlot("NUnit.Framework.TestContext");but is ugly and might break your expectation about what is in the context?-Erik


If you have any idea how we could prevent this issue from occurring feel free to share. Currently the only option I see is to make everything serializable, although it sounds more a workaround rather than a solution, since the assembly in which these classes are defined will most likely not be available in the remote process, but it's also true that the remote process will not need access to it in any case.


> This might help?>> -to-limit-the-scope-of-a-logical-call-context>> --> You received this bug notification because you are a bug assignee.> >> Title:> TestExecutaionContext SerializationException>> Status in NUnit V2 Test Framework:> Confirmed>> Bug description:> Nunit v 2.6.2.12296 throws a SerializationException because> TestExecutaionContext is not marked as Serializable.>> Looks like TestExecutaionContext is calling> CallContext.LogicalSetData("Nunit.Framework.TestContext",current.contextDictionary)> however contextDictionary=> TestExecutaionContext is not Serializable.> This prevents remote calls. You will get a> System.Runtime.Serialization.SerializationException : Type> 'NUnit.Core.TestExecutionContext' in Assembly 'nunit.core,> Version=2.6.2.12296, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' is> not marked as serializable> exception.> You can work around this by calling> CallContext.FreeNamedDataSlot("NUnit.Framework.TestContext");> but is ugly and might break your expectation about what is in the> context?> -Erik>> To manage notifications about this bug go to:> +bug/1084181/+subscriptions>


> Where in the NUnit code is this occuring... that is, what does Resharper> actually call into.>> My thought is that we could clear the context when we no longer need> it.>> Charlie>> --> You received this bug notification because you are a bug assignee.> >> Title:> TestExecutaionContext SerializationException>> Status in NUnit V2 Test Framework:> Confirmed>> Bug description:> Nunit v 2.6.2.12296 throws a SerializationException because> TestExecutaionContext is not marked as Serializable.>> Looks like TestExecutaionContext is calling> CallContext.LogicalSetData("Nunit.Framework.TestContext",current.contextDictionary)> however contextDictionary=> TestExecutaionContext is not Serializable.> This prevents remote calls. You will get a> System.Runtime.Serialization.SerializationException : Type> 'NUnit.Core.TestExecutionContext' in Assembly 'nunit.core,> Version=2.6.2.12296, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' is> not marked as serializable> exception.> You can work around this by calling> CallContext.FreeNamedDataSlot("NUnit.Framework.TestContext");> but is ugly and might break your expectation about what is in the> context?> -Erik>> To manage notifications about this bug go to:> +bug/1084181/+subscriptions> 2ff7e9595c


0 views0 comments

Recent Posts

See All

Time Out apk

O que é Time Out APK e por que você precisa dele Se você é fã de eventos ao vivo e jogos interativos, já deve ter ouvido falar do Time...

Comments


bottom of page