C#5 AsyncCtp BadImageFormatException

2024-05-04

请帮助我解决这个问题,我一直在使用异步库和 C#5 ctp 编译器编写控制台应用程序。当我第一次实际运行等待的代码时,我得到了这个:

System.BadImageFormatException was unhandled
  Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
  Source=AsyncCtpLibrary
  StackTrace:
    Server stack trace: 
       at [...].<Execute>d__1c.MoveNext()
       at [...].Execute()
       at [...].<Move>d__1d.MoveNext() in[..]:line 266
    Exception rethrown at [0]: 
       at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.<SetException>b__1(Object state)
       at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
  InnerException: 

我是否缺少要引用的dll?

重要的新东西
我的失败方法如下所示:

public async override Task<bool> Execute()
{
    //do stuff
    await stuff;
    //do other stuff
    await base.Execute()
    //do other stuff
    return true;
}

我遵循 Jon Skeet 的建议,尝试一点一点地重现错误,现在我可以看出,await base.Execute() 行是杀手!如果我注释掉该行,则一切都会运行,如果我保留它,则调用我的方法会立即失败(不是在到达 base.Execute() 时)。所以我认为 ctp 编译器做了一些奇怪的事情。为什么?我绝对不应该做什么?这个错误有多大?

老东西:

EDIT:
As for 32bit/64bit issue, my system is 32bit (inside a virtual machine, mind you), and as far as I know AsyncCtpLibrary.dll doesn't contain unmanaged code. All my projects (class libraries and single console app) all have build tabs like this:screenshot
What can possibly be still wrong?


编辑: 我还检查了融合日志查看器,AsyncCtpLibrary 已加载,没有任何错误:

*** Assembly Binder Log Entry  (6/10/2011 @ 9:04:11 PM) ***    
The operation was successful.    
Bind result: hr = 0x0. The operation completed successfully.     
Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll    
Running under executable  C:\Users\Daver\Documents\Visual Studio 2010\Projects\[...]\bin\Debug\MyApp.exe

--- A detailed error log follows. 

=== Pre-bind state information ===    
LOG: User = WIN-N74LV38NLV3\Daver    
LOG: DisplayName = AsyncCtpLibrary, Version=1.0.4107.18181, Culture=neutral, PublicKeyToken=31bf3856ad364e35    
 (Fully-specified)    
LOG: Appbase = file:///C:/Users/Daver/Documents/Visual Studio 2010/Projects/[...]/bin/Debug/

LOG: Initial PrivatePath = NULL    
LOG: Dynamic Base = NULL    
LOG: Cache Base = NULL    
LOG: AppName = MyApp.exe    
Calling assembly : MyLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===

LOG: This bind starts in default load context.    
LOG: Using application configuration file: C:\Users\Daver\Documents\Visual Studio 2010\Projects\[...]\bin\Debug\MyApp.exe.Config    
LOG: Using host configuration file:     
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.    
LOG: Post-policy reference: AsyncCtpLibrary, Version=1.0.4107.18181, Culture=neutral, PublicKeyToken=31bf3856ad364e35    
LOG: GAC Lookup was unsuccessful.    
LOG: Attempting download of new URL file:///C:/Users/Daver/Documents/Visual Studio 2010/Projects/[...]/bin/Debug/AsyncCtpLibrary.DLL.    
LOG: Assembly download was successful. Attempting setup of file: C:\Users\Daver\Documents\Visual Studio 2010\Projects\[...]\bin\Debug\AsyncCtpLibrary.dll    
LOG: Entering run-from-source setup phase.    
LOG: Assembly Name is: AsyncCtpLibrary, Version=1.0.4107.18181, Culture=neutral, PublicKeyToken=31bf3856ad364e35    
LOG: Binding succeeds. Returns assembly from C:\Users\Daver\Documents\Visual Studio 2010\Projects\[...]\bin\Debug\AsyncCtpLibrary.dll.    
LOG: Assembly is loaded in default load context.

我还检查了IL code of the <Execute>d__1c编译器生成的类的 MoveNext() 方法,其引用的唯一程序集 ([程序集名称]) 是 mscorlib、System.Core 和 AsyncCtpLibrary。


