Attention: We have retired the ASP.NET Community Blogs. Learn more >

Contents tagged with Visual Studio 2010

  • Interop type 'AssemblyName.ClassName' cannot be embedded

    I recently upgraded some Visual Studio 2008 projects to Visual Studio 2010.  Most everything went very smoothly but I did run into one issue with some COM assemblies that were part of the project.

    At first the ASP.NET Web Application project built without issues, but then I ran into this error when visiting one of the pages:

    Could not load file or assembly 'Interop.AssemblyName' or one of its dependencies. An attempt was made to load a program with an incorrect format.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details: System.BadImageFormatException: Could not load file or assembly 'Interop.AssemblyName' or one of its dependencies. An attempt was made to load a program with an incorrect format.

    My solution to this issue was to remove the COM Assembly from the Visual Studio references and then to add it back in.  (I am not sure this was the proper solution but it worked.)

    But then when I added the COM assembly back into the project as a reference I could not build the project with errors such as:

    Interop type 'AssemblyName.ClassName' cannot be embedded. Use the applicable interface instead. 

    And others indicating that the project was not seeing the reference to the COM assembly.  Luckily this StackOverflow question (Interop type cannot be embedded) had the answer.

    The solution is to change a property on the referenced COM assembly.  To do this, in your Visual Studio 2010 project expand the References section and right-click on the COM assembly and select Properties.  This will bring up the Properties dialog for the COM assembly reference.

    2011-11-03 06h42_17

    Just change the “Embed Interop Types” property from True to False.  Now just rebuild your project and everything will be working.

     

    UPDATE (2011-12-13): The above solution to remove the assembly and then add it back and then set the Embed Interop Type to false does resolve the issue but the problem is that after a Visual Studio 2008 project is upgraded to Visual Studio 2010 there is not EmbedInteropTypes element in the COMReference node in the Visual Studio project XML.  And also there is not a PlatformTarget node in the build configuration (see next item).

    By default the Visual Studio Properties Dialog shows no value as false.  The missing element is probably causing an issue elsewhere during the compilation.  The easy fix, instead of removing and adding the COM Interop assemblies back in, is to just toggle the Embed Interop Types property from False to True and then back to False.  This will put the element in the COMReference properties (as shown below in the screenshot) and then everything else will continue to work.

    image

    In addition I have found that you need to toggle the Platform Target:

    image

    Just change it to one of the other values and then change it right back to “Any CPU”. This will insert the appropriate PlatformTarget element in the project file (see screenshot below).  Make sure you do this for all build configuration (and not just the debug).

    image

  • Visual Studio Shortcut: Surround With

    I learned a new Visual Studio keyboard shortcut today that is really awesome; the “Surround With” shortcut. 

    You can trigger the Surround With context menu by pressing the Ctrl-K, Ctrl-S key combination when on a line of code.

    Ctrl-K, Ctrl-S means to hold down the Control key and then press K and then while still holding down the Control key press S.

    Here is where this comes in handy:

    You type a line of code and then realize you need to put it within an if statement block. So you type “if” and hit tab twice to insert the if statement code snippet.  Then you highlight the previous line of code that you typed, and then either drag and drop it into the if-then block or cut and paste it.  That is not too bad but it is a lot of extra key clicks and mouse moves.

    Now try the same with the Surround With keyboard shortcut.  Just highlight that line of code that you just typed and press Ctrl-K, Ctrl-S and choose the if statement code snippet, hit tab, and POW!... you are done!  No more code moving/indenting required.

    Here is what the Surround With context menu looks like:

    image

    Just up or down arrow inside the drop down list to the code snippet that you want to surround your currently selected text with.  Did I mention this is AWESOME!

    Now it is so simple to surround lines of code with an if-then block or a try-catch-finally block... things that usually took several key clicks and maybe one or two mouse moves.

    And this works in both Visual Studio 2008 and Visual Studio 2010 which means it has been around for a long time and I never knew about it.