c# system . runtime . serialize . serializationexception:无法找



我已经找了很多关于这个的指针,但还没有找到相关的Q/a

所以我要添加一个新的属性到Serializable类,看起来像这样:

[Serializable]
public class SerializableClass
{
public ExistingClass1[] property1;
public ExistingClass2[] property2;
// New property
public NewType[] property3;
}

新属性的类型是这样定义的:

[Serializable]
public struct NewType
{
public string prop1;
public string prop2;
}

到目前为止一切都很好,但在测试期间,我发现我们的客户端工具的一些旧版本停止工作。他们开始抛出异常:

System.Runtime.Serialization.SerializationException: Unable to find assembly 'MyAssembly, Version=21.7.0.0, Culture=neutral, PublicKeyToken=null'.

在System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly ()在System.Runtime.Serialization.Formatters.Binary.ObjectReader。GetType(二进制汇编信息汇编信息,字符串名称)在System.Runtime.Serialization.Formatters.Binary.BinaryConverter。TypeFromInfo(BinaryTypeEnum BinaryTypeEnum, Object typeInformation, ObjectReader, ObjectReader, BinaryAssemblyInfo, assemblyInfo, InternalPrimitiveTypeE&primitiveTypeEnum String&typeString Type&类型,Boolean&isVariant)在System.Runtime.Serialization.Formatters.Binary.__BinaryParser。ReadArray (BinaryHeaderEnum BinaryHeaderEnum)在System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run ()在System.Runtime.Serialization.Formatters.Binary.ObjectReader。反序列化(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter。Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)

有趣的是,只有当它试图反序列化一个具有新属性的对象时才会发生这种情况,否则旧客户端不会抱怨。

我们的反序列化器是这样的:

BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
using (MemoryStream r = new MemoryStream(bits))
{
return formatter.Deserialize(r);
}

正如你所看到的,我们不幸地使用了DotNet二进制序列化器/反序列化器。

这里有什么解决办法吗?使用List是否更好?

所以在花了很长时间之后,我得出结论,Binder是唯一的解决方案,但即使这样也不是万无一失的,只有在你幸运的情况下才能奏效。

对于我的场景,甚至自定义绑定也不是一个解决方案,因为其中一个工具调用一些api,这些api返回由该工具反序列化的blob。对于在服务端发生反序列化的其他调用,使用binder就可以了。

这是我所遵循的链接,而不是现有的关于BinaryFormatter和binder的SO答案。如上所述,这对我来说部分有效,但值得一提。

感谢所有试图帮助我的人,干杯!

最新更新