Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization error in .NET Core projects #82

Closed
zspitz opened this issue Jul 3, 2019 · 2 comments
Closed

Serialization error in .NET Core projects #82

zspitz opened this issue Jul 3, 2019 · 2 comments
Labels
bug Something isn't working

Comments

@zspitz
Copy link
Owner

zspitz commented Jul 3, 2019

ATM, using the visualizer against a .NET Core project fails, with the following stack trace:

An unhandled exception of type 'System.TypeLoadException' was thrown by the custom visualizer component in the process being debugged.

Could not load type 'System.Runtime.CompilerServices.IsReadOnlyAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

wtih the following stack trace:

 at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
   at System.Reflection.CustomAttribute.IsDefined(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
   at System.Runtime.Serialization.SerializationEvents.GetMethodsWithAttribute(Type attribute, Type t)
   at System.Runtime.Serialization.SerializationEvents..ctor(Type t)
   at System.Runtime.Serialization.SerializationEventsCache.<>c.<GetSerializationEventsForType>b__1_0(Type type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Runtime.Serialization.SerializationEventsCache.GetSerializationEventsForType(Type t)
   at System.Runtime.Serialization.SerializationObjectManager.RegisterObject(Object obj)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArrayMember(WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, Object data)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, BinaryFormatterWriter serWriter, Boolean fCheck)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Boolean check)
   at ExpressionTreeVisualizer.VisualizerDataObjectSource.TransferData(Object target, Stream incomingData, Stream outgoingData) in C:\Users\Spitz\source\repos\zspitz\ExpressionToString\Visualizer.Shared\VisualizerDataObjectSource.cs:line 9
   at Microsoft.VisualStudio.DebuggerVisualizers.DebuggeeSide.Impl.ClrCustomVisualizerDebuggeeHost.TransferData(Object visualizedObject, Byte[] uiSideData)

I've managed to isolate the problem outside of the context of the visualizer:

// using System;
// using System.IO;
// using System.Runtime.Serialization.Formatters.Binary;
var stream = File.Create(Path.GetTempFileName());
var formatter = new BinaryFormatter();
var data = new VisualizerData(expr); // serialized class with information about a given expression tree
formatter.Serialize(stream, data); // fails with the same exception

but only if there is a reference to the visualizer DLL project, and VisualizerData comes from there.

If I copy VisualizerData.cs to the .NET Core project and use that, without a reference to the visualizer DLL, the serialization works fine.

A possible workaround might be to move VisualizerData to the Shared project, as it doesn't have any dependencies on the visualizer DLL.

@zspitz
Copy link
Owner Author

zspitz commented Jul 4, 2019

See dotnet/core#2981 (comment).

When we upgrade to .NET Core 3, probably with the next version of .NET Standard, we can go back to auto-implemented properties.

@zspitz
Copy link
Owner Author

zspitz commented Aug 14, 2019

But since .NET Framework will never be adapted to match a later version of .NET Standard (>2), as long as Visual Studio runs on ,NET Framework, we'll never be able to use auto-implemented properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant