none
The ServiceHost.Close(Timespan) is not timing out as expected RRS feed

  • Question

  • Hi,

    I have the following piece of code in my WCF service shutdown

    private const double timeSpanInMS = 250;

    try {

    var serviceHostCloseTimeout = TimeSpan.FromMilliseconds(timeSpanInMS);                    serviceHost.Close(serviceHostCloseTimeout);

    }catch(TimeoutException) {}

    But I could see that in some cases the Close is taking up to 1 sec. As per the API specification it shall timeout always in 250 ms throwing TimeoutException.

    I also tried setting the CloseTimeout property before opening the serviceHost, but no effect.

    In this kind of scenarios we shall always assume there is some problem in the way we are using API.

    Anybody faced this issue, or I am using the API wrongly?


    Thursday, August 3, 2017 9:11 AM

All replies

  • Hi Sudheesh,

    In my option, there is no problem in your API using.

    I could not throw the CloseTimeOut exception either.

    Per the document, it should throw exception when the host close expires the time settings. But, I tried “host.Closing += Host_Closing;  host.Closed += Host_Closed;”  with a thread.sleep, it would not throw exception either.

    Did you get any get any timeout exception in production or development?

    As my experience, the timeout most happen with SendTimeout for Binding while transfer large data or file. There is no need to wrong about close time out.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 4, 2017 6:51 AM
  • Hi Edward,

    Thanks for the response.

    I am never getting TimeoutException and that is my problem.

    We have a ServiceHosting framework and there are performance tests running in daily regression.

    The performance test for shutting down services fail as close will not complete in stipulated time.

    I tried the following code.

    private void CloseServiceHost(TimeSpan timeOut) {
                var task = Task.Factory.StartNew(() => {
                    try {
                        serviceHost.Close();
                    } catch (Exception) { }
                });
                if (!task.Wait(timeOut)) {
                    var sw = new Stopwatch();
                    sw.Start();
                    serviceHost.Abort();
                    sw.Stop();
                    Debug.WriteLine("### Abort time: " + sw.ElapsedMilliseconds);
                }
            }

    Still some times Abort is taking more than 1 sec. So the test still fails sporadically.

    Suppose if we skip Abort, the next start of service may cause AddressAlreadyInUseException as there is a chance that previous instance may not be fully down. This cause instability in daily regression.

    Any thoughts?

    Wednesday, August 9, 2017 5:13 AM
  • Hi Sudheesh,

    According to CommunicationObject.Abort Method (), it will cancel the Close calls if the Close did not complete.

    I suggest you make a test with below code:

    private void CloseServiceHost(TimeSpan timeOut) {
                var task = Task.Factory.StartNew(() => {
                    try {
                        serviceHost.Close(timeOut);
                    } catch (Exception e) {
    		Debug.WriteLine("Time Out");
     }
                });
                if (!task.Wait(timeOut)) {
                    var sw = new Stopwatch();
                    sw.Start();
    Debug.WriteLine(serviceHost.State);
                    serviceHost.Abort();
    Debug.WriteLine(serviceHost.State);
                    serviceHost.Close();
    Debug.WriteLine(serviceHost.State);
                    sw.Stop();
                    Debug.WriteLine("### Abort time: " + sw.ElapsedMilliseconds);
                }
            }

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 10, 2017 2:26 AM