AssemblyResolve is not invoked and FileNotFoundException is thrown during serialization

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:TempAzureAdvancedRolesSourceEx2-StartupTasksCSBeginSampleWebAppDefault.aspx.cs:22

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

See this blog for more details: AppDomain.AssemblyResolve Event Tips


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x