当前位置:编程学习 > 网站相关 >>

化零为整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>();

            // 调用回调接口中的方法
     &nbs

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,