{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/23785","repository_url":"https://api.github.com/repos/dotnet/corefx","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/23785/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/23785/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/23785/events","html_url":"https://github.com/dotnet/corefx/issues/23785","id":255075778,"number":23785,"title":"BinaryFormatter has inconsistency when handling assembly names of core runtime types","user":{"login":"CXuesong","id":11611687,"avatar_url":"https://avatars2.githubusercontent.com/u/11611687?v=4","gravatar_id":"","url":"https://api.github.com/users/CXuesong","html_url":"https://github.com/CXuesong","followers_url":"https://api.github.com/users/CXuesong/followers","following_url":"https://api.github.com/users/CXuesong/following{/other_user}","gists_url":"https://api.github.com/users/CXuesong/gists{/gist_id}","starred_url":"https://api.github.com/users/CXuesong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CXuesong/subscriptions","organizations_url":"https://api.github.com/users/CXuesong/orgs","repos_url":"https://api.github.com/users/CXuesong/repos","events_url":"https://api.github.com/users/CXuesong/events{/privacy}","received_events_url":"https://api.github.com/users/CXuesong/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2017-09-04T15:44:08Z","updated_at":"2017-09-05T14:54:11Z","closed_at":null,"author_association":"NONE","body":"It seems that in .NET Framework, `BinaryFormatter` will only store the `Type` name without assembly name when the `Type` is in `mscorlib`. This can be inferred from the [source code of `ObjectWriter.GetAssemblyId`](http://referencesource.microsoft.com/#mscorlib/system/runtime/serialization/formatters/binary/binaryobjectwriter.cs,1356). However, in .NET Core, there are actually two assemblies involved. See the source below\r\nhttps://github.com/dotnet/corefx/blob/2443b0d5f2a25f9c619aff9dca8ec2625eba1c96/src/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectWriter.cs#L987-L992\r\n\r\nThe two assemblies, as defined in the follow\r\nhttps://github.com/dotnet/corefx/blob/87f4f3290ec7b2fc9418cf9c8bf9fbfd0cd7c485/src/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/Converter.cs#L34-L42\r\n\r\nare, in effect\r\n\r\n```\r\ns_urtAssembly: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\r\ns_urtAlternativeAssembly: System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e\r\n```\r\n\r\nWhen de-serializing, `BinaryParser.ReadObjectWithMapTyped` will determine whether the assembly id is 0, and if so, it will use the default assembly as mentioned above\r\nhttps://github.com/dotnet/corefx/blob/2443b0d5f2a25f9c619aff9dca8ec2625eba1c96/src/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryParser.cs#L430-L433\r\n\r\nThe problem is, the object parser just defaults the assembly to `mscorlib`, so for some types, e.g. `System.RuntimeType`, which is defined in `System.Private.Corelib`, will not be found, and eventually cause a `TypeLoadExceptionHolder` [instantiated here](https://github.com/dotnet/corefx/blob/87f4f3290ec7b2fc9418cf9c8bf9fbfd0cd7c485/src/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectReader.cs#L240-L246).\r\n\r\nBy the way, I am just trying to unofficially port BotBuilder SDK to .NET Core 2.0. It relies on the serialization of `Delegate`s, which amounts to the serialization of `Type`, `MemberInfo`, etc. I tried to use `ISerializationSurrogate` to hack the formatter, making it support more object types, but when trying to surrogate the deserialization of `RuntimeType`, my `ISerializationSurrogate.GetSurrogate` will just receive a `Type` of `TypeLoadExceptionHolder`. This is just suprising. Though I am now using `SerializationInfo.SetType` and  `IObjectReference` to workaround the problem, I'm still curious. Is this behavior is by design or not? Thank you.\r\n\r\nUpdate: Well, does this have something to do with `System.RuntimeType` that it doesn't have `[TypeForwardedFrom]` attribute?"},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/327201298","html_url":"https://github.com/dotnet/corefx/issues/23785#issuecomment-327201298","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/23785","id":327201298,"user":{"login":"CXuesong","id":11611687,"avatar_url":"https://avatars2.githubusercontent.com/u/11611687?v=4","gravatar_id":"","url":"https://api.github.com/users/CXuesong","html_url":"https://github.com/CXuesong","followers_url":"https://api.github.com/users/CXuesong/followers","following_url":"https://api.github.com/users/CXuesong/following{/other_user}","gists_url":"https://api.github.com/users/CXuesong/gists{/gist_id}","starred_url":"https://api.github.com/users/CXuesong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CXuesong/subscriptions","organizations_url":"https://api.github.com/users/CXuesong/orgs","repos_url":"https://api.github.com/users/CXuesong/repos","events_url":"https://api.github.com/users/CXuesong/events{/privacy}","received_events_url":"https://api.github.com/users/CXuesong/received_events","type":"User","site_admin":false},"created_at":"2017-09-05T14:54:11Z","updated_at":"2017-09-05T14:54:11Z","author_association":"NONE","body":"For one more reference, [here](https://gist.github.com/CXuesong/a29e8c126db96343e5948d67eb6010db#file-coreserializationtest-cs) is my code for problem reproduction.\r\n\r\nOn `net462`\r\n```\r\nTest for surrogate: System.RuntimeType\r\nSystem.String\u000B   \u0004\u0001   \u0012System.RuntimeType\u0002   \fAssemblyNamFullName\u0001\u0001\u0006\u0002   Kmscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\u0006\u0003\r\nTest for surrogate: System.RuntimeType\r\nTest for surrogate: System.RuntimeType\r\nTest for surrogate: System.RuntimeType\r\nDeserialized: System.String\r\n```\r\nOn `netcoreapp2.0`\r\n```\r\nTest for surrogate: System.RuntimeType\r\nSystem.String\u000B   \u0004\u0001   \u0012System.RuntimeType\u0002   \fAssemblyNamFullName\u0001\u0001\u0006\u0002   YSystem.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e\u0006\u0003\r\nTest for surrogate: System.Runtime.Serialization.TypeLoadExceptionHolder\r\n\r\nUnhandled Exception: System.Runtime.Serialization.SerializationException: Unable to load type System.RuntimeType required for deserialization.\r\n   at System.Runtime.Serialization.ObjectManager.DoFixups()\r\n   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(BinaryParser serParser, Boolean fCheck)\r\n   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, Boolean check)\r\n   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)\r\n   at CoreSerializationTest.Program.Laundry[T](T graph, IFormatter formatter) in Program.cs:line 29\r\n   at CoreSerializationTest.Program.Main(String[] args) in Program.cs:line 18\r\n```"}}
{"actor":{"display_login":"CXuesong"}}