【问题标题】:AssemblyResolve is not invoked and FileNotFoundException is thrown during serialization序列化期间未调用 AssemblyResolve 并引发 FileNotFoundException
【发布时间】:2012-01-10 16:55:51
【问题描述】:

在我的 ASP.NET 应用程序中有 MyAssembly.CustomIdentity 类和 .NET 运行时 tries to serialize that class。在序列化过程中,它会抛出 FileNotFoundException 抱怨它无法加载 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 事件应该会有所帮助。所以我实现了处理那个事件:

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

但是我的处理程序没有被调用,并且在序列化尝试期间我仍然遇到相同的异常。我该如何解决这个问题 - 如何让序列化程序找到我的程序集?

【问题讨论】:

  • 异常信息是什么?
  • 对于初学者,您不应该盲目地退回您的程序集;您需要检查args 以查看它的要求...但是:现在异常消息是什么?
  • @SLaks, @Marc Gravell:: 我添加了调用堆栈,我猜它提供了最详细的消息。
  • @Marc Gravell:是的,我想看看args 来推断要检查的内容,现在结果发现甚至没有调用处理程序。

标签: c# .net asp.net iis azure


【解决方案1】:

此问题可能与 CLR 在开始调用方法时尝试定位所有程序集的事实有关,因此它会在您为 AssemblyResolve 事件连接事件处理程序之前查找程序集。要解决这个问题,您可以将需要您的程序集的代码提取到一个单独的方法中,然后从 Page_Load 中调用它。

有关详细信息,请参阅此博客:AppDomain.AssemblyResolve Event Tips

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    相关资源
    最近更新 更多