server side object tags in global.asax

G

Guest

I am developing an asp.net app based on a previous asp application.

in the asp applications global.asa file I had several

<object id="id" runat="server" scope="scope" class="comclass"> tags for
objects that the app used to speed up some global level data access and
functionality.

I have recoded the class libraries in .net and would like to acomplish the
same functionality in asp.net.

in examples I have seen you can still use the
<object id="id" runat="server" scope="scope" class="Class Name">
tags in global.asax but all these examples seem not to use the codebehind
model where the global.asax file contains the class definition for global:

Public Class Global
Inherits System.Web.HttpApplication
....

how do I create my applcation level objects and how do I access them in a
webform using the new model?

thx
 
K

Kevin Spencer

how do I create my applcation level objects and how do I access them in a
webform using the new model?

This is probably going to sound crass, and I'm certainly used to being
called by that characterization, BUT...

1. Learn Object-oriented programming concepts
2. Learn the ASP.Net object model
3. Evaluate your requirements in the light of an OOP, ASP.Net application
4. Design your object model using the principles of OOP
5. Write your app

Here's why: ASP.Net and ASP have very little in common when it comes to
methodology. ASP is procedural. ASP.Net is object-oriented. ASP is extremely
limited. ASP.Net is unlimited. ASP has little or no programming model.
ASP.Net has a highly structured programming model.

In other words, just trying to "translate" ASP into ASP.Net is an exercise
in futility. You might succeed, but you'll regret it for the lifetime of
your application. Rather than translate, I would strongly suggest
re-engineering.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
G

Guest

Oh I have re-engineered using oop. I would still like to have an application
level complex object that I store application level data,collections... that
was the real question.
 
K

Kevin Spencer

If you've studied up on OOP and ASP.Net, then you should realize that
EVERYTHING in ASP.Net is a class. You would use your classes the same way
you use System.String or any other class. Reference it, and use it. Using it
has nothing whatsoever to do with the global.asax class.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
G

Guest

I fully understand that but what I would like to do is create some of those
objects at application start and store them in application level variables
without having to store them as Application("xyz") , but rather the strongly
typed objects.

I tried adding public properties to the global class in global.asax based on
my class types, but did not find a way to access those members on a webform.

I fully understand how to use my class libraries. what I am asking for is an
equivalent way to create application level objects (ie as using the <object
tag in a global asa file) in light of the new asp.net object model.

thx
 
G

Guest

Try using a class with static methods and properties. Make the constructor
private and when the class is first instantiated, populate the data, this
would work much like the application_start event and would be strongly typed.

Mark
 
G

Guest

I guess that is a possibility, where would you suggest creating and storing
this object for application level access??

I would still rather an equivalent to the old solution or at least a way of
extending the global object ... so that I can have a application level
object
any other ideas
 
G

Guest

public class myStaticClass{
private myStaticClass(){
//load data here
}

public static int myInt = 0;

}
 
G

Guest

Sorry the last reponse prematurly posted but the code should give you an
idea. Then you can access the class from anywhere as long as your project /
page has a reference to the namespace/assembly. You can simply call it like
this:

In page_A.aspx
myStaticClass.myInt = 1234;

In page_B.aspx
if(myStaticClass.myInt > 10)
//do something;

The thing about static members is you don't have to "store" a reference to
them anywhere. You just reference them w/o an instance variable and use them.
They are accessable anywhere (as long as they are public) and they live as
long as the application is running.

