全球 - Application_OnStart问题 [英] Global - Application_OnStart question

查看:55
本文介绍了全球 - Application_OnStart问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到Application_OnStart事件只针对某个应用程序触发了一次。


我有兴趣创建一个全局事件。 object(可供应用程序的所有请求使用的对象)。我以为我可以使用这个事件来创建我的对象的实例。


我发现事件被触发并且创建了一个实例。但是,当我从页面引用此对象时,它为null。什么给出了?


建议的方法是声明,创建等我的类,以便创建单个实例(应用程序范围)并且我的所有页面都可以访问此对象。


我不介意我必须使这个对象是线程安全的,也可以是单例。我想知道推荐的方式,如果可能的话。


-

Shiv R. Kumar
< a rel =nofollowhref =http://www.matlus.comtarget =_ blank> http://www.matlus.com

解决方案

如果您希望某个类对应用程序是全局的,则必须将其放入应用程序的全局内存区域,例如应用程序缓存。

应用程序缓存是一个集合,其中的元素是

整个应用程序的全局元素。但请注意,应用程序缓存已经到期,并且您需要处理其中元素的到期。


-

HTH,

Kevin Spencer

..Net开发人员

Microsoft MVP

大事是组成了很多小东西。
" Shiv Kumar" < SH *** @ erolsnoooospaaaam.com>在消息中写道

新闻:#u ************** @ TK2MSFTNGP10.phx.gbl ...

我看到了Application_OnStart事件仅针对某个

应用程序触发一次。


我对创建全局应用程序感兴趣。 object(对象的所有请求都可以使用
的对象)。我以为我可以使用这个

事件来创建我的对象的实例。


我发现事件被触发并且创建了一个实例。但是,当我从页面引用此对象时,它是
。给出了什么?


我的类的声明,创建等的推荐方法是什么,以便创建一个

单个实例(应用程序范围内)并且所有我的页面有

访问这个对象。


我不介意我必须让这个对象线程安全且单例

要么。我想知道推荐的方式,如果这是可能的话。

可能。


-

Shiv R. Kumar
http://www.matlus.com

>我发现事件被触发并创建了一个实例。

但是,当我从页面引用此对象时,它为null。




您确定要引用之前创建的相同内容?如果你创建一个对象并把它放在一个应用程序变量中,比如

