AssemblyResolve不调用,序列化过程中FileNotFoundException异常被抛出 [英] AssemblyResolve is not invoked and FileNotFoundException is thrown during serialization
问题描述
在我的ASP.NET应用程序中有 MyAssembly.CustomIdentity
类和.NET运行时的试图序列化类。在系列化它抛出 FileNotFoundException异常
抱怨无法加载 MyAssembly程序
。
In my ASP.NET application there's MyAssembly.CustomIdentity
class and the .NET runtime tries to serialize that class. During serialization it throws FileNotFoundException
complaining it can't load MyAssembly
.
[SerializationException: Unable to find assembly 'MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +9464367
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +345
System.AppDomain.get_Id() +0
<CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie) +151
<CrtImplementationDetails>.DefaultDomain.Initialize() +30
<CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* ) +41
<CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* ) +391
<CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) +65
[ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain.]
<CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException) +61
<CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) +113
.cctor() +46
[TypeInitializationException: The type initializer for '<Module>' threw an exception.]
Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.InitializeEnvironment() +0
Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment..cctor() +809
[TypeInitializationException: The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception.]
Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_IsAvailable() +17
SampleWebApp.Default.Page_Load(Object sender, EventArgs e) in C:\Temp\AzureAdvancedRolesSource\Ex2-StartupTasks\CS\Begin\SampleWebApp\Default.aspx.cs:22
我搜索,看起来像处理 AppDomain.AssemblyResolve
事件应该有所帮助。所以,我实现了处理该事件的:
I searched and looks like handling AppDomain.AssemblyResolve
event should help. So I implemented handling that event:
public partial class Default : System.Web.UI.Page
{
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
return typeof(MyAssembly.CustomIdentity).Assembly;
}
protected void Page_Load(object sender, EventArgs e)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve +=
new ResolveEventHandler(MyResolveEventHandler);
// this code throws `FileNotFoundException`
// during a serialization attempt
bool isAvailable =
Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable;
}
}
但我的处理程序不会被调用,我仍然有一个在序列化的尝试同样的异常。我该如何解决这个问题 - 如何让我的串行找到我的装配
however my handler is not invoked and I still have the same exception during a serialization attempt. How do I resolve this problem - how do I make the serializer find my assembly?
推荐答案
这个问题可以与该CLR试图找到所有组件在启动时调用一个方法,因此连线之前的事件查找组装的事实处理程序AssemblyResolve事件。为了解决这个问题,你可以提取需要您组装成一个单独的方法在code,并从Page_Load中调用它。
The issue can be related to the fact that CLR tries to locate all assemblies when it starts invoking a method so it looks for the assembly before you wire up the event handler for AssemblyResolve event. To solve the issue you can extract the code that needs your assembly into a separate method and invoke it from Page_Load.
有关详细信息,请参阅本博客: AppDomain.AssemblyResolve事件建议
See this blog for more details: AppDomain.AssemblyResolve Event Tips
这篇关于AssemblyResolve不调用,序列化过程中FileNotFoundException异常被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!