none
Using methods of a .net 4.0 dll by .net 3.5sp1 client.

    Question

  • Hi everyone,

    I've writed a dll using .net 4.0 to wrap a java service to the .net world, and I can't retarget it to a lower framework version. Now I need to use that dll into an existing project writed (and not upgradable to .net 4.0) in .net 3.5sp1. If I add a reference in a test app written in 3.5sp1, VS2008 arises an error (can't reference the dll). Can I do it ?

    Thanks in avance,

    Gianni Ghezzi.

    Tuesday, September 14, 2010 1:46 PM

Answers

  • Hi,

    Thanks for your post. If I understood it right, you want to use a DLL which is targetd at .NET 4.0 within an application which is targeted at .NET 3.5.

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

    Project 1 (HighFX):

          - targeted at .NET 4.0

    Project 2 (LowFX):

          - targeted at .NET 3.5

          - reference the 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 saying that the HighFX project couldn't be referenced because it is targeted at a higher version of .NET framework. This is a safe guard feature and it's by design.

        However, there's a work around.

             Step1: Unload the referencing project LowFX targeting at the .NET 3.5

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

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

     <ProjectReference Include="..\HighFX\HighFX.csproj">
     <Project>{72A92BA2-ECE2-4065-93B5-826E61DDD498}</Project>
     <Name>HighFX</Name>
     <SpecificVersion>true</SpecificVersion>
     </ProjectReference>
    

             Step4: Reload the project.

         Adding the above line would allow you to build the solution within the Visual Studio 2010, there is still a warning as below, but it won't stop the building.

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

           Step5: Add an App.config file to the LowFX project. And add the following content.

    <?xml version="1.0"?>
    <configuration>
     <startup>
     <supportedRuntime version="v4.0"/>
     </startup>
    </configuration>
    

    Then you can build your solution and run it.

    Here is a similar thread. In that thread, the op wanted to use a 3.5 assembly in 2.0 application. I offered the same solution but without bothering an App.config file in step 5. Because both .NET 3.5 and .NET 2.0 use the CLR 2.0. So it's not necessary to point it out. But .NET 4.0 use CLR 4.0, so in your case, we have to use CLR 4.0 to run both the HighFX and the LowFX assembly. Otherwise, as the LowFX starts to run, CLR 2.0 will be loaded, and the .NET 4.0 assembly HighFX will fail to load with it. The exception would be BadImageFormatException.


    Please mark the right answer at the right time.
    Thanks,
    Sam
    • Edited by SamAgain Wednesday, September 15, 2010 6:34 AM refine
    • Marked as answer by Gianni Ghezzi Thursday, September 16, 2010 10:38 AM
    Wednesday, September 15, 2010 6:25 AM

All replies

  • I don't think it will allow to use 4.0 version dll in 3.5 app referencees, as Frame work run time mismatch.
    Tuesday, September 14, 2010 8:51 PM
  • Hi,

    Thanks for your post. If I understood it right, you want to use a DLL which is targetd at .NET 4.0 within an application which is targeted at .NET 3.5.

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

    Project 1 (HighFX):

          - targeted at .NET 4.0

    Project 2 (LowFX):

          - targeted at .NET 3.5

          - reference the 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 saying that the HighFX project couldn't be referenced because it is targeted at a higher version of .NET framework. This is a safe guard feature and it's by design.

        However, there's a work around.

             Step1: Unload the referencing project LowFX targeting at the .NET 3.5

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

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

     <ProjectReference Include="..\HighFX\HighFX.csproj">
     <Project>{72A92BA2-ECE2-4065-93B5-826E61DDD498}</Project>
     <Name>HighFX</Name>
     <SpecificVersion>true</SpecificVersion>
     </ProjectReference>
    

             Step4: Reload the project.

         Adding the above line would allow you to build the solution within the Visual Studio 2010, there is still a warning as below, but it won't stop the building.

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

           Step5: Add an App.config file to the LowFX project. And add the following content.

    <?xml version="1.0"?>
    <configuration>
     <startup>
     <supportedRuntime version="v4.0"/>
     </startup>
    </configuration>
    

    Then you can build your solution and run it.

    Here is a similar thread. In that thread, the op wanted to use a 3.5 assembly in 2.0 application. I offered the same solution but without bothering an App.config file in step 5. Because both .NET 3.5 and .NET 2.0 use the CLR 2.0. So it's not necessary to point it out. But .NET 4.0 use CLR 4.0, so in your case, we have to use CLR 4.0 to run both the HighFX and the LowFX assembly. Otherwise, as the LowFX starts to run, CLR 2.0 will be loaded, and the .NET 4.0 assembly HighFX will fail to load with it. The exception would be BadImageFormatException.


    Please mark the right answer at the right time.
    Thanks,
    Sam
    • Edited by SamAgain Wednesday, September 15, 2010 6:34 AM refine
    • Marked as answer by Gianni Ghezzi Thursday, September 16, 2010 10:38 AM
    Wednesday, September 15, 2010 6:25 AM
  • Dear Sam,

    your reply is ok. In VS2010 I can build the project in .net35 with a reference to the dll written in .net40. So, can I do the same in VS2008? I try to reply the steps you provide to me but VS doesn't find the public methods provided by the .net40 compiled dll.

    Thanks in advance,

    Gianni Ghezzi.

    Thursday, September 16, 2010 10:38 AM
  • Hi,

    Thanks for your feedback. Though VS2008 supports multi-targeting, it doesn't support .NET 4.0. To use .NET 4.0, VS2010 is a must.


    Please mark the right answer at the right time.
    Thanks,
    Sam
    Friday, September 17, 2010 2:28 AM
  • Hi Sam,

     

    Thanks for the informative post. I have had no luck when I follow the above steps(tried 3 times to eliminate user error).  

     

    My HighFX is a 4.0 Class library, LowFX is 3.5 Windows Forms application. I still get the badformatimageexception at the end and my win app never runs successfully. 

    Any thoughts? 


    Thanks in advance

    Neelima


    Neelima
    Tuesday, March 08, 2011 11:28 PM