0

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

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

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

1 回答 1

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>

查看更多详细信息

于 2013-05-02T20:36:26.743 回答