views:

623

answers:

6

I'm running into some problems using my .NET 4.0 libraries in .NET 2.0 applications. I guess I was under the impression that being a Windows DLL, my other .NET apps would be able to access it. Is this not the case? Any recommendations in terms of supporting applications in both environments?

EDIT: I realize that I'd need to install the .NET 4.0 Framework on the target system, are there other reasons why this won't/shouldn't work?

EDIT: Probably should have been more specific. We have a current, large/complex, application written in .NET 2.0 (ASP.NET be exact). We have a new set of integration tools and workflow items we are writting in .NET 4.0. We wanted to add one of the 4.0 created libraries to the .NET 2.0 project (currently in VS2008) and make use of a few of it's methods. When we do this we run into problems, often cryptic errors relating to memory.

It would appear that both Earwicker and Brian Rasmussen are correct in the end. As I'm not too keen on exposing things via COM (not sure if this is technically COM or not but regardless) I think I will stick to the idea that the two are not 'compatible' and look to other means, which I think we have based on our specific needs. Longer term we will look to move the .NET 2.0 code up to 4.0.

+7  A: 

Please note that version 4.0 is more than just additional assemblies. The runtime itself has also been changed in this version (new concurrent GC mode, lots of changes to the thread pool, mscorwks.dll is now called clr.dll, etc.).

Brian Rasmussen
So I can't compile my .NET 4.0 library in some way that will expose itself as a traditional Windows DLL? The target workstation would have the .NET 4.0 Framework installed, that's not enough?
Douglas Anderson
@Douglas Anderson - yes, you can.
Daniel Earwicker
@Douglas Anderson: As Earwicker points out 4.0 supports loading different runtimes side by side (there's a recent MSDN magazine article on the subject). However, it wasn't to me what you were trying to do, so I just wanted to point out that unlike the previous versions the runtime has been updated as well for this version.
Brian Rasmussen
I've added some further edits that better specify my needs and why this to me is the answer I want but technically not the 'correct' answer.
Douglas Anderson
+1  A: 

i don't believe this is possible. Perhaps a MSIL to Native converter/compiler would help, but i don't have any experience with these.

henchman
A: 

Your assembly compiled for .NET 4 will contain references to other .NET 4 framework libraries that are not present in .NET 2.0.

If you want to have your applications compatible with .NET 2.0 you can use Visual Studio 2005 or target your projects to .NET 2.0 if you are using Visual Studio 2008 or 2010. Of course, if you target your projects to .NET 2.0 you will not be able to take advantage of .NET 3.5/4 features.

Daniel Melo
+2  A: 

It may be possible depending on how you are using it. In .Net 4.0 you have the option of In Process Side by Side Execution (InProc SxS). This allows you to host two different versions of CLR in the same process space. This is explained here.

Whether you can take advantage of this in your situtaion I don't know. I have no direct experience to help you.

Some scenarios in which this can be used.

Pratik
+4  A: 

Yes, this is perfectly possible. You just expose the components written in 4.0 as COM objects. The 2.0 hosting application just uses them as COM objects and has no idea whether they are native, 2.0, 4.0 or whatever. COM is the common interface that both runtime versions have to implement identically.

The new in-process SxS support in 4.0 means that when the 4.0-based COM object is loaded, it pulls in the required runtime instead of trying to run on the 2.0, so both runtimes are present in the process managing their own objects. Although you can't directly pass CLR objects between them, you can pass COM interfaces, and the CLR transparently wraps your objects in COM interfaces for you.

I don't know if you can make a single interop assembly for both versions to work from. But clearly you could write an interop assembly in C# in 2.0, export it to a .tlb, and then import it into an assembly in 4.0. That gives you two matching interop assemblies describing identical COM interfaces. (Or just build the same C# source in each version's assembly project).

Bonus Update: Will the resulting application be COM-based (with whatever problems that entails)?

It depends how you look at it. Authors of components will be making them as COM components. So the host application needs to locate and load them as COM components. This means fooling around with the GAC, the registry or SxS manifests, which is a lot less clean than just telling component authors to drop their assembly in a certain directory so you can load it with reflection.

And it has an impact at runtime: when the host has a reference to a component, there will be not one but three objects involved. The host has a reference to a RCW, which has a pointer to a COM interface implemented by a CCW, which in turn holds a reference to the actual component. The CCW in the middle is a reference-counted COM object, and the host's RCW has a finalizer that calls Release on the CCW, and when it is destroyed it deallocates the GCRoot that is keeping alive the actual component.

This means that - with a sufficiently complicated arrangement of callback pointers, etc. - the system may end up with circular reference counting problems, where a disconnected "island" of objects are all holding references to each other and so they never get deallocated.

Daniel Earwicker
That was my thought exactly. I won't recommend this though, as it's not as intuitive as the 'normal' .NET way. You'd still need .NET 4 on your machine though, because the library you reference still uses it.
Erik van Brakel
When you say you wouldn't recommend it - what else would you recommend to someone who wants to support .NET 4.0 plug-ins to a .NET 2.0 base platform application? Or vice versa? That is exactly what this feature of 4.0 has been developed to support. There is no other "normal" way to do this - it's never been possible before.
Daniel Earwicker
One should consider to migrate the 2.0 part to .net 4.0, if .net 4.0 is needed anyway. with this way you don't have the footprints of both frameworks and can use the new features in your own codebase, too.
henchman
@henchman - of course. The purpose of SxS is for situations where assemblies are maintained by separate teams publishing versions on separate schedules. The classic example being an extensible platform application for which 3rd parties can author plugins, so there's a plugin ecosystem with lots of vendors adding value to the platform. You want to be able to bring out a new version of the platform and yet remain compatible with all the plugins in your ecosystem, without having to wait for all the 3rd party vendors to port their plugins to the new .NET framework.
Daniel Earwicker
I awarding the answer here as technically it's the 'correct' answer. In the end it's not the solution I will run with but thanks to all who provided insight and details.
Douglas Anderson
Thanks: I'm going to add a short note about whether the result of working this way is technically COM or not.
Daniel Earwicker
+1  A: 

Looks like the in-process side-by-side feature which was added to CLR 4 wouldn't help in this case since the he wants to use a .NET4.0 library in a .NET2.0 application. As far as I understand it, in-process SxS would be helpful if the case was the opposite (consuming a .NET2.0 in a .NEt4.0 application) as the CLR of the 4.0 will work side by side with 2.0 in the same process..

Derar