C#/.NET 4.0 自定义 SessionState 提供程序/模块/SessionItems 集合类库的体系结构?
这个问题涉及编写一个大规模可扩展的应用程序。 C# 中高效的客户会话状态类库,我迫切需要明智的建议。
指导我的原始线程基于:
我刚刚发现为什么所有的 ASP.Net 网站都很慢,我正在尝试解决这个问题
希望这个帖子可以从阴间回调正如我所拥有的在过去的 48 小时里(小睡了几次)试图实现这里阐明的想法,我必须承认我在解决方案空间中有些迷失,如果能提供一些清晰的说明,我将不胜感激。
请允许我解释一下我在哪里&是什么让我感到困惑?我正在努力实现的目标。背面&与詹姆斯和他一起来到这里乔尔提出了很好的观点,但链接之外的具体实现让我有些茫然。
我正在尝试创建一个类库/dll,我可以在我的几个项目中使用它来绕过 inproc、sql 和 sql 的默认会话状态选项。状态服务器。这对我来说有点新鲜,所以请原谅我的无知。此外,我有点沮丧。肮脏的 asp.net / C# 编码器以及泛型、线程安全、锁定和序列化的一些更微妙的细微差别确实往往会伤害我的小脑袋:))
我首先根据我发现的示例创建一个自定义会话状态提供程序在MSDN上写访问。然后我发现了这一页,潘多拉的盒子已经敞开,现在我无法把精灵放回瓶子里。我挖掘了更多内容,找到了自定义会话状态模块的模板,并将其也添加到了我的类库中。所以现在wwww...我有自己的提供程序和模块,然后在阅读了 Joel 的许多技巧后,我最终根据我在 MSDN 上找到的另一个示例创建了一个自定义会话状态项集合。
这最初是基于哈希列表的,不知何故我最终将它变成了排序列表,然后我似乎一一遵循了乔尔的步骤,涉及以下内容:
在所有 .cs 文件中重写 lock(typeOf) 以使用 lock( thisLock) 其中 thisLock 是一个私有静态对象,根据这里提到的 Rico 文章...我确实理解死锁的“概念”,但这是我第一次尝试扩展 ASP.NET 站点,我感觉不太像 Joe Architect 然而。我不能在这里偷懒,因为我不希望建立在一个摇摇欲坠的基础上,只是为了在未来的道路上遇到一堆难以找出的错误。
然后我听从了 Joel 关于“slim”版本锁的建议,并让它在我的类库中也能工作。然而我的蜘蛛感应仍然刺痛着我,告诉我我基本上仍然处于服从模式而不是启蒙模式,即我没有 1s 和 1s 的 NEO 风格愿景。 0年代在我的感知场中翩翩起舞。
所以我不断地回到这个页面,就像丹·布朗小说中的难民一样,现在我正在看乔尔的最后警告,他建议在会话状态项目集合实现线程安全之前,在多元宇宙中不能保证有任何好处。所以我认为是时候开始温习线程安全集合了。
就在那时,我开始阅读 MSFT 关于 http://msdn.microsoft 的文章。 com/en-us/library/dd997305.aspx 排序列表和排序列表其他老东西但显然不是那么好东西被弃用,转而使用泛型。现在,我实际上已经创建了整个业务层和 linq 层,它们是可序列化的,用于购物车并存储在离线数据库中。
但事实是会话状态、线程安全、同时使用模块和线程安全。供应商,并且这里缺乏任何可供调试的示例,使我的大脑驱动器不断红线,并且散热器摇摇欲坠。
MSFT 在上面的链接中表示,“.NET Framework 2.0 中引入的集合类位于 System.Collections.Generic 命名空间中。其中包括 List、Dictionary 等。与 .NET Framework 2.0 相比,这些类提供了改进的类型安全性和性能。 NET Framework 1.0 类但是,.NET Framework 2.0 集合类不提供任何线程同步;当在多个线程上同时添加或删除项目时,用户代码必须提供所有同步
。 .NET Framework 4,因为它们不仅提供 .NET Framework 2.0 集合类的类型安全性,而且比 .NET Framework 1.0 集合提供的更高效、更完整的线程安全性。
”明白了,这就是线程的结尾,但我在调试时遇到了一些非常奇怪的行为。
- 我正在使用线束网站来测试我的会话库。如果我同时指定 httpmodule 和自定义提供程序,则只有该模块在我启动的线束/测试站点中运行。但是-如果我修改真正成熟的代码库的 web.config,它会选择提供程序。
我确实意识到我可以一次使用一个来通过不使用该模块来使线束站点指向提供者。
现在很好,但后来我发现自己对自己说,“自我......你有一个自定义集合,但它是线程安全的吗?我们是否仍然锁定它,我们应该从集合外部锁定它,即在模块中还是我的大脑说的是“线程安全”封装在集合本身中,
而且我如何真正知道所有这些缓慢的 page.aspx/longpage.aspx 示例必须等待线程完成,即使用户沮丧地点击了另一个链接(什么沮丧?一个网站?思想消失了!)...
所以基本上,就像兔八哥一样,我恳求你,“让我离开这里......”
我提前感谢你的及时帮助对这个问题以及其他具有里程碑意义的问题的考虑
最好,
Cary Abramoff,MCSD.NET(.net 十年了,我仍然希望这都是 JQUERY 宝贝!)
This question pertains to writing a massively scalable & efficient customer sessionstate class library in C# and I am in desperate need of sage counsel.
Original thread that has been guiding me based upon:
I just discovered why all ASP.Net websites are slow, and I am trying to work out what to do about it
Hope this thread can be called back from the netherworld as I have been spending the past 48 hours (with a few power naps) trying to implement the ideas elucidated here and I must admit I am somewhat lost in the solution space and would be grateful for some clarity.
Allow me to explain where I am at & what is confusing me & what I am trying to accomplish. The back & forth here with James & Joel raises great points but concrete implementations beyond the links leave me somewhat in the dark.
I'm trying to create a class library/dll I can use in several of my projects to bypass the default sessionstate options of inproc, sql & stateserver. This is somewhat new to me so please excuse my ignorance. Further I am somewhat of a down & dirty asp.net / C# coder and some of the more subtle nuances of generics, thread safety, locking, and serialization do tend to hurt my little head :))
I had started by creating a custom sesion state provider based upon a sample I found on MSDN that writes to access. Then I had found this page and Pandora's box was wide open and now I cannot get the genie back in the bottle. I dug more and I found a template for a custom sessionstate module and I added that too to my class library. SO nowwwww... I have both a provider and module of my own and then after reading Joel's many tips I ended up creating a custom session state items collection based upon yet another sample I found on MSDN.
That was originally based upon a hashlist, somehow I ended up turning it into a sortedlist, and then I seem to have followed Joel's steps one by one as pertains to the following:
lock(typeOf) rewritten in all .cs files to use lock(thisLock) where thisLock is a private static object as per the Rico article mentioned here ... I do understand the "concept" of deadlocks but this is my first attempt to scale an asp.net site and I am not feeling like Joe Architect quite yet. I cannot afford to be lazy here because I do not wish to build upon a shaky foundation only to get a bunch of hard to figure out bugs further on up the road.
Then I followed Joel's advice about the "slim" version of the locks and got that working in my classlib as well. And yet still my spider-sense is tingling telling me I basically still am in obedience mode rather than enlightenment mode, i.e. I don't have the NEO style vision of 1s & 0s dancing in my perceptual field.
So I keep coming back to this page like a refugee from a Dan Brown novel and now I am looking at the final admonition by Joel advising that until the sessionstateitem collection is made threadsafe no good can be assured in the multiverse. So I figure it is time to start brushing up on threadsafe collections.
That's when I start reading an article by MSFT on http://msdn.microsoft.com/en-us/library/dd997305.aspx SortedLists & other oldies but apparently not so goodies being deprecated in favor of generics. Now I have actually created entire business layers and linq layers that are serializable and are used for shopping carts and stored in an offline database.
But the truth is sessionstate, threadsafety, using BOTH module & provider, and the lack of any samples here to debug has my cerebral drive continuously redlining and the heat sink is faltering.
MSFT says in above link, "The collection classes introduced in the .NET Framework 2.0 are found in the System.Collections.Generic namespace. These include List, Dictionary, and so on. These classes provide improved type safety and performance compared to the .NET Framework 1.0 classes. However, the .NET Framework 2.0 collection classes do not provide any thread synchronization; user code must provide all synchronization when items are added or removed on multiple threads concurrently.
We recommend the concurrent collections classes in the .NET Framework 4 because they provide not only the type safety of the .NET Framework 2.0 collection classes, but also more efficient and more complete thread safety than the .NET Framework 1.0 collections provide.."
So James sorta bolted thru the exit saying I got it, and that's the end of the thread but I have experienced some very peculiar behavior while debugging.
- I am using a harness website to test my session lib. If I specify BOTH the httpmodule and the custom provider only the module runs in the harness/testing site I whipped up. BUT- if I modify the web.config of a real mature codebase it opts for the provider.
I did realize I could use one at a time to get the harness site pointing to the provider by not using the module.
That is fine for now but then I found myself saying to myself, "self.... you have a custom collection but is it threadsafe, are we still locking it, should we be locking it from outside the collection, i.e. in module or provider? My brain is saying is "thread-safety" encapsulated in the collection itself.
And how do I really know that all these slow page.aspx/longpage.aspx samples where one must hang out waiting for a thread to finish, even if user has click another link in frustration (what frustration? on a web site? perish de thought!)...
So basically, like Bugs Bunny, hangin' from the smithereen remnants of la bella luna I beseech thee, "GET ME OUTTA HERE..."
I thank you in advance for your timely consideration in this matter & others of monumental importance.
Best,
Cary Abramoff, MCSD.NET (Ten years of .net & I still wish it were all JQUERY baby!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您接下来真正需要做的是创建一个测试程序,同时调用您的线束站点中的多个页面。这将模拟锁定会带来好处的情况,并且还将帮助您确定您的实现在逻辑上是否正确。如果没有这一点,我认为你在这方面的工作有点盲目。
希望有帮助。
附带说明:我目前正在开发由 Redis 支持的自定义会话状态提供程序。我的计划是实现一个允许关闭锁定的实现。一旦稳定,我可能会在 Github 上开源。
Sounds like what you really need to do next is create a testing program that calls multiple pages in your harness site simultaneously. This will simulate the situation that locking is meant to benefit and it will also help you determine if your implementation is logically correct. Without that I think you're sort of working at this blindly.
Hope that helps.
On a side note: I am currently working on a custom session state provider backed by Redis. My plan is to have an implementation that allows the locking to be turned off. I will likely get open sourced on Github once it is stable.