Skip to content

Commit

Permalink
use BinaryFormatter as a fallback when we get NotSupportedException e…
Browse files Browse the repository at this point in the history
…xception and the stream is seekable
  • Loading branch information
adamsitnik committed May 20, 2024
1 parent ba41ff0 commit cde8100
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,25 @@ private bool ValidateReaderType(string readerType)
#pragma warning disable SYSLIB0011
private object ReadBinaryFormattedObject()
{
BinaryFormattedObject binaryFormattedObject = new BinaryFormattedObject(_store.BaseStream,
new BinaryFormattedObject.Options()
long position = _store.BaseStream.CanSeek ? _store.BaseStream.Position : -1;

try
{
BinaryFormattedObject binaryFormattedObject = new BinaryFormattedObject(_store.BaseStream);

return binaryFormattedObject.Deserialize();
}
catch (NotSupportedException) when (position >= 0)
{
_store.BaseStream.Position = position;

BinaryFormatter? formatter = new()
{
Binder = new UndoTruncatedTypeNameSerializationBinder()
});
};

return binaryFormattedObject.Deserialize();
return formatter.Deserialize(_store.BaseStream);
}
}
#pragma warning restore SYSLIB0011

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ internal static TypeName ReadTypeName(this BinaryReader binaryReader, PayloadOpt
string name = binaryReader.ReadString();
if (!TypeName.TryParse(name.AsSpan(), out TypeName? typeName, options.TypeNameParseOptions))
{
throw new SerializationException($"Invalid type name: '{name}'");
throw
#if SYSTEM_RUNTIME_SERIALIZATION_BINARYFORMAT
new SerializationException
#else
new NotSupportedException
#endif
($"Invalid type name: '{name}'");
}
else if (typeName.AssemblyName is not null)
{
Expand All @@ -83,7 +89,13 @@ internal static AssemblyNameInfo ReadLibraryName(this BinaryReader binaryReader)
string name = binaryReader.ReadString();
if (!AssemblyNameInfo.TryParse(name.AsSpan(), out AssemblyNameInfo? libraryName))
{
throw new SerializationException($"Invalid library name: '{name}'");
throw
#if SYSTEM_RUNTIME_SERIALIZATION_BINARYFORMAT
new SerializationException
#else
new NotSupportedException
#endif
($"Invalid library name: '{name}'");
}

return libraryName;
Expand Down

0 comments on commit cde8100

Please sign in to comment.