每个 HTTP 请求的类的单个实例
为了调试我的 ASP.NET 应用程序,我创建了一个名为 MessageHandling.cs
的类。此时它是一个单例模式,但我希望它成为每个请求的一个实例。
我的 mssqlDb 类用诸如“Db 连接”、“数据插入”之类的消息填充 MessagesHandling
类。处理完 apsx 页面的所有事件后,Page_LoadComplete()
事件中的 createFile.apsx.cs
会读取 MessageHandling
类。所有错误和消息都会显示给用户。
此时系统进入调试状态。此时的问题是,发送请求后 MessageHandling 并未清空,并且错误也显示在第二个浏览器上,而不执行任何操作。我还希望能够使用该系统向最终用户显示消息,例如:“博客已创建”。
我的问题的基本内容如下:A 类
创建B 类
C 类
读取B 类
单例不起作用,因为它不是针对每个用户/会话/请求的。所以我需要另一种方法。
To debug my ASP.NET application I created a class called MessageHandling.cs
. At this point it's a singleton pattern but I want it to be an instance per request.
My mssqlDb
class fills the MessagesHandling
class with messages like: 'Db connected', 'Data inserted' and stuff like that. After all the Events of the apsx page are processed the MessageHandling
class is read by createFile.apsx.cs
in the event Page_LoadComplete()
. All the errors and messages will be showm to the user.
At this point the system works for debugging. The problem at this point is that the MessageHandling isn't emptied after the request has been send and the errors are also shown on the second browser without doing anything. I also want to be able to use this system for showing messages to the end users like: "Blog created".
The basic of my problem is the following:Class A
creates Class B
Class C
reads Class B
The singleton doesn't work because it's not per user / session / request. So I need an other method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其存储在
HttpContext.Items
< /a>,这样每个请求都会如此。HttpContext.Items - 一个 Per-请求缓存存储
Store it in the
HttpContext.Items
, that way it will be for each request.HttpContext.Items - a Per-Request Cache Store
HttpContext.Current
是当前请求的HttpContext
对象。它有一个Items
属性,它是一个从object
到object
的IDictionary
。您可以在其中放置任何您喜欢的内容,它将与当前请求相关联。HttpContext.Current
is theHttpContext
object for the current request. It has anItems
property which is anIDictionary
fromobject
toobject
. You can put anything you like in there, and it will be tied to the current request.您可以检查单例的构造函数以查看该对象是否存在于会话中吗?如果没有,创建它然后返回它?
Can you check in the constructor of your singleton to see if that object exists in the session? If not, create it and then return it?