简体   繁体   English

从异步WCF双工调用更新asp.net页-对象范围-ASP.NET

[英]Updating the asp.net page from the asynchronous WCF duplex call - object scope - ASP.NET

I've the following doubt. 我有以下疑问。 I've a page "MyPage" and i've declared few dictionary objects in the page class. 我有一个页面“ MyPage”,并且在页面类中声明了几个字典对象。

My doubt is 我的疑问是

  1. If i declare the dictionary as a private non-static object i'm not able to use it across the functions in that page class (the object is getting nulled) 如果我将字典声明为私有的非静态对象,则无法在该页面类中的函数之间使用它(该对象将为空)
  2. But if i declare the dictionary to be static i'm able to across the object across the functions. 但是,如果我声明字典为静态字典,则可以跨函数访问对象。 But will that object be same across all the users who have opened the page right now (guessing that each user will have an instance of the page in turn has an instance for the page class and static variables of a class are invariable across all the instances of the class) 但是,该对象在现在已打开页面的所有用户中是否相同(猜测每个用户将拥有该页面的一个实例,而该用户又具有该页面类的一个实例,并且该类的静态变量在所有实例中都是不变的班级的)

Is my understanding right? 我的理解正确吗? How to declare the object to be available across all the functions within the page class and unique for each instance of the page (user). 如何声明对象可在页面类中的所有函数中使用,并且对于页面的每个实例(用户)唯一。

Update1 更新1

Okie, i find that the initialization of the dictionary object in the page class is done in a different thread (WCF Duplex callback). 好的,我发现页面类中字典对象的初始化是在另一个线程中完成的(WCF Duplex回调)。 But in the main page thread the dictionary object is still remaining as null (uninitialized). 但是在主页线程中,字典对象仍然保留为空(未初始化)。 Any ideas? 有任何想法吗?

Update2 更新2

Marshalling - useful by any chance? 编组-有用吗?

Update3 更新3

In reply to John- 为了回应约翰-

Thanks for your reply. 感谢您的回复。 The problem i'm facing now is to pass the data from the WCF callback class at the client side(which will be invoked in a separate thread) to the asp.net page. 我现在面临的问题是将数据从客户端的WCF回调类(将在单独的线程中调用)传递到asp.net页。 So how can i do that? 那我该怎么办呢? View state may help me to persist the data across requests. 查看状态可以帮助我在请求之间持久保存数据。 But when the callback is invoked by the server notifying for a change how should i pass the changes (data) to the UI thread (asp.net page)? 但是,当服务器调用回调通知更改时,应如何将更改(数据)传递给UI线程(asp.net页)?

You are right in the second case. 您在第二种情况下是正确的。 In your first case I'm guessing you mean that if a user clicks multiple controls on your page then the event handlers are seeing that the dictionary is null (instead of having the result from the previous event handler). 在第一种情况下,我猜测您的意思是,如果用户单击页面上的多个控件,则事件处理程序将看到字典为空(而不是从先前的事件处理程序获得结果)。

Remember that every request on a page (even from the same user) creates a new instance of your page class. 请记住,页面上的每个请求(甚至来自同一用户)都将创建页面类的新实例。 That means that each time a request starts, your dictionary will be null. 这意味着每次请求开始时,您的字典都将为空。 The only way for a variable to maintain its value between subsequent requests is to persist it server-side (for example, in user-specific session information on the server) or to push it to the client with the page content so that it can be part of the subsequent request data (so it's stored in ViewState or other storage at the client's browser between requests). 变量在后续请求之间保持其值的唯一方法是在服务器端将其持久化(例如,在服务器上的用户特定会话信息中)或将其与页面内容一起推送到客户端,以便可以后续请求数据的一部分(因此,它们在两次请求之间存储在ViewState或客户端浏览器的其他存储中)。

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. ViewState,以便将“用户状态”的状态机作为页面状态的一部分向下发送,并在回发时发送回去。 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). (类似于会话-尽管没有ASP.NET即可使用)。

There are lots of nuances to each solution - I'd advise light reading :) 每个解决方案都有很多细微差别-我建议您轻读:)

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! 您不能使用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. 而是创建一个单独的Windows Service或其他东西,然后让它发出请求并等待回调。 The page can then use Ajax or something to ask if the request is complete, and to get the response once complete. 该页面然后可以使用Ajax或其他工具询问请求是否完成,并在完成后获取响应。


If you think you heard me say to call back to an ASP.NET page, then you misunderstood. 如果您认为您听到我说过要回ASP.NET页,那么您会误解。

Create a Windows Service. 创建一个Windows服务。 Your Windows Service will host a WCF service that the ASP.NET application will talk to. 您的Windows服务将托管ASP.NET应用程序要与之通信的WCF服务。 The WCF Service will keep track of things like who's joined a chat, who's typing, etc. WCF服务将跟踪谁加入了聊天,谁在键入等内容。

The web application cannot be notified when something interesting happens. 当发生有趣的事情时, 无法通知Web应用程序。 Instead, the web application will have to poll the WCF service, asking if anything interesting has happened. 相反,Web应用程序将不得不轮询WCF服务,询问是否发生了任何有趣的事情。 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). 当发生某些事情时,WCF服务会将“某些东西”传递回ASP.NET应用程序(或者可能传递回页面,由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. 它们更像是一个桌面应用程序,当用户发出请求时,您可以带走他的PC并在响应到来之前给他一个新的PC。

you are right that a static member will be the same for all instances of the page, and thus all individual users. 您是对的,静态成员对于页面的所有实例都是相同的,因此对于所有个人用户都是相同的。 You need to make it a non-static member if you want to access it from every method in the class. 如果要从类中的每个方法访问它,则需要使其成为非静态成员。 You should look into why the object is null. 您应该调查为什么该对象为null。 Are you properly instantiating it at the proper time? 您是否在适当的时间正确实例化了它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM