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) 

Published Tue, Oct 14 2008 3:15 by Matthieu MEZIL

Comments

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

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..?

Tuesday, October 14, 2008 9:42 PM by Kristofer

# re: 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 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.

Wednesday, October 15, 2008 3:00 AM by Matthieu MEZIL

# re: 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 am also having the same error. but it isnt resolved even after changing the exe version

Wednesday, October 15, 2008 7:58 AM by abc

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

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

Thursday, October 16, 2008 7:01 PM by Kristofer

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

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

Wednesday, October 28, 2009 3:47 PM by Jonathan (SMT)

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

This bug is now fixed (with VS 2010 Beta)

Sunday, November 08, 2009 8:46 AM by Matthieu MEZIL

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: