473,388 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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
----------------------------------
Nov 22 '05 #1
1 1848
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.

juchytil wrote:
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
----------------------------------

Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
3
by: Ian | last post by:
The beginning of my assembly that I am getting the access error from looks like this. ********************************* Imports System.EnterpriseServices Imports System Imports...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
20
by: Olav.NET | last post by:
I am a .NET/C++ developer who is supposed to do some work with Access. I do not know much about it except for the DB part. Questions: *1* I am looking for INTENSIVE books to get quickly up to...
9
by: Stan | last post by:
Here is my scenario: Web server ------------ Framework 1.1 Application proxy for the serviced component Component server ------------------- Framework 1.0
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
0
by: m.posseth | last post by:
Hello All , I encounter the following strange situation ,,, I have developed a remoting component , this functions fine ,,, however a customer wanted to have the ability to access this...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
0
by: p.thorn.ru | last post by:
Hello, I am running db2 express-c 9.5 under linux (fedora 7), and my error log frequently shows messages like these: 2008-01-08-18.30.01.952964+180 I651334G1048 LEVEL: Error (OS) PID ...
10
by: Oriane | last post by:
Hi there, this is certainly not the right NG, anyway... I need to launch a registered COM component from within an Asp.Net web page, with the worker process. But this is impossible since the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.