简体   繁体   中英

How can I keep WCF from timing out?

I have a very simple WCF service:

[ServiceContract]
public interface IEngine
{
    #region Test code
    // test - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    [OperationContract]
    string Test();
     }

implemented as follows:

public partial class Engine : IEngine
{
    private static int nTestCount = 0;
    string IEngine.Test()
    {
        try
        {
            nTestCount++;
        }
        catch (Exception)
        {
        }

        return "Service OK " + nTestCount.ToString();
    }
     }

When I call the test service method 10 times, in about 10 seconds... I get this error:

The request channel timed out while waiting for a reply after 00:01:00. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.

( I tried to put in my web.config file but it doesn't show up here ) ( help on posting the contents would be appreciated )

Update 1:

Here is the client code that invokes the service:

    private void btn_Test_Click(object sender, EventArgs e)
    {
        ServiceReference1.EngineClient eng = new EngineClient();
        textBox1.Text = eng.Test();
    }

You are not closing your connections when you are done with them, this is causing the server to tie up a connection until the garbage collector collects your object. The default max open sessions a server can handle is 10 for .net 3.5 and older ( it was raised to 100 * ProcessorCount in .NET 4 ).

Dispose of the engine and it should work fine.

private void btn_Test_Click(object sender, EventArgs e)
{
    using(ServiceReference1.EngineClient eng = new EngineClient())
    {
        textBox1.Text = eng.Test();
    }
}

You aren't closing your client connections after each use which after a certain period causes the service to run out of available connections. Once the service reaches its max open connections, new connections will timeout waiting for a connect to clear up.

You need to close your connections when you're done with them by calling their Dispose() or wrapping them in using blocks

private void btn_Test_Click(object sender, EventArgs e)
{
    using (ServiceReference1.EngineClient eng = new EngineClient())
    {
        textBox1.Text = eng.Test();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM