0

I've a C# class with CorePoll namespace, I compiled it to .DLL file and put it inside bin folder of website. But I cannot use it. Imports CorePoll returns Could not load file or assembly 'CorePoll' or one of its dependencies. An attempt was made to load a program with an incorrect format. It's my class file in C# http://pastebin.com/JkdrnXyT
enter image description here

Maysam
  • 7,246
  • 13
  • 68
  • 106
  • What does the library depend on? If it depends on other dlls, you need to place them too in the in `bin` directory. – Oded Dec 12 '12 at 13:02
  • I do not have additional dlls, I created an empty class then paste this code into the .cs file – Maysam Dec 12 '12 at 13:03
  • When version of .NET did you compile the C# class with? Is it the same version the site runs under? – Oded Dec 12 '12 at 13:04
  • 3
    make sure there aren't any differences in platform and/or .Net version when building. Both should point to the same target platform (x86, x64, ...). Check the C# code first and make sure your VB uses the same build settings. – Wim Ombelets Dec 12 '12 at 13:07
  • If the classes in the CorePoll namespace have references to external assemblies (3rd party libraries, libraries you've written, etc.) you will need to put those assemblies (dlls) in the bin directory as well as the dll that you compiled. – Kevin Dec 12 '12 at 13:34
  • It refs are: Microsoft.CSharp, System, System.Core, System.Data, System.Data.DataSetEstention, System.Xml – Maysam Dec 12 '12 at 13:47
  • @Wimbo the website is not solution. – Maysam Dec 12 '12 at 14:08
  • @JeffBridgman Yes it is in IIS – Maysam Dec 12 '12 at 15:06
  • Does the solution to this questin help? http://stackoverflow.com/questions/41449/i-get-a-an-attempt-was-made-to-load-a-program-with-an-incorrect-format-error-o – Kratz Dec 12 '12 at 15:21
  • Yup, that was just was about to suggest (Kratz's comment). You may need to enable 32-bit applications in IIS. – Jeff B Dec 12 '12 at 15:38
  • I face error in the IDE, I cannot compile it – Maysam Dec 12 '12 at 15:46

2 Answers2

3

The message An attempt was made to load a program with an incorrect format., typically comes from mixing 64/32 bit environments. When you build your .DLL, make sure to select Any CPU as the Target CPU int the build settings. That way, the DLL should work no matter if the website is running 32 or 64 bit. But be aware, the message also states Could not load file or assembly 'CorePoll' or one of its dependencies., meaning that if you've referenced other libraries in your DLL, they could be failing to load as well.

Edit: After looking at the documentation, another potential cause of this exception is mixing projects built with different versions of the .Net Framework. Is this possibly causing your error?

Components have been created using different versions of the .NET Framework. Typically, this exception occurs when an application or component that was developed using the .NET Framework 1.0 or the .NET Framework 1.1 attempts to load an assembly that was developed using the .NET Framework 2.0 SP1 or later, or when an application that was developed using the .NET Framework 2.0 SP1 or .NET Framework 3.5 attempts to load an assembly that was developed using the .NET Framework 4. The BadImageFormatException may be reported as a compile-time error, or the exception may be thrown at run time.

Kratz
  • 4,280
  • 3
  • 32
  • 55
  • I've set Any CPU and I have no 3rd party libraries, but it still does not work. Uploading the solution files will help you? – Maysam Dec 12 '12 at 15:11
  • The only thing I've ever seen cause that error message is the 32/64 bit thing. The only other thing is if the file is somehow being corrupted when you upload it to the website, but that is doubtful. – Kratz Dec 12 '12 at 15:19
  • Both projects are set to Any CPU and both are running in the localhost – Maysam Dec 12 '12 at 15:22
  • I looked at the documentation for `BadImageFormatException` and it seems like there are a couple other potential causes. See my edit. – Kratz Dec 12 '12 at 18:50
0

None of answers helped me, I converted the C# code to VB.NET and used it with no problem, thanks for your time.

Maysam
  • 7,246
  • 13
  • 68
  • 106