本文介绍了WCF:如何将 ServiceThrottlingBehavior 添加到 WCF 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码用于返回我的 WCF 服务实例 ServiceClient:

I have the below code for returning back an instance of my WCF Service ServiceClient:

    var readerQuotas = new XmlDictionaryReaderQuotas()
    {
        MaxDepth = 6000000,
        MaxStringContentLength = 6000000,
        MaxArrayLength = 6000000,
        MaxBytesPerRead = 6000000,
        MaxNameTableCharCount = 6000000
    };


    var throttlingBehaviour = new ServiceThrottlingBehavior(){MaxConcurrentCalls=500,MaxConcurrentInstances=500,MaxConcurrentSessions = 500};
    binding = new WSHttpBinding(SecurityMode.None) {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas};

    dualBinding = new WSDualHttpBinding(WSDualHttpSecurityMode.None)
                      {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas};

    endpointAddress = new EndpointAddress("http://localhost:28666/DBInteractionGateway.svc");

    return new MusicRepo_DBAccess_ServiceClient(new InstanceContext(instanceContext), dualBinding, endpointAddress);

最近我在超时方面遇到了一些问题,因此我决定添加一个限制行为,例如:

Lately I was having some trouble with timeouts and so I decided to add a throttling behavior, like such:

    var throttlingBehaviour = new ServiceThrottlingBehavior () {
        MaxConcurrentCalls=500,
        MaxConcurrentInstances=500,
        MaxConcurrentSessions = 500
    };

我的问题是,我应该在上面的代码中的哪个位置将此 throttlingBehaviour 添加到我的 MusicRepo_DBAccess_ServiceClient 实例中?

My question is, where in the above code should I add this throttlingBehaviour to my MusicRepo_DBAccess_ServiceClient instance?

从我在网上找到的一些例子来看,他们正在做这样的事情:

From some of the examples I found on the web, they are doing something like this:

ServiceHost host = new ServiceHost(typeof(MyService));
ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior
{
    MaxConcurrentCalls = 40,
    MaxConcurrentInstances = 20,
    MaxConcurrentSessions = 20,
};
host.Description.Behaviors.Add(throttleBehavior);
host.Open();

请注意,在上面的代码中,他们使用了 ServiceHost 而我没有,然后他们打开它(使用 Open()),而我打开 ServiceHostcode>MusicRepo_DBAccess_ServiceClient 实例...这让我感到困惑.

Notice that in the above code they are using a ServiceHost whereas I am not, and they are then opening it (with Open()) whereas I open the MusicRepo_DBAccess_ServiceClient instance...and this is what got me confused.

推荐答案

可以在配置文件afaik中指定行为,生成的客户端会服从,使用行为.

You can specify the behavior in the configuration file afaik, and the generated client will obey, using behaviors.

为简洁起见排除了一些配置部分

Some configuration sections excluded for brevity

<service
    behaviorConfiguration="throttleThis" />

        <serviceBehaviors>
            <behavior name="throttleThis">
                <serviceMetadata httpGetEnabled="True" />
                <serviceThrottling
                    maxConcurrentCalls="40"
                    maxConcurrentInstances="20"
                    maxConcurrentSessions="20"/>
            </behavior>
        </serviceBehaviors>

这篇关于WCF:如何将 ServiceThrottlingBehavior 添加到 WCF 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 19:34