.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
[INFO] Exception Thrown When Trying To Deserialize An Object Whose Type Is Defined In An Assembly Which Is Loaded By Assembly.Lo

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

SYMPTOMS

Suppose you call Assembly.LoadFrom(...) to load an assembly named SimpleAsm in which type SimpleType is defined. Then you try to serialize a SimpleType ojbect, it works fine. But as soon as you deserialize some SimpleType object, you encounter following exception:

System.Runtime.Serialization.SerializationException: Cannot find the assembly SimpleAsm, Version=xxxx, Culture=xxxx, PublicKeyToken=xxxx.

Even the call to deserialization is in SimpleAsm itselft, the exception still exists. Although it looks a little bit ridiculous, it's the proper behavior by design.

CAUSE

"Some extensible applications use Assembly.LoadFrom to load an assembly and then construct objects from types defined in the loaded assembly. These objects can be serialized to a stream without any trouble. However, when deserializing this stream, the formatter attempts to load the assembly by calling Assembly's Load or LoadWithPartialName method instead of calling the LoadFrom method. In most cases, the Common Language Runtime (CLR) will not be able to locate the assembly file, causing a SerializationException exception to be thrown. "

RESOLUTION

Implement and register a System.ResolveEventHandler to help CLR class loader redirect to the properly position of the assembly and reload it into memory. The handler may look like this:

public static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
        string[] strProps = args.Name.Split(new char[] {','});
        String strPath = m_codebase + strProps[0] + ".dll";
        return Assembly.LoadFrom(strPath);
}

m_codebase is a relative directory that your assembly resides in. Before deserialize any SimpleType objects, register your event handler to the application domain:

Thread.GetDomain().AssemblyResolve += resolveHandler;

Also remember to unregister the handler when finish deserializing SimpleType objects to avoid any side-effects that may be brought by custom event handler:

Thread.GetDomain().AssemblyResolve -= resolveHandler;

MORE INFORMATION

.NET 247: SerializationException on microsoft.public.vsnet.ide
Run-time Serialization




相关文章

相关软件