Consume a .NET Assembly from a classic ASP page.
Whenever I think that I'm getting to the bottom of .NET, there's, well: there's always something to remind me...
http://users.cis.net/sammy/remindme.htm ( open in new window while you continue reading for the full effect )
I haven't really done a lot with strongly named assemblies or interop so, this morning I thought that I'd be bold and try a little experiment; I thought that I'd create a .dll in Visual Basic .NET and consume it from a classic ASP page - seems pretty trivial, so off I went:
1) Open New Project named “SimpleDLL” - assembly name “SimpleDLL“, namespace “SimpleDLL“
2) Create a class named “SimpleClass”
Public Class SimpleClass Public Function WriteName(ByVal name As String) As String Return name End Function End Class
3) Register the assembly in the Registry
4) Register it for COM
regasm /tlb SimpleDLL.dll
Voila! A quick check of the HKEY\LocalMachine\Software\Classes\ tells me that I had (at least) some level of success and that the Assembly is, indeed in the registry. So, off I go to create my classic asp page and consume it. So, again...
5) Open Visual Studio
6) Create SimplePage.asp
7) Type the following into a page named SimplePage.asp:
Dim foo Set foo = Server.CreateObject("SimpleDLL.SimpleClass") Response.Write foo.WriteName("blah")
Sure enough, it didn't work.
The page cannot be displayedError Type: |
After a bit of head scratching it was apparent that the source of my problems was that there was not enough information for the file to be found. Therefore I decided that I needed to create a strong name for my assembly and register it in the GAC
8) Generate a public/private key pair
sn -k MarkItUp.key
9) Add the attribute to my assembly for registering it:
<Assembly:
AssemblyKeyFile("C:\MarkItUp.key")>
10) Re-build the assembly
11) Install it into the GAC
gacutil /i SimpleDLL.dll
12) Re-install it into the registry
Fire-up the asp page.
Did it work? Yep :-) Amazing eh? Now I just have to work out how to get that type library information into the registry so that I can also get intellisense working while coding the asp page.
Finally, because I was having such luck I decided to call the Assembly from a Sql Server Stored Procedure too. That also worked first time!
DECLARE @object int DECLARE @hr int DECLARE @return varchar(255) EXEC @hr = sp_OACreate 'SimpleDLL.SimpleClass', @object OUT EXEC @hr = sp_OAMethod @object, 'WriteName', @return OUT, 'This is the text' PRINT @return -- Displays "This is the text" EXEC @hr = sp_OADestroy @object