Change timeout for each WCF method or call

2020-03-03 05:29发布

问题:

I have a quite large "old" WCF Service with many different methods.

The most of these methods are "normal" so they should answer in less than 10 seconds but there are several methods (8 or 9) that are long processes so they can take a long time to get a response.

The receivetimeout and sendtimeout were set to 00:40:00 to ensure they had time enought to accomplish these processes.

The problem is sometimes we have connection issues and the "normal" methods take a really long time to crash...

They are all in the same service because they use a really big model and they wanted to reuse the model from the service in every call (not having a PersonsService.User and a RobotsService.User... because they are the same class in different services).

The first solution I imagine is to make a different Service with those long processes and set a short timeout to the normal service... but I should have to make a lot of changes because of Model use...

Is there any way to set a different timeout in each call? Or by service method? Should I chunk the Service anyway?

Thanks in advance!!

回答1:

First of all, the timeout to configure in your case is OperationTimeout, which allows the time limit to wait for the service to reply before timing out. You can modify the operation timeout before making a call from the client side.

To set the OperationTimeout on the channel, you can type case your proxy/channel instance as IContextChannel and set OperationTimeout.

For example:

IClientChannel contextChannel = channel as IClientChannel;
contextChannel.OperationTimeout = TimeSpan.FromMinutes(10);

HTH, Amit