单个执行多个应用程序请求 [英] Singleton execution for multiple application requests

查看:88
本文介绍了单个执行多个应用程序请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类( Concurrency )对象,我在其中启动所有与数据库相关的设置。

所有这些代码都在WCF服务中。例如管理,我已经在 persession 的基础上建立了服务实例。现在我想通过所有数据库层类共享并发类对象。我正在考虑做这个单例可以使用。虽然我已经使用了perscion实例创建wcf服务,但我很困惑,单例模式将如何同时响应多个请求。每个用户是否会获得相同的类对象,或者同一个类对象将与所有用户共享?

I have one class (Concurrency) object in which i am initiating all db related settings .
All this code is in WCF service. For instance management, i have made service instance on persession basis. Now i want to share Concurrency class object through all db layer classes. I was thinking to do this singleton can be used.Though i have used persession instance creation for wcf service, i am confused , how singleton pattern will react for multiple requests at same time. Will each user get same class object or same class object will be shared with all users ?

推荐答案

单例模式旨在将同一个类的实例返回给所有呼叫者。

要同时处理多个请求,您可以使用 lock 机制。



这个MSDN页面应该会给你一些很好的信息

http://msdn.microsoft.com/en-us/library/ff650316.aspx [ ^ ]



难以给出更好的答案使用您提供的信息。
The singleton pattern is designed to return the same single instance of your class to all callers.
To handle multiple requests at the same time you can use the lock mechanism.

This MSDN page should give you some good information
http://msdn.microsoft.com/en-us/library/ff650316.aspx[^]

Difficult to give a better answer with the information you have given.


1.如果您只想访问和存储数据库相关设置,则不需要单例,您可以使用由静态构造函数初始化的静态属性。



2.如果您想在WCF的上下文中实现更复杂的并发场景,可以在下一篇文章中找到详细信息:WCF并发(单个,多个和可重入)和限制 [ ^ ]
1.If you are just want to access and store DB related settings you do not need singleton, you could use static properties that are initialized by static constructor.

2.If you want to implement more complex concurrency scenarios in the context of WCF you could find details in the next article: WCF Concurrency (Single, Multiple, and Reentrant) and Throttling[^]


我要求根据客户端请求在运行时选择连接字符串。所以我宣布了一个密封的类,我用来设置我的所有数据库和其他设置。然后我使用字典对象和客户端ID作为键值对将该特定对象存储在缓存中。



在执行期间我检查了特定客户端数据库设置对象的缓存,如果没有找到,那么我创建了新对象并用新对象保持我的缓存更新
I had requirement to choose connection string at run time according to client request. so i declared an sealed class which i used to set all my db & other settings. Then i stored that particular object in cache using dictionary object & client id as key value pair.

During execution i checked cache for particular client db setting object, if not found then i created new object & kept my cache updated with new object


这篇关于单个执行多个应用程序请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