locked
Stop VS 2010 build errors when referencing newer framework dlls RRS feed

  • Question

  • Hi,

    Hope this is the correct forum.

    We develop applications on AutoCAD. Each version of AutoCAD use a different .NET Framework version dependent on the release dates current Framework version. As the application dll is loaded into AutoCAD it must be compiled on the same Framework version or lower as that AutoCAD requires.

    Newer versions of AutoCAD have new functionality that we can conditionally use via reflection to check AutoCAD version. In order to use the AutoCAD API we simply reference 2 dlls (names do not change per version)

    In VS 2008 we could compile a dll targeted at 2.0 (so it would load on any AutoCAD) that referenced the latest AutoCAD API dll that are targeted at 3.5. ie myapp.dll(2.0) referenced acdbmgd.dll(3.0). A warning would be given but it would build in the VS IDE.

    In VS 2010 this is not allowed. When building in the VS IDE it fails with warning. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3268: The primary reference "acdbmgd, Version=18.0.0.0, Culture=neutral, processorArchitecture=MSIL" could not be resolved because it has an indirect dependency on the framework assembly "PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v2.0". To resolve this problem, either remove the reference "acdbmgd, Version=18.0.0.0, Culture=neutral, processorArchitecture=MSIL" or retarget your application to a framework version which contains "PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" and errors that all the functions/types used in the api dll are not defined.

    Strangely, also I can add the reference (albeit with the same warning about framework versions as VS 2008) and there will be no problem with the intellisense so it can read the API dll - it just fails at build.

    I can build the whole thing targeted at 2.0 (if I remove the newer functionality) and then load it into a newer AutoCAD so this proves at runtime that a 2.0 can access a 3.0 dll - it's just VS 2010 being picky at build time. 

    Now I know that VS is just trying to help :-). It's trying to make sure it doesn't crash at runtime as the newer framework may not be available. I, however, know what I am doing (debatable) so is there a way that I can turn off this particular check.

    Thanks in advance,

    Adam

     


    Adso
    Wednesday, April 14, 2010 10:41 AM

Answers

  • Hi AdsDavis,

        In order to repro your scenario, I create 2 projects in 1 solution as below:

        Project 1 (HighFX):

          - targeted at .NET 3.5

          - use some assemblies from .NET 3.0

        Project 2 (LowFX):

          - targeted at .NET 2.0

          - reference proejct 1

            (in fact, while adding the reference, Visual Studio asked me if I really want that, this is true for both VS2008 and VS2010)

        When using the Visual Studio 2010 Build command from the menu to build this solution, there will be warnings and errors as your described. This feature is a safe guard feature and it's by design.

        However, there're 2 workarounds.

        Option 1:

             Step1: Unload the referencing project targeting .NET 2.0

             Step2: Right click the unloaded project and select edit from context menu

             Step3: Add <SpecificVersion>true</SpecificVersion> to the reference. Below is a sample from my repro solution:

        <ProjectReference Include="..\HighFX\HighFX.csproj">
          <Project>{8DD71CAF-BEF7-40ED-9DD0-25033CD8009D}</Project>
          <Name>HighFX</Name>
          <SpecificVersion>true</SpecificVersion>
        </ProjectReference>

             Step4: Reload the project.

         Now your should be able to build within the Visual Studio 2010, there could still be a warning as below, but the build can be successful.

    Warning 1 The project 'XXX' cannot be referenced.  The referenced project is targeted to a higher framework version (3.5) 

        Option2:

        Use the command line tool csc.exe to build each of your source file, which won't prevent the building.

     

        For additional information, please refer to here:

        .NET 2.0 target project can not refer to .NET 3.5 target project

        Best Practices For Creating Reliable Builds, Part 1

        Best Practices For Creating Reliable Builds, Part 2

     


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Proposed as answer by Nancy Shao Thursday, April 15, 2010 6:35 AM
    • Edited by SamAgain Thursday, April 15, 2010 6:37 AM refine
    • Marked as answer by AdsDavis Thursday, April 15, 2010 10:31 AM
    Thursday, April 15, 2010 4:16 AM

