如何配置WCF超时?

时间:2014-07-22 22:45:37

标签: wcf

我有一个测试WCF应用程序,它只是调用服务层上的方法。我试图测试WCF超时但我似乎无法发生超时 - 我添加了一个线程休眠模拟一个长时间运行的事务。

我在客户端绑定配置中设置了sendTimeout="00:05:00"但是当我在服务调用中添加15秒线程休眠时,这似乎没有任何影响。即应用程序等待15秒,结果成功返回。

客户端配置

<configuration>
    <system.serviceModel>
        <bindings>
            <ws2007HttpBinding>
                <binding name="WS2007HttpBinding_IBookService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:05:00" sendTimeout="00:05:00"
                    bypassProxyOnLocal="false" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    useDefaultWebProxy="true" allowCookies="false">
                    <security>
                        <transport realm="" />
                    </security>
                </binding>
            </ws2007HttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8080/bookservice/ws" binding="ws2007HttpBinding"
                bindingConfiguration="WS2007HttpBinding_IBookService" contract="BookServiceReference.IBookService"
                name="WS2007HttpBinding_IBookService" />
        </client>
    </system.serviceModel>
</configuration>

服务配置

<configuration>
    <system.serviceModel>
        <services>
            <service behaviorConfiguration="MetaDataBehaviour" name="Service.BookService">
                <clear />
                <endpoint address="ws" binding="ws2007HttpBinding" contract="Service.IBookService"
                    listenUriMode="Explicit">
                </endpoint>
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8080/bookservice" />
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="MetaDataBehaviour">
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

如何让超时发生?

1 个答案:

答案 0 :(得分:0)

15秒肯定不到5分钟(即sendTimeout="00:05:00"的值)。

如果您想模拟sendTimeout违规会引发TimeoutException,那么您需要保证在超出sendTimeout之后服务电话才会返回。

在你5分钟的特殊情况下,这就足够了:

public int MyServiceCall()
{
    // Better bring something to read...
    System.Threading.Thread.Sleep(300000);
    return 0;
}