I want to call dll which is built using .NET 4.0 from C# project which should be built using .NET 3.5. I mean .net 4.0 dependent code should executed only when my machine has .net 4.0 installed otherwise just skip that part. Is this possible ( any workaround) ? If yes,then how it can be achieved?
-
4No, you can't. Why did you not try? – leppie Apr 16 '13 at 13:33
-
@leppie: I tried this and getting exception as "this assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded" So i am looking for any other way it can be possible – sagar Apr 16 '13 at 13:36
-
It is not possible. Why can't you compile the code for .NET 3.5? What special feature do you use? – leppie Apr 16 '13 at 13:39
-
@leppie: Actually some api requires .NET 4.0. Though i can build project using "NANT" with target framwork 3.5, but when call come to .NET 4.0 built dll, it throws above mentioned error. – sagar Apr 16 '13 at 13:44
-
Why do you require .NET 3.5? Why can't you compile everything with .NET 4, which'll remove this problem entirely? – thecoop Apr 16 '13 at 14:56
-
@thecoop: Because .NET 4.0 doesn't support some OS which are supported by .NET 3.5 – sagar Apr 17 '13 at 06:26
-
You can install .NET 4 on windows XP. Why would you want to go further back than that? – thecoop Apr 17 '13 at 08:28
1 Answers
Instead of adding the .NET 4.0 DLL to your references, wrap it into a service and call the service to access your methods. The service can be a HTTP/HTTPS service (such as MVC4 RESTful WebAPI, WCF), or a window service watching a drop folder or a SQL table.
[Edit]
Lets say you have a Windows Service (Project A) which is built using .NET 3.5.
Project A needs to call a method which resides in a class library (Project B) which is built using .NET 4.0.
With this setting you cannot add a reference to Project B from Project A (ProjectA ---x--> ProjectB)
The alternative are:
- Upgrade Project A to .NET 4.0 and then add reference to the Project B and call your method
Find a way that project A and Project B can communicate without adding a direct reference. Of course it comes with a cost but if you have no alternative it is the best way. You can do this in two ways:
2.1 HTTP API: I would go with MVC4 Web APIs http://www.codeproject.com/Articles/344078/ASP-NET-WebAPI-Getting-Started-with-MVC4-and-WebAP
2.2 Database queue: Project A would serialize and insert the requests into a shared Database (e.g. SQL) table and Project B keeps watching for new rows in that table. For each new row, it picks them up runs the desired functionality and stores the results in another table.
2.3 Drop folder: Project A would serialize and write its requests and parameters into a file and store it in a shared folder. The project B keeps looking for new files in that shared folder and as soon as it sees one, reads it, deletes it, and stores the results in the shared folder.
Disclaimer: My proposed solution is well suited for more than small projects in which a fairly large and important logic resides in .NET 4.0 project which cannot be ported into .NET 3.5. If you only need to run few lines of 4.0 code and you dont want to bother with large complexity then probably you need to rethink what you want to achieve in your .NET 4 project. You may want to check out How to reference .NET 4.0 assembly within .NET 3.5 projects
-
:I didn't understand exactly what u r suggesting. My .Net3.5 compiled project is actually windows service. Please tell me in detail. – sagar Apr 17 '13 at 06:23
-