All replies

  • Hi All,

    As is always the case when I post a question I find that on re-reading I don't think I have I have described my problem particularly well!!!

    Essentially I must compile the main dll as 2.0 (for it to work in older versions of AutoCAD) - this 2.0 dll then references a 3.0 dll. VS 2008 would allow this; VS 2010 will not allow this.

    So how can I switch off the seemingly overzealous framework checking of the VS 2010 compiler?

    Adam


    Adso
    Wednesday, April 14, 2010 10:21 PM
  • Hi AdsDavis,

        In order to repro your scenario, I create 2 projects in 1 solution as below:

        Project 1 (HighFX):

          - targeted at .NET 3.5

          - use some assemblies from .NET 3.0

        Project 2 (LowFX):

          - targeted at .NET 2.0

          - reference proejct 1

            (in fact, while adding the reference, Visual Studio asked me if I really want that, this is true for both VS2008 and VS2010)

        When using the Visual Studio 2010 Build command from the menu to build this solution, there will be warnings and errors as your described. This feature is a safe guard feature and it's by design.

        However, there're 2 workarounds.

        Option 1:

             Step1: Unload the referencing project targeting .NET 2.0

             Step2: Right click the unloaded project and select edit from context menu

             Step3: Add <SpecificVersion>true</SpecificVersion> to the reference. Below is a sample from my repro solution:

        <ProjectReference Include="..\HighFX\HighFX.csproj">
          <Project>{8DD71CAF-BEF7-40ED-9DD0-25033CD8009D}</Project>
          <Name>HighFX</Name>
          <SpecificVersion>true</SpecificVersion>
        </ProjectReference>

             Step4: Reload the project.

         Now your should be able to build within the Visual Studio 2010, there could still be a warning as below, but the build can be successful.

    Warning 1 The project 'XXX' cannot be referenced.  The referenced project is targeted to a higher framework version (3.5) 

        Option2:

        Use the command line tool csc.exe to build each of your source file, which won't prevent the building.

     

        For additional information, please refer to here:

        .NET 2.0 target project can not refer to .NET 3.5 target project

        Best Practices For Creating Reliable Builds, Part 1

        Best Practices For Creating Reliable Builds, Part 2

     


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Proposed as answer by Nancy Shao Thursday, April 15, 2010 6:35 AM
    • Edited by SamAgain Thursday, April 15, 2010 6:37 AM refine
    • Marked as answer by AdsDavis Thursday, April 15, 2010 10:31 AM
    Thursday, April 15, 2010 4:16 AM
  • Sam,

    You are a star. Thanks so much for your detailed response.

    Setting <SpecificVersion>true</SpecificVersion> works great.

    A note of caution to other users who come across this as a solution. When referencing a HighFX DLL that is not in the current solution setting the SpecificVersion to True in the Properties window of the Referenced dll in VS actually removes the SpecificVersion line from the proj file. You then have to follow the steps detailed above to get it to stick.

    A note to Microsoft. This is a valid workaround, however, in my reasonably small project I had to spend several hours adding this line to any referenced dll that had a reference to a HighFX dll no matter how many reference levels away.

    ie Project1 (2.0) references Project2 (2.0) references Project3 (3.0). SpecificVersion has to be set on both Project3 within Project2.csproj and Project2 within Project1.csproj.

    Could a Global SpecifcVersion override be made? I understand it is by design to stop us getting in trouble but it helps less experienced users of VS at the expense of power users.

    Thanks for listening and thanks again to Sam


    Adso
    Thursday, April 15, 2010 10:31 AM
  • Sam, I tried to vote your reply as helpful but I just get unexpected errors on the web page :-(
    Adso
    Thursday, April 15, 2010 10:34 AM
  • Hi AdsDavis,

       Thanks for your response. I'm glad this problem is solved.  8^D


    Please mark the right answer at right time.
    Thanks,
    Sam
    Thursday, April 15, 2010 12:30 PM
  • Hi,

    I know its an old thread but...

    I used this solution and it worked prefectlly.

    We use this solution like this:

    Project_1 - 3.5

    Project_2 - 2.0 referenced to Project_1

    Project_3 - 2.0 referenced to Project_2

    Project_4 - 2.0 referenced to Project_2

    My question is if there is no framework 3.5 installed on the machine where the exception will occur?

    1. In one of the following projects: Project_3, Project_4

    2. In Project_2

    3. In Project_1

    Sunday, December 5, 2010 11:28 AM