Bug with IQueryable and yield syntax: System.BadImageFormatException "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"

I find a bug if you mix IQueryable and yield syntax.

After Diego has changed a little bit my sample to reproduce it, this is the code he sent to the C# team:

class Program

{

    static void Main(string[] args)

    {

        try

        {

            GetCategoryProductsUsingQueryable(() => new MyProduct(), 1).ToList();

            Console.WriteLine(“Queryable succeeds”);

        }

        catch (Exception e)

        {

            LogException(e);

        }

    }

 

    private static void LogException(Exception e)

    {

        Console.WriteLine(e.GetType());

        Console.WriteLine(e.Message);

    }

 

 

    private static List<Category> Categories =

        new List<Category> {

            new Category {

                CategoryID = 1,

                Products = new List<Product> {

                    new Product {

                        ProductID = 1 } } } };

 

 

    static IEnumerable<P> GetCategoryProductsUsingQueryable<P>(Func<P> newP, int categoryId) where P : IProduct

    {

        var categ = Categories.AsQueryable().FirstOrDefault(c => c.CategoryID == categoryId);

        if (categ == null)

            yield break;

        foreach (var p in categ.Products)

            yield return Load(newP(), p);

    }

 

    static P Load<P>(P item, Product p) where P : IProduct

    {

        item.ID = p.ProductID;

        return item;

    }

}

 

public interface IProduct

{

    int ID { get; set; }

}

 

public class MyProduct : IProduct

{

    public int ID { get; set; }

}

 

public class Category

{

    public int CategoryID { get; set; }

    public List<Product> Products { get; set; }

}

public class Product

{

    public int ProductID { get; set; }

}


In this case, the console output is:


System.BadImageFormatException
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) 

This entry was posted in 7671, 7672, 7674, 7676, 7677, 7806. Bookmark the permalink.

6 Responses to Bug with IQueryable and yield syntax: System.BadImageFormatException "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"

  1. Kristofer says:

    What OS are you running and in what kind of project do you get the error?

    I am running on Vista X64 SP1 and I used to get that error on a website project when deployed under IIS on a x86 system using a web deployment package. After changing to compile using the “publish website” thing in visual studio rather than the web deployment package the error went away.

    So depending on your system/os config, is it possible that you have a mix of x64 and x86 assemblies..?

  2. Matthieu MEZIL says:

    I use Vista x86 SP1.
    I don’t think you speak about the same thing than me. In my case, it’s just a console application (so without IIS and without possible mix of x64 and x86 assemblies).
    Moreover, in GetCategoryProductsUsingQueryable, if I remove the AsQueryable(), I have no problem.

  3. abc says:

    i am also having the same error. but it isnt resolved even after changing the exe version

  4. Kristofer says:

    No, not the same problem then. What caught my eye was that the exception was the same…

  5. Jonathan (SMT) says:

    Has this bug been resolved yet? I am still getting it now with VS 2008 and Win7.

  6. Matthieu MEZIL says:

    This bug is now fixed (with VS 2010 Beta)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>