Help using Application object in ASP.NET!

J

jsever05

I have created a class that communicates with an external program using
a single socket connection. This socket connection is already
thread-safe, so multiple requests to the socket can be made at the same
time.

I am trying to set this up as a global object in an ASP.NET web server
running IIS 5.0, but I'm having a problem with that.

In the Global.asax.cs file, I have added the following reference:


CMyClass _theGlobalObjectOfMyClass;


Then in the Application_Start() routine, I added the following code:


_theGlobalObjectOfMyClass = new CMyClass();


Finally, on one of the pages in question I added the following code:

CMyClass _localReference = (CMyClass)
Application["_theGlobalObjectOfMyClass"];


When I try to load the page in question, I get the following error:


System.NullReferenceException: Object reference not set to an instance
of an object.

I have tried restarting IIS by going on the command line and using the
"iisreset", which tells me that the app is restarted successfully, but
nothing changes when I try to access the page. It's like the global
object is not there?

Anyways, if someone could point out what I'm doing wrong that would be
appreciated. One thing - the object in question is actually being
compiled as a separate DLL and linked into the web project in question
as a reference. Would it make a difference if I was not doing that?

Thanks!
 
G

Guest

I would guess that your object is not marked as serializable? Try adding the
[Serializable()] attribute to your class.
 
J

jsever05

I tried that by adding the [Serializable()] tag right above the class
declaration, but it didn't make any difference. Not sure if there was
anything else I was supposed to do?

To be honest, I don't know too much about serialization or why it would
be important in this case - can anybody help out on that?

Thanks...
 
G

Guest

You may have to mark any other custom classes that your main class uses as
serializable as well.

Serialization simply extracts the object and its values into XML, then the
deserialization recreates the object and sets the values back so you can
access them.
 
J

jsever05

I figured out a way to do what I want... by creating a static member
variable in Global.asax.cs like so:

public static CMyClass _theGlobalObjectOfMyClass = new CMyClass();

I was able to successfully call it from a web-page using:

Global._theGlobalObjectOfMyClass.SomeFunction();

and it worked.

Can anybody point out if there are problems with this approach that I
am not considering? This object has events that fire when a message is
received from a socket connection - would using a static class result
in problems with that?
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top