2011-06-27 22 views
3

ASP.NETアプリケーションには、MyAssembly.CustomIdentityクラスと.NETランタイムtries to serialize that classがあります。シリアライゼーション中にMyAssemblyをロードできないと不平を言うとFileNotFoundExceptionがスローされます。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

この問題は、AssemblyResolveイベントのイベントハンドラを配線する前に、アセンブリを検索するようにメソッドを呼び出すときに、すべてのアセンブリをCLRが見つけようとしていることに関連している可能性があります。この問題を解決するには、アセンブリを必要とするコードを別のメソッドに抽出し、Page_Loadから呼び出すことができます。

詳細については、このブログを参照してください。AppDomain.AssemblyResolve Event Tips

関連する問題