我检查了manifest我的 dll 和 AsyncCtpLibrary 的,我的说.corflags 0x00000003 // ILONLY 32BITREQUIRED,AsyncCtpLibrary说.corflags 0x00000009 // ILONLY,我不确定这是否是问题所在。

请帮忙,我没有主意了!


编辑:我收到了编译器团队的回复,他们已确认这是一个错误。它已经在他们的代码库中得到修复,所以希望我们能在下一个版本/测试版/CTP 中看到该修复。该修复不会向后移植到“正常”VS2010,因为这是一组非常不寻常的情况,至少在异步之前是这样。


编辑:好的,我现在有了一个非常短但完整的程序来演示这个问题。我相信它是泛型的混合物and调用基本方法:

using System;
using System.Threading.Tasks;

public abstract class AsyncAction<T>
{
    public virtual Task<T> Execute()
    {
        // We never get this far
        Console.WriteLine("Execute called");
        return null;
    }
}

public class BoolAction : AsyncAction<bool>
{
    public async override Task<bool> Execute()
    {
        return await base.Execute();
    }
}

class Test
{
    static void Main()
    {
        BoolAction b = new BoolAction();
        b.Execute();
    }
}

编辑:好的,我想出了一个解决方法。基本上,为了非虚拟地调用基类方法,编译器在中创建一个合成方法BoolAction。它有点错误,但是we可以得到它的权利:

public class BoolAction : AsyncAction<bool>
{
    public async override Task<bool> Execute()
    {
        return await BaseExecute();
    }

    private Task<bool> BaseExecute()
    {
        return base.Execute();
    }
}

所以每当你写作的时候base.Execute, write BaseExecute并插入那个额外的方法。它不是too糟糕的解决方法,直到团队修复该错误。

编辑:我稍微简化了示例 - 您不需要任何覆盖,特别是您不需要基类来公开Task<T>。致电any虚拟的base.Foo方法会做到这一点:

public abstract class AsyncAction<T>
{
    public virtual T GetT()
    {
        return default(T);
    }
}

public class BoolAction : AsyncAction<bool>
{
#pragma warning disable 1998 // We're not awaiting anything
    public async void Execute()
    {
        base.GetT();
    }
#pragma warning restore 1998
}

class Test
{
    static void Main()
    {
        BoolAction b = new BoolAction();
        b.Execute();
    }
}

编辑:与我之前的想法相反,这does也会影响迭代器。无需异步 CTP...

public abstract class Base<T>
{
    public virtual T GetT()
    {
        return default(T);
    }
}

public class Derived : Base<bool>
{
    public System.Collections.IEnumerator Foo()
    {
        base.GetT();
        yield break;
    }
}

class Test
{
    static void Main()
    {
        Derived d = new Derived();
        d.Foo().MoveNext();
    }
}

编辑:它也会影响匿名函数......

using System;

public abstract class Base<T>
{
    public virtual T GetT()
    {
        return default(T);
    }
}

public class Derived : Base<bool>
{
    public void Foo()
    {
        Action x = () => base.GetT();
        x();
    }
}

