Wednesday, July 22, 2009

Consume .Net DLL in Classic ASP Script

I've been fighting this one all day, and I think I've finally got it figured out. I use XML web services all the time. But without a lot of crazy SOAP calls you can't use them in a Classic ASP script. My solution is to make a DLL using VB.Net that will call the web service, pass the same parameter values, and return the results back to the calling app. Seems simple, right? Here's the breakdown:

1. Create the XML Web Service. Add your exposed web methods.

2. Create a new vb.net Class Library type project. This will create the DLL. I use VS.Net 2003 and 2008 on the same dev machine. The web server I am installing this one on is running a Classic ASP web site, and IIS is configured to use the .Net framework 1.1. Also, RegAsm and GACUtil only exist on this server for v1.1, so I chose VS 2003 to build the DLL

3. Set a web reference to the web service, set the URL Behavior to Dynamic, be sure the URL is accessible from the live web server whether it be IP address or www something.

4. Use this code to build your public exposed methods:

Public Function MyMethod(ByVal psPass As String) As String
Dim sReturn As String = ""
Dim oSend As New Webservice.webserviceClass
sReturn = oSend.Method1(psPass)
Return sReturn
End Function

And repeat as needed for each web method you need to expose from the web service.

5. Right click on the DLL project, and go to Properties. This should show you the assembly property pages. On the Configuration --> Build page, check the box to Register for Com Interop. In VS 2008 it's in the project properties --> Application tab --> Assembly Information button --> check box says "Make assembly COM visible".

6. Build your DLL

7. Open the VS.Net Command Prompt that came with your version of VS. Enter the command:

sn -k "C:\myDLL.snk"

This creates what's called a strongly typed name for your assembly. You'll need that snk file later when we go to deploy.

8. Add the strong name into your DLL. Open the AssemblyInfo.vb file and add the line:

Blogger doesn't want me to display this line, so i'm going to write it out vertically.

<
A
s
s
e
m
b
l
y
:

A
s
s
e
m
b
l
y
Key
File
("
c:\
MyDLL
.snk
")
>

Build your DLL again. Note the spaces in the vertical list. blogger doesn't make it very easy to post code in here that can be interepreted.

9. If VS.Net builds all of that without errors then we are ready to deploy to the web server. 2003 does not care about the config file. So minimally you will need to copy the DLL and the snk file up to the web server. My web server is Windows 2003 Server with all versions of the .Net framework installed. Everything else has to be done FROM THE SERVER CONSOLE. I put the DLL in a folder that is not behind the web server for security reasons. And I put the snk file on the root of C: just like it was on the dev workstation.

10. Open up a command prompt. Get into the C:\Windows\Microsoft.Net\v1.1.4322\ folder

11. Register the assembly using RegAsm:

regasm /tlb c:\dll\mydll.dll

This registers the dll with windows. This must be done without errors coming back before you can get any further. You must use the same version of regasm as you used to build the DLL.

12. Load the assembly into the Global Assembly Cache (GAC):

gacutil /i c:\dll\mydll.dll

If this comes back without an error you are good to go. Again, gacutil must be from the same .net version as you used to build the dll.

13. Call the DLL from the ASP Script

<
%
set oSend = createobject("MyDLL.Class1")
str = oSend.MyMethod(sPass)
set oSend = nothing
response.write(str)
%
>

Now everything has done it's job. Your asp script called the dll, which called the web service, which did it's thing. Then (in this example) we sent a string back to the DLL, sent it back to the script and wrote it out to the browser. that's all it takes!

1 comment:

  1. OOooooh, boy! I don't think I could keep up with this blog!

    ReplyDelete

thanks for the comments!