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?
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
© 2022 - 2024 — McMap. All rights reserved.