Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instanciate .net assembly as com object

Status
Not open for further replies.

ianohlander

Programmer
Feb 28, 2002
12
0
0
US
I'm not sure if this is the right forum for this, but here goes. I have designed and implemented an asp.net web app that is working well. But our company uses some 3rd party web software in ASP. Because it is 3rd party, I can't really migrate it into .net without breaking it, or at least invalidating any support they offer on it.

One of the 3rd party ASP pages is customizable, but again, cannot be changed to .net (that would entail migrating global includes that are used on other asp pages as well). Yet I need to incorporate into that ASP some functionality provided by my asp.net web app. Specifically, I need a way to call .net methods from an asp page.

Now some research gave me the following strategy.
1) Create a .net assembly that contains a class that basically wrapped method calls to the web app assembly and compile it.
2) use regasm to write the appropriate class data to the registry, so it can be called as a COM object.
3) from the ASP page use the following code:
set testobject=server.CreateObject("ogwebComLibrary.testObject")
response.Write (testobject.getText())

Now, the response I kept getting when I viewed the ASP page was "object not set to instance" on the 2nd line: response.write(testObject.getText()).

So a bit more research turned up another way to do this.

1) Create a strong named key and apply it to that assembly. This was more complicated because all other assemples (like those associated with the webapp and the MS Data application block) also needed to be compiled with an snk.
2) use gacutil to register it in the global assembly cache
3) create object from ASP page.

Same error message.

On both occassions, I checked the registry and can see the registry keys pointing to the .net object.

Looking at the error message more closely, I can see that the ASP is not having a problem SEEING the .net object. It is having a problem instanciating it. Yet the object simple looks like this:

using System;
using System.Text;
using System.Web;
using System.IO;

namespace ogwebComLibrary
{
/// <summary>
/// Summary description for testObject.
/// </summary>
public class testObject{
protected StringWriter tw;

public testObject(){
tw= new StringWriter();
}

public string getText(){
bool hasForm=true;
HttpContext.Current.Server.Execute("testnet.aspx",tw);
tw.Flush();
StringBuilder sb=tw.GetStringBuilder();
string test=sb.ToString();
int bodyindex=test.LastIndexOf("<form");
if(bodyindex==-1){
bodyindex=test.LastIndexOf("<body");
hasForm=false;
}
int endBrace=test.IndexOf(">",bodyindex);
test=test.Substring(endBrace+1);
int bodyslashindex;
if(hasForm){
bodyslashindex=test.LastIndexOf("</form>");
}else
bodyslashindex=test.LastIndexOf("</body>");
test=test.Substring(0,bodyslashindex);
bodyindex=test.LastIndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
if(bodyindex!=-1){
bodyslashindex=test.IndexOf(">",bodyindex);
string test1=test.Substring(0,bodyindex);
string test2=test.Substring(bodyslashindex+1);
test=test1+test2;
}
return test;
}
}
}

The object simply sends a processed ASP.NET page (with a single label whose text is set to "label") to an ASP page, stripping out all the html dealing with body, form, viewstate, etc. It will also handle ASP pages. Just a little test. It will really wrap calls to another assembly once I can be sure ASP can call it's methods.

And an ASP.NET page instanciates it fine. So why won't ASP create the object? It must be seeing the class in the registry (and GAC, for that matter) or I'd get a "class not found"-like exception on the "server.createobject()" line.

any ideas?

Ian Ohlander
iohlander@ogequip.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top