I'm sure you could add public members to the HttpApplication class, but I
can't look at that right now. I can check it out for you tomorrow though
(hittin' the road).

Mark
 
S

Steven Cheng[MSFT]

Hi Randyr,

I think we may have further works need to do if we want to get the those
components objects works in ASP.NET just like those global <object >
serverside tags in CLASSIC ASP:

1. As other members have mentioned, we no longer need to use serverside
<object > tag in asp.net , we can just define static class member variables
in the ASP.NET application's Global class to hold the reference of our
global component objects.

2. The second and is the most important point, is that your components are
COM objects( rather than .net class objects). So we can't reference COM
objects directly in .NET world. We need to define .net wrapper class for
those COM objects. And if you're using VS.NET , you can use the "Add
Reference" menu to add the COM objects into your project and VS.NET will
automatically create the Wrapper classes of those COM class for you. Then,
we can reference those wrapper classes in your .net code. Something like
below:

=========================
public class Global : System.Web.HttpApplication
{
public static COMWrapperClass comobj = null;

................

protected void Application_Start(Object sender, EventArgs e)
{
comobj = new COMWrapperClass();
}
.............

========================

And we can reference that static variable in our page's code. Note, in
ASP.NET all the page is executing under FreeThread retrieved from
threadpool (not the STA mode thread as in classic ASP). If your COM
components are STA component, be sure to set your asp.net page as
"aspCompat" = true in the @page directive.

In addition, here are some reference in MSDN on accessing COM objects in
NET through interop or migrate classic ASP code to ASP.NET

#Converting ASP to ASP.NET
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/convertasptoaspnet.asp

#Calling COM Components from .NET Clients
http://msdn.microsoft.com/library/en-us/dndotnet/html/callcomcomp.asp?frame=
true

For further questions about .NET's interop with COM, I suggest you try
posting in the dotnet.framework.interop newsgroup, you can find more
resources on COM interop there.

HTH. Thanks,


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
K

Kevin Spencer

I'm afraid you don't fully understand. Putting a class in a Collection
doesn't make it any less strongly-typed. The Application Collection (which
is pretty well deprecated now in favor of the Cache), the Application Cache,
and the other caching mechanisms of ASP.Net do not change or "reduce" the
type of a class.

If you want to store something in Application, you simply create an instance
and store it. Forget about <OBJECT> tags and other procedural workarounds.
Think abstract!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
K

Kevin Spencer

1. As other members have mentioned, we no longer need to use serverside
<object > tag in asp.net , we can just define static class member
variables
in the ASP.NET application's Global class to hold the reference of our
global component objects.

I'm afraid I have to take issue with this statement. It implies that THE
method for cahing objects is to make them static. In fact, there is a place
for static objects, and there is a place for instance objects. And I would
venture to guess that instance objects are more often useful than static
objects, for a variety of reasons. For one thing, static objects are not
thread-safe. And a static Collection is begging for trouble.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
G

Guest

Ok,
I'll accept that it is not such a loss storing the objects in the
application cache, but just for my own reference if you want to extend the
global class and add a few public members and access those in web forms how
would you do that?
 
K

Kevin Spencer

I'll accept that it is not such a loss storing the objects in the
application cache, but just for my own reference if you want to extend the
global class and add a few public members and access those in web forms
how
would you do that?

I would not do that. The Global class has a specific usage, and I don't mess
with it. However, you might define another class and use the
Application_OnStart to create an instance of that class and store it in
cache.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
G

Guest

Assuming the name of the class which inherits from the HttpApplication class
is "Global" (VS.Net defualt) then you can use the following line of code to
access your custom instance members.

((Global) Context.ApplicationInstance).myInstanceProperty;

I didn't try and do a proper test to see if you need to use a lock on this.
I know that it pulls the instance from a pool of HttpApplication objects and
each instance services one request at a time and then returns to the pool.
But if you want to modify the value, I don't know how it updates the other
instances in the pool or how it should be synced up with the other instances
(if it must be done manually).

But you mentioned that you were concerned about using static members because
of thread safty. Getting around that issue is simple. Instead of using static
fields, use a static property or method where the field is private. When the
set method is called or the public method is called you can lock the field
like this:

private static _myInt;
public static int myInt{
set{
lock(_myInt){
_myInt = value;
}
}
}


I hope that helps
 
G

Guest

thanks

Kevin Spencer said:
I would not do that. The Global class has a specific usage, and I don't mess
with it. However, you might define another class and use the
Application_OnStart to create an instance of that class and store it in
cache.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 

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