I recently spent some time reacquainting myself with the language I first cut my teeth on twenty years ago and even got recertified again. The last time I held a ColdFusion certification, it was still owned by the Allaire brothers (almost 20 years ago). :D
ColdFusion is an excellent language for rapid application development, but sometimes it falls a little short. Many developers call Java in instances like that, but I prefer to call out to .Net because it is my preferred development language and considerably faster. Most ColdFusion developers have noticed the “ColdFusion .Net Service,” but either has no idea what it is or how to use it. So, I’m writing this straightforward tutorial to show you how simple it is to call .Net assemblies from ColdFusion!
I began this tutorial by trying to consume a .Net 6 class, then downgraded sequentially from there. It would appear that the underlying component (JNBridgePro) that provides for .Net/ColdFusion (Java) interoperability only supports .Net Framework versions 1.0–4.x. I’ve reached out to Adobe to confirm this. A COM visible assembly may do the work with jIntegra. I’ll do more research and will write about that later. Nevertheless, .Net 4.x is also very powerful, installed on most servers, and can be incredibly useful, so I’ll continue with this tutorial.
The following code will tell you what version of .Net JNBridgePro is configured to use:
This example will call a simple .Net class library that pings a host and returns a boolean up/down state. I’ve also included a C# function that returns the entire PingReply object, which is returned as a CF struct.
As long as you have installed the ColdFusion .NET Service, you shouldn’t need to do anything special to make this code work. ColdFusion/JNBridge does all the heavy lifting for you behind the scenes.
First, you will want to fire up Visual Studio and create a .Net Framework Class Library (not .Net Core or later). I called my project CoderPro.Blog.Networking, but you can call yours whatever you like. Now create a class called Connectivity and paste the following:
That is all you need from the .Net side. Now we only need to build the project C# project and consume it from ColdFusion, which is remarkably easy.
As always, you’ll want to start with your Application.cfc component.
I always like to separate my presentation, business logic, and data access layers. So, next, you’ll want to create a file called /components/networking.cfc. That will look like the following:
Last but not least, you will want to display the results of the function. I do this in the /index.cfm file in the root of the ColdFusion application. This simply consumes the component created in the last step. I’ve provided the cfScript and CFML code required for this to work.
As I mentioned, there is another .Net function called PingHost that I don’t show you how to implement. From my own experience, I know that we learn nothing unless we do things for ourselves, so I left that for you to experiment with.
You can find all of the .Net code from this blog post here:
You can find the ColdFusion side of this code here: https://github.com/coderpros/blog/tree/master/ColdFusion/calling-dotNet
As always, feel free to reach out if you have any questions & happy coding!