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



7 comments:

  1. Nice blog.. Thanks for sharing this blog information with us…

    ReplyDelete
  2. Hi,

    My environment: window server 20008, .net 3.5 installed, cf8 and also cf .net integration installed.

    I having the problem when try to call a basic DLL from my CF8. it showing me


    Object Instantiation Exception.

    An exception occurred while instantiating a Java object. The class must not be an interface or an abstract class. If the class has a constructor that accepts an argument, you must call the constructor explicitly using the init(args) method. Error : ''


    I able to view the class when using the CFDump, but when I try to call the method within the class, it showing error. Anything that I can look into for this problem?

    Thanks

    ReplyDelete
    Replies
    1. Can you please try with the attached the DLL file and let me know whether you are getting the same error or not.
      Otherwise if you could post your stack trace then it would help me to identify the exact issue.

      Delete
  3. Hi there,

    Sorry to bother you this much later. I am working fine upto the end of tutorial you have published. Then, after I instantiate the .net object, calling any function of it throws error: "Method Not Found". However, I can see that the specified method is listed in the cfdump of the object instance. Do you know what is causing this trouble? I will be waiting anxiously ... :P

    Thank you.

    ReplyDelete
    Replies
    1. Asad,

      Check if you are passing the correct argument type. If possible please share the code sample.

      Delete
  4. Dear Upendra,

    Thank you for this tutorial! It's been helpful. I am still getting the error "Class Calculator not found in assembly". I have double checked that I have the same code as your example shows as well. Any ideas on what to do? Do i need to add this dll to the GAC?

    ReplyDelete
  5. BlueHost is one of the best web-hosting company for any hosting plans you might need.

    ReplyDelete

Followers