2011-06-27 93 views
3

在我的ASP.NET應用程序中有MyAssembly.CustomIdentity類和.NET運行時tries to serialize that class。在序列化期間,它會引發FileNotFoundException抱怨它無法加載MyAssembly未調用AssemblyResolve,並在序列化期間拋出FileNotFoundException

[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事件應該幫助。所以,我實現了處理該事件:

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; 
    } 
} 

但是我的處理程序不被調用,我仍然有一個序列化嘗試期間相同的異常。我如何解決這個問題 - 我如何讓序列化程序找到我的程序集?

+0

什麼是異常消息? – SLaks

+0

對於初學者,你不應該盲目地**返回你的程序集;你需要檢查'args'來查看它要求的內容......但是:現在有什麼異常消息? –

+0

@SLaks,@Marc Gravell ::我添加了調用堆棧,我想它提供了最詳細的信息。 – sharptooth

回答

4

該問題可能與CLR在開始調用方法時嘗試查找所有程序集,以便在爲AssemblyResolve事件連接事件處理程序之前查找程序集有關。爲了解決這個問題,你可以把需要你的程序集的代碼提取到一個單獨的方法中,並從Page_Load中調用它。

查看此博客的更多詳細信息:AppDomain.AssemblyResolve Event Tips

相關問題