WCF服务节流不工作

本文关键字:工作 服务 WCF | 更新日期: 2023-09-27 18:11:48

我有一个WCF服务,我已经配置为使用serviceThrottling,但是这似乎不工作。

我有一个多线程客户端调用这个服务@

http://localhost:7778/test/sleep?sleep=1000 

和服务器似乎忽略了最大???在WCF配置中设置。

我真正想做的是增加并发连接的数量,但似乎我根本没有影响设置。

我的服务器托管如下:

            WebServiceHost zHost = new WebServiceHost(typeof (Service1));
            zHost.Open();
            Console.WriteLine("Started");
            Console.ReadLine();
            zHost.Close();

App.config的设置如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
      <system.net>
        <connectionManagement>
          <add address="*"  maxconnection="65535"/>
        </connectionManagement>
      </system.net>
      <system.serviceModel>
        <standardEndpoints />
        <behaviors>
          <endpointBehaviors>
            <behavior name="NewBehavior0">
              <webHttp />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="NewBehavior0">
              <serviceThrottling maxConcurrentCalls="1" maxConcurrentSessions="1" maxConcurrentInstances="1" />
            </behavior> 
          </serviceBehaviors>
        </behaviors>
        <services>
          <service name="WCF_REST_TEST_1.Service1">
            <endpoint address="/test" behaviorConfiguration="NewBehavior0" binding="webHttpBinding" bindingConfiguration="" contract="WCF_REST_TEST_1.IService1" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:7778" />
              </baseAddresses>
            </host>
          </service>
        </services>
      </system.serviceModel>
    </configuration>

Service1的设置如下:

[ServiceContract]
public interface IService1
{
    [WebGet(UriTemplate = "sleep?sleep={value}", ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    CompositeType sleep(int value);
}

public class Service1 : IService1
{
    public CompositeType sleep(int sleep)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        if (sleep > 0)
        {
            try
            {
                logger.Log(LogLevel.Info, "Sleep Request recieved: {0}ms", sleep);
                Thread.Sleep(sleep);                   
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, "Error. {0} - {1}", ex.Message, ex.InnerException);
            }
        }
        else
        {
            logger.Log(LogLevel.Info, "No Sleep value supplied");   
        }
        sw.Stop();
        CompositeType zTest = new CompositeType();
        zTest.BoolValue = true;
        zTest.StringValue = "Some String 2" + sleep;
        //zTest.RemoteResponse = responseFromServer;
        zTest.RemoteTime = sw.ElapsedMilliseconds;
        return zTest;
    }
}

WCF服务节流不工作

默认InstanceContextModePerCall,因此您必须设置maxConcurrentCallsmaxConcurrentInstances

看起来你的配置文件是错误的,因为只支持一个调用

<serviceThrottling maxConcurrentCalls="1" maxConcurrentSessions="1" maxConcurrentInstances="1" />
例如

<system.serviceModel>
    <services>
        <service behaviorConfiguration="ServiceProviderBehavior"
                 name="ServiceProvider">
            <endpoint address="http://localhost:5060/" binding="webHttpBinding"
                      behaviorConfiguration="Web" contract="ServiceContracts.IService" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceProviderBehavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceThrottling maxConcurrentCalls="250" maxConcurrentInstances="250" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="Web">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

查看更多细节