简体   繁体   English

来自多个客户端的多个ASP.NET请求如何共享信息?

[英]How can multiple ASP.NET requests from multiple clients share information?

Imagine a website where individual clients can see each-other's presence - like a social network or a chatroom. 想象一个网站,单个客户可以看到彼此的存在-例如社交网络或聊天室。

For example: 例如:
Client1 connects to my website. Client1连接到我的网站。
The back-end C# code updates a static field to indicate Client1's presence. 后端C#代码更新一个静态字段以指示Client1的存在。
Client2 connects to my website. Client2连接到我的网站。
Will the back-end C# code for the second request see the information stored in the static field from the first request? 第二个请求的后端C#代码会看到第一个请求的静态字段中存储的信息吗? Is there anything I need to do to guarantee that these seperate instances of my ASP application are sharing static data? 我需要做些什么来确保我的ASP应用程序的这些单独的实例共享静态数据?

You can also use the Application state or Cache to share information across all your clients. 您还可以使用应用程序状态或缓存在所有客户端之间共享信息。

In an ASP.net application, application / cache would be one of the common ways to share information across the application. 在ASP.net应用程序中,应用程序/缓存将是跨应用程序共享信息的常用方法之一。

However, you need to consider the following first to make sure it fits your specific needs. 但是,您需要首先考虑以下内容,以确保它满足您的特定需求。

When using application state, you must be aware of the various considerations: 使用应用程序状态时,必须注意各种注意事项:

  1. Resources 资源
  2. Volatility 挥发性
  3. Scalability 可扩展性
  4. Concurrency 并发

Refer section "Application State Considerations" in the link above to know more about things that you need to consider before deciding on your choice. 请参阅上面链接中的“应用程序状态注意事项”部分,以了解有关在决定选择之前需要考虑的事项的更多信息。

It totally depends on what information are you trying to share. 这完全取决于您要共享的信息。 Various options can be : 各种选项可以是:

  • DataBase 数据库
  • Cache 高速缓存
  • Application Variables 应用变量

Each of these options is suitable for specific needs. 这些选项中的每一个都适合特定的需求。 You need to identify your needs carefully and then analyze each of the options or their combination to find a perfect solution. 您需要仔细确定需求,然后分析每个选项或它们的组合以找到理想的解决方案。

If you are looking to just share the presence information, you can look at this post here How to tackle this session problem in ASP.NET,VB.NET? 如果您只想共享状态信息,则可以在此处查看此帖子。 如何在ASP.NET,VB.NET中解决此会话问题?

If it is static fields that they are editing then it will be shared between the different users. 如果它们正在编辑的是静态字段,那么它将在不同的用户之间共享。 The only time you should be concerned that a static object isn't being shared between the different threads is if you have different web servers hosting your app (such as a load balancing architecture) since it would be different application domain. 唯一需要担心的是,不同线程之间不会共享静态对象的情况是,如果您拥有托管应用程序的不同Web服务器(例如负载平衡体系结构),因为它是不同的应用程序域。

I would consider using a Windows service that the web application(s) can communicate through via WCF. 我会考虑使用Windows服务,该Web应用程序可以通过WCF进行通信。

This will allow you a great degree of flexibility; 这将为您提供极大的灵活性; it allows you to perform additional processing of data out of process (for example, aggregating presence statistics like "50 people in your extended network are online"). 它允许您对数据进行额外的处理(例如,汇总状态统计信息,例如“扩展网络中的50个人在线”)。 A Windows service also completely frees you from any limitations that in process state mechanisms have. Windows服务还使您完全摆脱了进程状态机制所具有的任何限制。 It will allow you to load balance freely and let multiple applications consume its data. 它将允许您自由地进行负载平衡,并让多个应用程序使用其数据。

If the data is expendable, make the service stateless. 如果数据是消耗性的,则使服务成为无状态。 Otherwise, persist some/all/periodic data to a database, which will allow you to recover even if the service and/or websites go down. 否则,将部分/全部/定期数据保存到数据库中,即使服务和/或网站出现故障,也可以使您恢复。

Lastly, if you build an application that depends on a service, it is a potential single point of failure. 最后,如果您构建依赖于服务的应用程序,则它潜在的单点故障。 The best installations use either a database or a synchronization mechanism to ensure redundancy on out of process services. 最好的安装使用数据库或同步机制来确保流程外服务的冗余。

Edit: to give more detail on your original question, static members are available to all HTTP requests to the app domain that your ASP.Net site is running in. For what it's worth, static variables can also be designated (through attributes) with different levels of access, for example ThreadStaticAttribute . 编辑:要提供有关原始问题的更多详细信息,静态成员可用于运行ASP.Net站点的应用程序域的所有HTTP请求。对于其价值,还可以(通过属性)使用不同的静态变量来指定静态变量访问级别,例如ThreadStaticAttribute

In practice, static variables are a cheap/weak solution. 实际上,静态变量是一种便宜/弱的解决方案。 You have to manually synchronize access and you have little control over expiration. 您必须手动同步访问,并且对到期几乎没有控制。 You can implement a Cache-based solution with zero additional effort, or move to the aforementioned out of process methodology to support enterprise requirements. 您可以以零的额外努力来实施基于缓存的解决方案,或者转到上述的过程外方法来支持企业需求。

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

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