WCF serviceThrottling无法正常工作

时间:2013-05-02 20:14:12

标签: c# .net wcf

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

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

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

并且服务器似乎忽略了最大值??? WCF配置中的设置。

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

我的服务器托管如下:

            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;
    }
}

1 个答案:

答案 0 :(得分:0)

默认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>

Take a look for more details