0

我有以下疑问。我有一个页面“MyPage”,并且我在页面类中声明了几个字典对象。

我的疑问是

  1. 如果我将字典声明为私有非静态对象,我将无法在该页面类中的函数中使用它(该对象为空)
  2. 但是,如果我将字典声明为静态的,我就可以跨函数跨对象。但是该对象对于现在打开页面的所有用户来说是否相同(猜测每个用户将拥有一个页面实例反过来又具有页面类的实例,并且类的静态变量在所有实例中都是不变的类)

我的理解对吗?如何声明该对象在页面类中的所有函数中都可用,并且对于页面的每个实例(用户)都是唯一的。

更新1

Okie,我发现页面类中字典对象的初始化是在不同的线程中完成的(WCF Duplex 回调)。但是在主页线程中,字典对象仍然保持为空(未初始化)。有任何想法吗?

更新2

编组 - 有用吗?

更新3

回复约翰-

感谢您的回复。我现在面临的问题是将数据从客户端的 WCF 回调类(将在单独的线程中调用)传递到 asp.net 页面。那么我该怎么做呢?视图状态可以帮助我跨请求持久化数据。但是,当服务器调用回调通知更改时,我应该如何将更改(数据)传递给 UI 线程(asp.net 页面)?

4

4 回答 4

1

在第二种情况下你是对的。在您的第一种情况下,我猜您的意思是,如果用户单击页面上的多个控件,则事件处理程序会看到字典为空(而不是从前一个事件处理程序获得结果)。

请记住,页面上的每个请求(即使来自同一用户)都会创建页面类的新实例。这意味着每次请求开始时,您的字典都会为空。变量在后续请求之间保持其值的唯一方法是将其持久化到服务器端(例如,在服务器上的用户特定会话信息中)或将其与页面内容一起推送到客户端,以便它可以后续请求数据的一部分(因此它在请求之间存储在 ViewState 或客户端浏览器的其他存储中)。

于 2010-07-02T14:55:16.010 回答
1

Rereading this question, there are three seperate state machines, and none of them are being coupled together - hence the problem :)

  • State of the "user state" - these are the key/value pairs in the dictionary, their lifetime spans multiple page requests and callbacks

  • State of a "page", which needs to consume the data from "user state". Pages are destroyed after each and every page request.

  • State of the "service call" which needs to populate the data in "user state" Service calls are generally destroyed after each invocation.

There are a few strategies that could enable you to couple the systems:

  • ViewState such that the state machine for "user state" is sent down as part of the state of the page, and sent back on postbacks. This may constrain how you perform service callbacks

  • Session such that the state machine for "user state" is stored server side, and can be accessed by key.

  • Static dictionary of user states, where the key for the outer dictionary would be the identity of the "user state", where the 1st page request would create the "user state" entry, and you'd need to manage teardown. (v.similar to session - though works without ASP.NET).

There are lots of nuances to each solution - I'd advise light reading :)

于 2010-07-02T15:24:16.657 回答
1

Don't do things like this.

If you need to maintain data between pages, use Session state. That's what it's for. You should remember that you get a new instance of your page class on every request. Do not use statics to keep changing data around for subsequent requests. You will probably get into trouble with multiple requests updating the data at the same time.


You can't do things like this with ASP.NET!

You seem to be treating this as though it were a desktop program - as though your class instance and all state will still be there for you next time you execute a method on the page. That's not true - when the request is complete, your page will be Disposed. Nothing about the page will remain valid.

In particular, if the callback doesn't happen before the request ends, then the callback method had better not reference anything having to do with the request, like your page. That's because the callback might fire after the request is already over! The state is corrupt or worse.

Unless you are going to have the page wait for the callback, you'd really better not use them in your pages. Instead, create a separate Windows Service or something and have it issue the requests and await the callback. The page can then use Ajax or something to ask if the request is complete, and to get the response once complete.


If you think you heard me say to call back to an ASP.NET page, then you misunderstood.

Create a Windows Service. Your Windows Service will host a WCF service that the ASP.NET application will talk to. The WCF Service will keep track of things like who's joined a chat, who's typing, etc.

The web application cannot be notified when something interesting happens. Instead, the web application will have to poll the WCF service, asking if anything interesting has happened. When something happens, the WCF service will pass the "something" back to the ASP.NET application (or possibly, back to the page, called by AJAX).

I misspoke earlier. You simply cannot use a callback contract at all in this situation. It's not like the web pages are like a desktop application, one per user, waiting to be notified. They're more like a desktop application where, when the user makes a request, you take his PC and give him a new one just like it, before the response arrives.

于 2010-07-03T05:45:31.087 回答
0

你是对的,静态成员对于页面的所有实例都是相同的,因此对于所有个人用户都是一样的。如果要从类中的每个方法访问它,则需要将其设为非静态成员。您应该研究为什么该对象为空。您是否在适当的时间正确实例化它?

于 2010-07-02T14:28:17.760 回答