c# - 序列化期间未调用 AssemblyResolve 并抛出 FileNotFoundException

标签 c# .net asp.net iis azure

在我的 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;
    }
}

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

最佳答案

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

请参阅此博客了解更多详细信息:AppDomain.AssemblyResolve Event Tips

关于c# - 序列化期间未调用 AssemblyResolve 并抛出 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6493371/

相关文章:

asp.net - Response.write 和 Response.output.write 之间有什么区别

c# - 亚马逊产品广告API更新问题

c# - 如何以编程方式确定哪个应用程序正在锁定文件?

c# - 多个项目共享相同的代码而没有循环引用

c# - 为什么当找到多个元素时Single()不直接返回?

c# - 如何在 Windows 10 应用中实现应用内购买?

.net - 如何从 Devops 管道将 azure 函数发布到 Active Directory 中的 Azure Linux 函数

c# - Outlook 文件夹浏览对话框?

javascript - 检查 Javascript 中的干扰时间跨度?

asp.net - 这些中间件有什么不同