创建了WCF服务端 ServiceHost
怎样不用配置文件 直接用源码就能更改ServiceHost的MaxConcurrentCalls,MaxConcurrentInstances,MaxConcurrentSessions三个属性?

解决方案 »

  1.   

    自定义一个 ServiceThrollAttribute 然后加到Service Class上。using:
    using System;
    using System.Collections.ObjectModel;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;ServiceThrottlingAttribute:
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
        public sealed class ServiceThrottlingAttribute : Attribute, IServiceBehavior
        {
            public int MaxConcurrentCalls { get; set; }
            public int MaxConcurrentInstances { get; set; }
            public int MaxConcurrentSessions { get; set; }
            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
            }
            public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
            }        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                var currentThrottle = serviceDescription.Behaviors.Find<ServiceThrottlingBehavior>();
                if (currentThrottle == null)
                {
                    serviceDescription.Behaviors.Add(GetConfiguredServiceThrottlingBehaviour());
                }
            }        private ServiceThrottlingBehavior GetConfiguredServiceThrottlingBehaviour()
            {
                var behaviour = new ServiceThrottlingBehavior();
                if (MaxConcurrentCalls > 0)
                {
                    behaviour.MaxConcurrentCalls = MaxConcurrentCalls;
                }
                if (MaxConcurrentInstances > 0)
                {
                    behaviour.MaxConcurrentInstances = MaxConcurrentInstances;
                }
                if (MaxConcurrentSessions > 0)
                {
                    behaviour.MaxConcurrentSessions = MaxConcurrentSessions;
                }
                return behaviour;
            }
        }使用:
    [ServiceThrottlingAttribute(MaxConcurrentCalls=1, MaxConcurrentInstances=1, MaxConcurrentSessions=1)]
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
        }
      

  2.   

    我这样设置的 不知道对不对 求大神解答ServiceHost host = new ServiceHost(typeof(MyService), baseAddress);
    ...
    ServiceThrottlingBehavior throttle;
    throttle = new ServiceThrottlingBehavior();
    throttle.MaxConcurrentCalls = 1000;
    throttle.MaxConcurrentInstances = 1000;
    throttle.MaxConcurrentSessions = 1000;
    host.Description.Behaviors.Add(throttle);
      

  3.   

    为什么不直接在WCF配置文件里面设置呢
      

  4.   

    同问,如果是限制某个接口的流量大小,单独使用Attribute可以说的通。不太清楚为啥会话数限制也要单独去做。