webabcd - 专注于asp.net
ASP.NET
从现在开始 一切都不晚
posts - 149, comments - 4064, trackbacks - 328, articles - 0
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
化零为整WCF(12) - 并发和限流(Concurrent和Throttle)
Posted on 2008-06-03 19:30
webabcd
阅读(3107)
评论(10)
编辑
收藏
所属分类:
WCF
[索引页]
[源码下载]
化零为整WCF(12) - 并发和限流(Concurrent和Throttle)
作者:
webabcd
介绍
WCF(Windows Communication Foundation) - 并发(Concurrent):
1、ConcurrencyMode.Single:单线程并发模式。系统自动加锁,无并发问题
·InstanceContextMode.PerCall:每个线程都会被分配一个新的实例
·InstanceContextMode.PerSession:每个Session被分配一个新的实例,每个Session内同时只会有一个线程操作实例
·InstanceContextMode.Single:唯一实例,并发调用只会有一个线程操作实例
2、ConcurrencyMode.Reentrant:可重入的单线程并发模式。有可重入(回调)操作时,此模式才会生效,从回调返回的线程会进入队列尾部排队
·InstanceContextMode.PerCall:每个线程都会被分配一个新的实例,当有回调操作时如果使用Single并发模式的话就会产生死锁(1、调用服务端;2、回调客户端;3、返回服务端,1的时候锁定了,到3的时候就无法执行了,所以死锁了),此时应该用Reentrant并发模式
·InstanceContextMode.PerSession:每个Session被分配一个新的实例,每个Session内同时只会有一个线程操作实例,Session内可重入
·InstanceContextMode.Single:唯一实例,并发调用只会有一个线程操作实例,全局可重入
3、ConcurrencyMode.Multiple:多线程并发模式。系统不会自动加锁,有并发问题
·InstanceContextMode.PerCall:每个线程都会被分配一个新的实例,无并发问题
·InstanceContextMode.PerSession:每个Session被分配一个新的实例,每个Session内多线程操作实例的话会有并发问题
·InstanceContextMode.Single:唯一实例,允许多线程并发操作实例,有并发问题
WCF(Windows Communication Foundation) -
限流
(
Throttle
):
<
behaviors
>
<
serviceBehaviors
>
<
behavior
name
="BehaviorPerCall"
>
<!--
httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false
-->
<
serviceMetadata
httpGetEnabled
="true"
/>
<!--
maxConcurrentCalls - 服务中同时存在的最大活动消息数,默认值为 16
-->
<!--
maxConcurrentInstances - 服务中同时存在的最大服务实例数,默认值为 Int32.MaxValue
-->
<!--
maxConcurrentSessions - 服务中同时存在的最大会话数,默认值为 10
-->
<
serviceThrottling
maxConcurrentCalls
=""
maxConcurrentInstances
=""
maxConcurrentSessions
=""
/>
</
behavior
>
<
behavior
name
="BehaviorPerSession"
>
<
serviceMetadata
httpGetEnabled
="true"
/>
<
serviceThrottling
maxConcurrentCalls
=""
maxConcurrentInstances
=""
maxConcurrentSessions
=""
/>
</
behavior
>
<
behavior
name
="BehaviorSingle"
>
<
serviceMetadata
httpGetEnabled
="true"
/>
<
serviceThrottling
maxConcurrentCalls
=""
maxConcurrentInstances
="1"
maxConcurrentSessions
=""
/>
</
behavior
>
</
serviceBehaviors
>
</
behaviors
>
示例(以
ConcurrencyMode.Reentrant
为例)
1、服务
IDuplexReentrant.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
namespace
WCF.ServiceLib.Message
{
/**/
///
<summary>
///
IDuplexReentrant接口(演示ConcurrencyMode.Reentrant)
///
</summary>
///
<remarks>
///
IDuplexReentrantCallback - 回调接口
///
</remarks>
[ServiceContract(CallbackContract
=
typeof
(IDuplexReentrantCallback))]
public
interface
IDuplexReentrant
{
/**/
///
<summary>
///
Hello
///
</summary>
///
<param name="name">
名字
</param>
[OperationContract]
void
HelloDuplexReentrant(
string
name);
}
/**/
///
<summary>
///
IDuplexReentrant回调接口
///
</summary>
public
interface
IDuplexReentrantCallback
{
/**/
///
<summary>
///
Hello
///
</summary>
///
<param name="name"></param>
[OperationContract]
void
HelloDuplexReentrantCallback(
string
name);
}
}
DuplexReentrant.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
namespace
WCF.ServiceLib.Message
{
/**/
///
<summary>
///
DuplexReentrant类 (演示ConcurrencyMode.Reentrant)
///
</summary>
///
<remarks>
///
ConcurrencyMode - 获取或设置一个值,该值指示服务是支持单线程、多线程还是支持可重入调用。默认值为 System.ServiceModel.ConcurrencyMode.Single。
///
Single - 服务实例是单线程的,且不接受可重入调用。
///
Reentrant - 服务实例是单线程的,且接受可重入调用。
///
Multiple - 服务实例是多线程的。
///
</remarks>
[ServiceBehavior(ConcurrencyMode
=
ConcurrencyMode.Reentrant)]
public
class
DuplexReentrant : IDuplexReentrant
{
/**/
///
<summary>
///
Hello
///
</summary>
///
<param name="name">
名字
</param>
public
void
HelloDuplexReentrant(
string
name)
{
//
声明回调接口
IDuplexReentrantCallback callback
=
OperationContext.Current.GetCallbackChannel
<
IDuplexReentrantCallback
>
();
//
调用回调接口中的方法
callback.HelloDuplexReentrantCallback(name);
}
}
}
2、宿主
DuplexReentrant.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
namespace
WCF.ServiceHost2.Message
{
/**/
///
<summary>
///
host WCF.ServiceLib.Message.DuplexReentrant的类
///
</summary>
public
class
DuplexReentrant
{
/**/
///
<summary>
///
启动WCF.ServiceLib.Message.DuplexReentrant服务
///
</summary>
public
void
Launch()
{
using
(ServiceHost host
=
new
ServiceHost(
typeof
(WCF.ServiceLib.Message.DuplexReentrant)))
{
host.Open();
Console.WriteLine(
"
服务已启动(WCF.ServiceLib.Message.DuplexReentrant)
"
);
Console.WriteLine(
"
按<ENTER>停止服务
"
);
Console.ReadLine();
}
}
}
}
App.config
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
<
system.serviceModel
>
<
services
>
<!--
name - 提供服务的类名
-->
<!--
behaviorConfiguration - 指定相关的行为配置
-->
<
service
name
="WCF.ServiceLib.Message.DuplexReentrant"
behaviorConfiguration
="MessageBehavior"
>
<!--
address - 服务地址
-->
<!--
binding - 通信方式
-->
<!--
contract - 服务契约
-->
<
endpoint
address
="Message/DuplexReentrant"
binding
="netTcpBinding"
contract
="WCF.ServiceLib.Message.IDuplexReentrant"
/>
<
endpoint
address
="mex"
binding
="mexHttpBinding"
contract
="IMetadataExchange"
/>
<
host
>
<
baseAddresses
>
<
add
baseAddress
="http://localhost:12345/Message/DuplexReentrant"
/>
<
add
baseAddress
="net.tcp://localhost:54321/"
/>
</
baseAddresses
>
</
host
>
</
service
>
</
services
>
<
behaviors
>
<
serviceBehaviors
>
<
behavior
name
="MessageBehavior"
>
<!--
httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false
-->
<
serviceMetadata
httpGetEnabled
="true"
/>
<
serviceDebug
includeExceptionDetailInFaults
="true"
/>
</
behavior
>
</
serviceBehaviors
>
</
behaviors
>
</
system.serviceModel
>
</
configuration
>
3、客户端
DuplexReentrant.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
using
System.Windows.Forms;
namespace
Client2.Message
{
/**/
///
<summary>
///
演示Message.DuplexReentrant的类
///
</summary>
public
class
DuplexReentrant
{
/**/
///
<summary>
///
Hello
///
</summary>
///
<param name="name">
名字
</param>
public
void
HelloDulexReentrant(
string
name)
{
var ct
=
new
Client2.Message.ReentrantCallbackType();
var ctx
=
new
InstanceContext(ct);
var proxy
=
new
MessageSvc.DuplexReentrant.DuplexReentrantClient(ctx);
proxy.HelloDuplexReentrant(name);
}
}
}
ReentrantCallbackType.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
Client2.Message
{
/**/
///
<summary>
///
实现回调接口
///
</summary>
///
<remarks>
///
CallbackBehavior - 在客户端应用程序中配置回调服务实现
///
UseSynchronizationContext - 如果对服务的所有调用都必须在 System.Threading.SynchronizationContext 指定的线程上运行,则为 true;否则为false。默认值为 true。
///
</remarks>
[System.ServiceModel.CallbackBehavior(UseSynchronizationContext
=
false
)]
public
class
ReentrantCallbackType : MessageSvc.DuplexReentrant.IDuplexReentrantCallback
{
/**/
///
<summary>
///
Hello
///
</summary>
///
<param name="name">
名字
</param>
public
void
HelloDuplexReentrantCallback(
string
name)
{
MessageBox.Show(
"
Hello:
"
+
name);
}
}
}
App.config
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
<
system.serviceModel
>
<
client
>
<
endpoint
address
="net.tcp://localhost:54321/Message/DuplexReentrant"
binding
="netTcpBinding"
contract
="MessageSvc.DuplexReentrant.IDuplexReentrant"
>
</
endpoint
>
</
client
>
</
system.serviceModel
>
</
configuration
>
运行结果:
单击"
btnDuplexReentrant
"按钮后弹出提示框,显示"Hello: webabcd"
OK
[源码下载]
Feedback
#1楼
回复
引用
2008-06-03 21:30 by
黑白 [未注册用户]
呵呵,终于出新作了
看来兄弟最近很忙啊
#2楼
[
楼主
]
回复
引用
查看
2008-06-04 08:34 by
webabcd
@黑白
:)
确实啊,最近太忙了
#3楼
回复
引用
2008-06-04 09:10 by
ivw [未注册用户]
呵呵,要支持了。。
#4楼
[
楼主
]
回复
引用
查看
2008-06-04 12:14 by
webabcd
@ivw
:)
多谢
#5楼
回复
引用
查看
2008-06-04 14:33 by
哦,奇怪
哦,我的沙发,板凳和地板……
#6楼
[
楼主
]
回复
引用
查看
2008-06-04 18:28 by
webabcd
@哦,奇怪
:)
多谢支持
#7楼
回复
引用
2008-06-05 22:03 by
黑白 [未注册用户]
“·InstanceContextMode.Single:唯一实例,允许多线程并发操作实例,有并发问题”
那这个并发问题怎么解决呢
#8楼
[
楼主
]
回复
引用
查看
2008-06-06 08:38 by
webabcd
@黑白
要做并发处理
下次会写一篇关于并发处理的
#9楼
回复
引用
2008-06-06 22:37 by
黑白 [未注册用户]
呵呵
期待啊
#10楼
[
楼主
]
回复
引用
查看
2008-06-10 07:53 by
webabcd
@黑白
:)
一有时间就会写的
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2008-07-24 08:39 编辑过
历史上的今天:
2007-06-03
[翻译]使用ASP.NET AJAX实现幻灯片效果
所属分类的其他文章:
·
化零为整WCF系列文章索引
·
化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)
·
化零为整WCF(17) - 安全(Security)
·
化零为整WCF(16) - 消息队列(MSMQ - MicroSoft Message Queue)
·
化零为整WCF(15) - 可靠性消息(ReliableMessaging)
·
化零为整WCF(14) - 事务(Transaction)
·
化零为整WCF(13) - 并发控制(锁)(Mutex, Semaphore, Monitor, Lock, ThreadPool, Interlocked, ReaderWriterLock)
·
化零为整WCF(12) - 并发和限流(Concurrent和Throttle)
·
化零为整WCF(11) - 会话状态(Session)
·
化零为整WCF(10) - 实例模型(InstanceContextMode)
最新IT新闻:
·
第一财经周刊:当前互联网世界正处无秩序时代
·
Visual Studio 2008 SDK 1.1 发布
·
死敌VMware变身微软认证计划新成员
·
英特尔雅虎开发网络计算机频道
·
Windows Live视频邮件9月9日开始测试
博客园新闻频道
博客园首页
社区
<