How do I access a C# COM+ component from Asp.NET?

G

Guest

We have written a C# class library project called ESUtilsCS.cs and its dll
was imported as a component into a COM+ Application on a Windows 2000 server.
(The code for the class library cann be found at the end of this post.)

We successfully access the component from a jscript ASP page (code below )

How can I access the same component from C# Asp.NET?

Joe



// ------------- begin jscript asp page code -----------------
var oESU = Server.CreateObject("ESUtilsCS.Binary");
Response.Clear();
Response.ContentType = sContentType;
Server.ScriptTimeout = 24 * 60 * 60;
if( oESU.readFile(sUrl) == true)
{
Response.AddHeader("content-disposition","inline; filename=\"" +
sDrawingIDTemp + "." + sFileType + "\"");
Response.AddHeader("content-Length","" + oESU.Length);
Response.BinaryWrite(oESU.File);
}
else
{
sError = "Unable to read file.";
}
// ------------- end jscript asp page code -----------------


// -------------------- ESUtilsCS.cs begin
----------------------------------
using System;
using System.IO;
using System.Security.Principal;
using System.Runtime.InteropServices;

namespace ESUtilsCS
{
public class Binary
{
private string _error;
private byte[] _file;
private int _length;

public Binary()
{
_error = "";
_file = null;
_length = 0;
}

public bool readFile(string fileName)
{
_file = null;
_error = "";
_length = 0;

try
{
FileInfo fi = new FileInfo(fileName);
if( !fi.Exists )
{
_error = String.Format("File '{0}' does not exist", fileName);
return false;
}

FileStream fs = null;
try
{
fs = fi.OpenRead();
}
catch(Exception ex)
{
_error = String.Format("Failed to open the file '{0}'. Error =
'{1}'", fileName, ex.Message);
return false;
}

_length = Convert.ToInt32(fi.Length);
_file = new byte[_length];
int nBytesRead=fs.Read(_file, 0, _length);
fs.Close();

if( nBytesRead != fi.Length )
{
_error = String.Format("Bytes read = '{0}', Should have been
'{1}'", nBytesRead, _length);
return false;
}

return true;
}
catch(Exception ex)
{
_error = String.Format("Unhandled exception. Error = '{0}'",
ex.ToString());
return false;
}
}

public int Length
{
get { return _length; }
}

public byte[] File
{
get { return _file; }
}

public string Error
{
get { return _error; }
}
}
}
// -------------------- ESUtilsCS.cs end
----------------------------------
 
J

Joshua Flanagan

Add a reference to the C# library in your ASP.NET project. Use the
types in the library like any other library referenced in your ASP.NET
project.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top