none
Calling a .net 4.0 DLL from a .net 2.0 DLL? RRS feed

  • Question

  • Hi all;

    We have a commercial library we provide that is written in .NET 3.5 (ie 2.0 runtime). It needs to be using the 2.0 runtime because we have many customers still on 2.0 - 3.5.

    We have a driver that uses the OData library that first exists in .NET 4.0. We're presently using the source to it compiled using .NET 3.5 but we want to use the shipping version to get the latest bug fixes. So that means using .NET 4.0.

    Is there a way our 2.0 library can call a separate 4.0 dll with the OData code - assuming that the application calling our main dll is also .net 4.0? In other words:

    app(4.0).exe -> MainLibrary(2.0).dll -> ODataLibrary(4.0).dll

    ??? - thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Thursday, July 10, 2014 3:32 PM

Answers

  • Is there a way our 2.0 library can call a separate 4.0 dll with the OData code - assuming that the application calling our main dll is also .net 4.0? In other words:

    Short answer: No, you cannot reference a .NET 4 assembly from a .NET 2 assembly but you can reference a .NET 2 assembly from a .NET 4 assembly.
    • Marked as answer by DavidThi808 Friday, July 11, 2014 7:54 PM
    Friday, July 11, 2014 12:56 PM
  • I think the best approach would be to simply build a 4.0 dll from the same code you build the 2.0 dll and add the OData related code only to the 4.0 dll. Customers will use one of the 2 dll depending on their target framework.
    • Proposed as answer by Ioana Vasilescu Friday, July 11, 2014 7:49 PM
    • Marked as answer by DavidThi808 Friday, July 11, 2014 7:54 PM
    Friday, July 11, 2014 2:21 PM
    Moderator

All replies

  • Hi David,

    I think you cannot do this. CLR is not forward compatible. Therefor assemblies of new CLR version won’t run on older CLR versions (1.x, 2.0, 3.0, and 3.5). Also note that 2.0 assembly can communicate with 4.0 assembly, but they have to run in the same CLR runtime (4.0 in this case).

    >app(4.0).exe -> MainLibrary(2.0).dll -> ODataLibrary(4.0).dll

    Starting with NET 4, you don’t have to worry about compatibility issues. It introduced a new feature called “In-Process Side-by-Side Execution”, which essentially allows you to load multiple versions of the CLR into the same process. See more from http://msdn.microsoft.com/en-us/library/ee518876.aspx.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, July 11, 2014 2:03 AM
    Moderator
  • Hi Herro;

    The link you sent me was for managed COM components. We have plain old CLR code - no COM. And based on your reference of side-by-side, it seems to me that our use case may be supported.

    Is there any information on how to configure side-by-side when we're running a 4.0 exe on .net 4.0?

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Friday, July 11, 2014 12:46 PM
  • Is there a way our 2.0 library can call a separate 4.0 dll with the OData code - assuming that the application calling our main dll is also .net 4.0? In other words:

    Short answer: No, you cannot reference a .NET 4 assembly from a .NET 2 assembly but you can reference a .NET 2 assembly from a .NET 4 assembly.
    • Marked as answer by DavidThi808 Friday, July 11, 2014 7:54 PM
    Friday, July 11, 2014 12:56 PM
  • Friday, July 11, 2014 12:59 PM
  • In theory you could use reflection to load and execute methods in the OData library, I'm not familiar with that library to tell if this is feasible or not.

    That said, I don't understand you're restriction to the 2.0 runtime. In your post you seem to indicate that the application is using 4.0, not 2.0.

    Friday, July 11, 2014 1:55 PM
    Moderator
  • We have customers running on .net 2.0, others on .net 4.0. It would be ok to say no OData unless the exe is 4.0 - if that made a difference.

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Friday, July 11, 2014 2:14 PM
  • I think the best approach would be to simply build a 4.0 dll from the same code you build the 2.0 dll and add the OData related code only to the 4.0 dll. Customers will use one of the 2 dll depending on their target framework.
    • Proposed as answer by Ioana Vasilescu Friday, July 11, 2014 7:49 PM
    • Marked as answer by DavidThi808 Friday, July 11, 2014 7:54 PM
    Friday, July 11, 2014 2:21 PM
    Moderator