class Test
{
    static void Main()
    {
        Derived d = new Derived();
        d.Foo();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C#5 AsyncCtp BadImageFormatException 的相关文章

  • AZURE:workerrole 中的异步 Run()

    我有一个异步任务 async Task UploadFiles 我想在 azure 工作者角色的 Run 方法中调用 UploadFiles 上的 等待 但 await 仅适用于声明为异步的方法 那么我可以使 Run 方法异步 如下所示 p
  • 为什么 async wait 在 IIS 上不起作用,但在 IIS Express 上起作用

    我不明白为什么 async await 不能解决 IIS 线程的问题 我发现当我使用 IIS 时 我们对 IIS 线程的限制等于 10 而对 IIS Express 则没有限制 我在 HomeController 中添加了 2 个方法来重复
  • async/await 中的 return 语句在哪里

    我可能已经让自己陷入了相当不成熟的困惑之中 请参考下面的代码 控制台应用程序 namespace Tasks101 class Program static void Main string args Program p new Progr
  • FindAsync 很慢,但是延迟加载很快

    在我的代码中 我曾经使用加载相关实体await FindAsync 希望我能更好地遵守 C 异步指南 var activeTemplate await exec DbContext FormTemplates FindAsync exec
  • ServiceStack:异步/等待服务处理程序

    我读过一些涉及这个问题的问题 尽管其中许多已经有好几年了 如何在 ServiceStack API 中编写 Service 处理程序 使其成为 async await docs servicestack net 上没有任何文档提到 asyn
  • Task.Factory.StartNew 与异步方法

    可能是一个微不足道的问题 但它可能有助于我的基本理解 以下两个实现之间有什么重要区别吗 Task Factory StartNew public Task
  • wait task.delay 有助于加快 UI 刷新速度,但是如何实现呢?

    我有一个视图模型 它正在获取一行记录并显示在 Windows Phone UI 上 这个获取数据的视图模型方法正在执行大量任务 所有任务都标记为等待操作 如下所示 async Task GetData var dataCollection
  • 如何在 C# 中捕获等待的异步方法的异常?

    我基本上想知道在 C 中我应该如何捕获通过等待的异步方法的异常await关键词 例如 考虑以下小控制台程序 其中最重要的是包含一个名为AwaitSync AwaitSync calls TestAsync 它返回一个任务 执行时会抛出异常
  • 基础设施 - 同步和异步接口和实现? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 在实现库 基础设施时 并且该 API 的用户希望同步和异步使用代码 我读到混合同步和异步并不是一个好主意 例如 同步实现包括等待异步实现 显然
  • 在 Swift async/await 中,我可以使用 Lock 还是 Semaphore

    这不是问题 这是一个想寻求帮助以及专业指导的问题 根据文档 Sendable 类型可以在 Swift Concurrency 中安全地传递 在旧项目中并非所有类型都是可发送的 并且可能使用Cocoa类型 但它们是线程安全的 例如 class
  • 同步执行异步函数

    我对此主题进行了大量搜索 并且阅读了本网站上有关此主题的大部分帖子 但是我仍然感到困惑 我需要一个直接的答案 这是我的情况 我有一个已建立的 Winform 应用程序 但无法使其全部 异步 我现在被迫使用一个全部编写为异步函数的外部库 在我
  • async wait 在调用异步方法时返回 Task> 而不是 List

    我正在尝试了解 async wait 的用法 并且研究了一些博客文章 现在我已经编写了一个测试代码 但它没有按照我期望的方式工作 我有一个返回列表的方法 private List
  • 如何使用Task.WhenAny并实现重试

    我有一个创建多个基于 I O 的任务的解决方案 我正在使用Task WhenAny 来管理这些任务 但通常许多任务会由于网络问题或请求限制等原因而失败 我似乎找不到一个解决方案 使我能够在使用时成功重试失败的任务Task WhenAny 方
  • 为什么等待冷任务不会抛出

    我只是在尝试看看当一个冷任务 即一个任务 时会发生什么Task尚未开始 正在等待 令我惊讶的是 代码永远挂起并且 完成的 永远不会被打印 我希望抛出异常 public async Task Test1 var task new Task g
  • 嵌套异步/等待 Nodejs

    似乎无法弄清楚为什么这对我不起作用 我有一个父函数 它对子加载进程执行 AWAIT LOAD 进程又调用另一个名为 LOADDATA 的 AWAIT 所以基本上是这样的 module exports async function try a
  • 等待 IAsyncResult 函数直至完成

    我需要创建等待 IAsyncResult 方法完成的机制 我怎样才能做到这一点 IAsyncResult result contactGroupServices BeginDeleteContact contactToRemove Uri
  • Web UI 中的 .Result 出现死锁

    我正在阅读以下主题http blog stephencleary com 2012 07 dont block on async code html http blog stephencleary com 2012 07 dont bloc
  • Async/Await - 如何在递归 Ajax 函数中实现 Javascript Async-Await?

    我有两个功能 I call trendyolStocksUpdate 内部有循环的函数多次syncTrendyolOFFStocks 功能 I used async await but trendyolStocksUpdate 函数不是按顺
  • 惰性共享异步资源——澄清一下?

    我在斯蒂芬的书的末尾看到了这个例子 该代码可以通过以下方式访问more比一个线程 static int simpleValue static readonly Lazy
  • 调用await后程序退出

    我有一个while loop 应该重复程序直到满足特定条件 在这个循环中我称之为async函数 它为我打印一条消息 这是 简短的 代码 private void InitializeMessageSystem do Do stuff awa

随机推荐