Showing posts with label .net dll. Show all posts
Showing posts with label .net dll. Show all posts

Wednesday, April 04, 2012

Working with .net dll Object in ColdFusion

Usually in a ColdFusion Application we do all the logic implementation using ColdFusion. There is no need of any other server side language. But sometimes we need to access the library created by some other languages like .net, java etc . ColdFusion runs on java platform so there is no issue in consuming the java library. The question here is how we can use the .net library.

The answer is by creating a "dll" in .net. Then ColdFusion will access the methods of the dll file by creating Object of that dll.

Example:
Now, I am going to create a Calculator library in .net(a dll file) which will have two methods addition and subtraction using the following .net code.

using System;
namespace Calculator
{
    public class Calculator
    {
        public int addition(int a, int b)
        {
            return a + b;
        }

        public int subtraction(int a, int b)
        {
            return a - b;
        }
    }
}


Then, I wrote the following lines of ColdFusion code to create object of that ".dll" file.

 <cfscript>
   variables.dotNetObject = createObject("dotnet", "Calculator", "#expandPath('./Calculator.dll')#").init();
   WriteDump(variables.dotNetObject);
</cfscript>

But, I got the one error message:

"DotNet Side does not seem to be running. Ensure that the DotNet agent is running and you have provided the correct host and port information "



Solution:

1. Go to the window services viewer and see whether  "ColdFusion  .NET Service" service is running or not. If not running then start the service. If the service is not Present then go to step 3.
2. After that check whether the code is again throwing the same error or not.
3. If it again throwing the same error then go to "http://help.adobe.com/en_US/ColdFusion/9.0/Installing/WSc3ff6d0ea77859461172e0811cdec18969-7ff1.html" and follow the instructions to reinstall/install the ColdFusion .net Service.

After, following  all the instructions and reinstalling the ColdFusion .net Service I again run the code. I got the following error:

"Class Calculator not found in the specified assembly list.The assembly that contains the class must be provided to the assembly attribute. "



Solution:-

In the above ColdFusion code, in the function createObject we are passing the class name as "Calculator" in the argument. Our class name in the above .net code is aslo "Calculator". Then what is the cause of the problem???

While creating a .net class we have to specify the name space of that class, if you are not specifying the name space then the Project will be taken as the default name space.
So, in the .net code our name space will be "" and during accesing the class we must have to sppecify the name space like this:"<name-space>.<class>".
In ColdFusion code instead of passing "Calculator" we have to pass "Calculator.Calculator". The out put of the Dump will be as follow:



The dump shows the object have two function addition and subtraction. Now, we can perform the operation like general ColdFusion Object.

NOTE: If you want to download the "Calculator.dll" then you can download it from this url:

https://docs.google.com/open?id=0B2GDR5_Jv000NUR2ZXMxeWhTbm0yQWZ0NUozVjRkZw



Followers