I need to create a class that can be accessible through the ASP Classic's Server.CreateObject method, and that exposes three properties (int Width, int Height, bool Loaded) and three methods (void Load(string locatoin), void Resize(int width, int height), and void Save(string location)). All my attempts so far has been unsuccessful.
Building the object is very easy - registering it and managing the COM dependency can be quite tricky.
Your .NET project should be a class library, and your class can be a straightfoward C# / .NET CLR object:
namespace MyCompany.MyProject.Com {
public class MyObject {
public int Width { get; set; }
public int Height { get; set; }
public void Load(string location) { /* implementation here */ }
public void Resize(int width, int height) { /* implementation here */ }
}
}
Right-click your project, select Properties, Application, click Assembly Information... and ensure that "Make assembly COM-Visible" is selected at the bottom of the Assembly Information dialog.
Build your project - you should end up with MyCompany.MyProject.Com.dll in your \bin\debug\ folder.
Build a simple ASP webpage that looks like this:
<% option explicit %>
<%
dim myObject
set myObject = Server.CreateObject("MyCompany.MyProject.Com.MyObject")
myObject.Width = 20
myObject.Height = 40
%>
<html>
<head>COM Interop Demo</head>
<body>
<p>Width + Height = <%= myObject.Width + myObject.Height %></p>
</body>
</html>
Bring up that page on http://localhost/ and verify that you get "Server.CreateObject failed" the first time you try and run it.
Now register your DLL as a COM object using regasm.exe, installed with the .NET framework:
C:\>C:\Windows\Microsoft.NET\Framework\v2.0.50727\regasm.exe /tlb MyCompany.MyProject.Com.dll /codebase
Microsoft (R) .NET Framework Assembly Registration Utility 2.0.50727.4927
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.
Types registered successfully
Assembly exported to 'D:\WebDlls\MyCompany.MyProject.Com.tlb', and the type library w
as registered successfully
Now refresh your web page, and you should see Width + Height = 60 in your output.
These instructions assume you're not running anything in 64-bit; if you are, it gets more complex. (You either need to run everything as 64-bit - compile a 64-bit project and use the 64-bit version of regasm.exe to register it for 64-bit COM, accessed by IIS running a 64-bit scripting host) - or manually force everything to 32-bit.