Application [" MyObject] = new MyObject();


you应该可以从任何页面访问它:


Application.Lock();

MyObject obj =(MyObject)Application [" MyObject] ;

Application.UnLock();

Application_Start是创建对象的最佳位置。对您的问题的解释可能是Application_Start中的代码在创建对象之前的某处失败。您可能想尝试将您的创作声明放在首位,然后再试一次。


Martin。


" Shiv Kumar" < SH *** @ erolsnoooospaaaam.com>在消息新闻中写道:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

我看到Application_OnStart事件只被触发一次某个应用程序。


我有兴趣创建一个全球的object(可供应用程序的所有请求使用的对象)。我以为我可以使用这个事件来创建我的对象的实例。


我发现事件被触发并且创建了一个实例。但是,当我从页面引用此对象时,它为null。什么给出了?


建议的方法是声明,创建等我的类,以便创建单个实例(应用程序范围)并且我的所有页面都可以访问此对象。


我不介意我必须使这个对象是线程安全的,也可以是单例。我想知道推荐的方式,如果可能的话。


-

Shiv R. Kumar
< a rel =nofollowhref =http://www.matlus.comtarget =_ blank> http://www.matlus.com


单例模式是一种广为接受且可重复使用的方式来创建对象的
。如果它与你所有的

页面在同一个程序集(项目)中,他们将能够引用该对象而不需要查看

应用程序状态。
< br $> b $ b -

Scott
http: //www.OdeToCode.com


On Tue,2004年1月13日05:12:12 -0500,Shiv Kumar

< SH *** @ erolsnoooospaaaam.com>写道:

我看到Application_OnStart事件只针对某个应用程序触发了一次。

我对创建一个全局事件很感兴趣。 object(可供应用程序的所有请求使用的对象)。我以为我可以使用这个事件来创建我的对象的实例。

我发现事件被触发并且创建了一个实例。但是,当我从页面引用此对象时,它为null。是什么给出了什么?

建议的方法是声明,创建等我的类,以便创建单个实例(应用程序范围)并且我的所有页面都可以访问此对象。

我不介意我是否必须使这个对象具有线程安全性和单例。我想知道推荐的方式,如果可能的话。




I see that the Application_OnStart event is fired only once for a certain application.

I''m interested in creating a "global" object (an object that will be available to all requests for the application). I thought I could use this event to create an instance of my object.

I find that the event does get fired and an instance is created. However, when I reference this object from a page it is null. What gives?

What is the recommended way to declare, create etc. my class such that an single instance is created (application wide) and that all my pages have access to this object.

I don''t mind if I have to make this object thread-safe and a singleton either. I''d just like to know the recommended way and if this is at all possible.

--
Shiv R. Kumar
http://www.matlus.com

解决方案

If you want a class to be global to the Application, you have to put it into
a global memory area for the Application, such as the Application Cache. The
Application Cache is a Collection, and the elements in it are global to the
entire Application. Be aware, however, that the Application Cache has
expiration, and you need to handle the expiration of the elements in it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Shiv Kumar" <sh***@erolsnoooospaaaam.com> wrote in message
news:#u**************@TK2MSFTNGP10.phx.gbl...
I see that the Application_OnStart event is fired only once for a certain
application.

I''m interested in creating a "global" object (an object that will be
available to all requests for the application). I thought I could use this
event to create an instance of my object.

I find that the event does get fired and an instance is created. However,
when I reference this object from a page it is null. What gives?

What is the recommended way to declare, create etc. my class such that an
single instance is created (application wide) and that all my pages have
access to this object.

I don''t mind if I have to make this object thread-safe and a singleton
either. I''d just like to know the recommended way and if this is at all
possible.

--
Shiv R. Kumar
http://www.matlus.com


> I find that the event does get fired and an instance is created.

However, when I reference this object from a page it is null.



Are you sure you are referencing the same thing you created earlier? If you create an object and put it in an application variable like

Application["MyObject] = new MyObject();

you should be able to access it from any page like so:

Application.Lock();
MyObject obj = (MyObject)Application["MyObject];
Application.UnLock();
Application_Start is a perfect spot to create your object. An explanation for your problem could be that the code in Application_Start fails somewhere before the creation of your object. You may want to try to put your creation statement first and try again.

Martin.

"Shiv Kumar" <sh***@erolsnoooospaaaam.com> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
I see that the Application_OnStart event is fired only once for a certain application.

I''m interested in creating a "global" object (an object that will be available to all requests for the application). I thought I could use this event to create an instance of my object.

I find that the event does get fired and an instance is created. However, when I reference this object from a page it is null. What gives?

What is the recommended way to declare, create etc. my class such that an single instance is created (application wide) and that all my pages have access to this object.

I don''t mind if I have to make this object thread-safe and a singleton either. I''d just like to know the recommended way and if this is at all possible.

--
Shiv R. Kumar
http://www.matlus.com


The singleton pattern is a well accepted and reusable way to create
your object. If it is inside the same assembly (project) as all your
pages they will be able to reference the object without even lookin in
Application state.

--
Scott
http://www.OdeToCode.com

On Tue, 13 Jan 2004 05:12:12 -0500, "Shiv Kumar"
<sh***@erolsnoooospaaaam.com> wrote:

I see that the Application_OnStart event is fired only once for a certain application.

I''m interested in creating a "global" object (an object that will be available to all requests for the application). I thought I could use this event to create an instance of my object.

I find that the event does get fired and an instance is created. However, when I reference this object from a page it is null. What gives?

What is the recommended way to declare, create etc. my class such that an single instance is created (application wide) and that all my pages have access to this object.

I don''t mind if I have to make this object thread-safe and a singleton either. I''d just like to know the recommended way and if this is at all possible.




这篇关于全球 - Application_OnStart问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