Thursday, March 29, 2012

server side object tags in global.asax

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
randyr> 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 S Is What You Get.
"randyr" <randyr@.online.nospam> wrote in message
news:9EC807FF-20BD-4B7C-A42E-DDF0F9AB39C9@.microsoft.com...
>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
>
> --
> randyr
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.
"Kevin Spencer" wrote:

> 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 extreme
ly
> 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 S Is What You Get.
> "randyr" <randyr@.online.nospam> wrote in message
> news:9EC807FF-20BD-4B7C-A42E-DDF0F9AB39C9@.microsoft.com...
>
>
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 S Is What You Get.
"randyr" <randyr@.online.nospam> wrote in message
news:869FA3F3-2668-4D92-8313-D679CD443229@.microsoft.com...
> 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.
> "Kevin Spencer" wrote:
>
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
"Kevin Spencer" wrote:

> 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 S Is What You Get.
> "randyr" <randyr@.online.nospam> wrote in message
> news:869FA3F3-2668-4D92-8313-D679CD443229@.microsoft.com...
>
>
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
"randyr" wrote:
> I fully understand that but what I would like to do is create some of thos
e
> objects at application start and store them in application level variables
> without having to store them as Application("xyz") , but rather the strong
ly
> 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 webfor
m.
> 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 <objec
t
> tag in a global asa file) in light of the new asp.net object model.
> thx
>
> "Kevin Spencer" wrote:
>
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
"wsMarkM" wrote:
> 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 typ
ed.
> Mark
> "randyr" wrote:
>
public class myStaticClass{
private myStaticClass(){
//load data here
}
public static int myInt = 0;
}
"randyr" wrote:
> I guess that is a possibility, where would you suggest creating and storin
g
> this object for application level access'
> I would still rather an equivalent to the old solution or at least a way o
f
> extending the global object ... so that I can have a application level
> object
> any other ideas
> "wsMarkM" wrote:
>
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
"randyr" wrote:
> I guess that is a possibility, where would you suggest creating and storin
g
> this object for application level access'
> I would still rather an equivalent to the old solution or at least a way o
f
> extending the global object ... so that I can have a application level
> object
> any other ideas
> "wsMarkM" wrote:
>
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/d...-us/dndotnet/ht
ml/convertasptoaspnet.asp
#Calling COM Components from .NET Clients
http://msdn.microsoft.com/library/e...comp.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.)

0 comments:

Post a Comment