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!

Bond's nerdy contra-personality

Greetings fellow nerds. I am a full time software developer, and I am starting this blog to post code and hurdles that I encounter in daily programming. I seem to find myself solving the same problems again and again. Instead of re-inventing the wheel every time, I want to create posts about solving a specific problem. Blogger lets me search through the posts so I can easily find my solutions.

Originally, I started doing the same concept on my site for a company I used to own. www.flynntechnology.com has a "Tips" section, where I would put up tips for each language I regularly write. there's maybe 30 tips in there, but the navigation and depth of the whole thing made it take about an hour for each tip I wanted to put up. Here, I can just copy some code, provide a brief explanation about what I am trying to do, and publish the answer. Much easier.

I am a triathlete by hobby. My training blog is at http://trainingsmoker.blogspot.com and I post funny, inspiring, and sometimes strange stories about my running, cycling, and triathlon adventures. Plus I talk about the family there, and it's supposed to be very personable. This blog, by contrast, should have no family pictures or social drive. Just the code, ma'am.

So check over to the Smoke Training blog if you want to know more about me. Comment on any of these posts if you have programming questions or get stuck with something. I'll help out when I can. I will tell you this about myself:

I am a huge James Bond fan. As much as Bond is a general badass, men love him, ladies want to be with him... I consider myself the geek equivalent. Other geeks want to write as many languages as I do. My wife wants me to step away from the laptop every now and then. If you picked up the fact that 00111 was a Bond reference, you are as big of a geek as I am. 111 is the number 7 in binary. We are going to be good friends.