开发者

WCF: Timeout error

I have a piece of code that calls a WCF service that is hosted on a server.

The code keeps looping around and around calling this method over and over again. (It's asking for a 'status', so it's not doing any work at all).

That's fine except that after a short period of time I get an error:

This request operation sent to net.tcp://serverName:9001/service1 did not receive a reply within the configured timeout (00:00:09.9843754)

And suddenly i cannot get to the server at all EVER. I increased the timeout to 1min but still the same problem. Note that the program that hosts the service is doing nothing else, just offering it's 'status'. So it's not an issue with the WCF service app being busy.

I think it's a problem with the code calling the service because when i re-start the app it can connect to the service just fin开发者_如何学Pythone ... until after another short time i get the timeout error again. For this reason i don't thnk it's a network error either, as when I restart the app it's ok for a period of time.

Here is the code i use to call the service. Do i need to dispose of the ChannelFactory after each call to clean it up or what am i doing worng?

        NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
                binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

                EndpointAddress endPoint = new EndpointAddress(new Uri(clientPath));

                ChannelFactory<IClient> channel = new ChannelFactory<IClient>(binding, endPoint);
                channel.Faulted += new EventHandler(channel_Faulted);
                IClient client = channel.CreateChannel();

                ((IContextChannel)client).OperationTimeout = new TimeSpan(0, 0, 10);
                ClientStatus clientStatus = client.GetStatus();


You do have to close client connections after you finish calling GetStatus. The best way to do this is to use a using block. But you can also do something like this after your call client.GetStatus()

ClientStatus clientStatus = client.GetStatus();

try
{
    if (client.State != System.ServiceModel.CommunicationState.Faulted)
    {
         client.Close();
    }
}
catch (Exception ex)
{
    client.Abort();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