简体   繁体   中英

WCF Call during Service OnShutDown()

I know there are a few other similar questions on this topic, but none of them seem to have an answer so I thought we could all hash it out here.

I have a WCF service running on my servers, and I am attempting to send a status message when the server is shut down by overriding "OnShutDown()".

This can be seen as follows:

     protected override void OnShutdown()
           {
               try
               {
                   client.updateInvStatus(machineName, "Client Host Has Shutdown");
               }
               catch (Exception ex)
               {
                   EH.writeToErrorLog("||OnShutdown||Error Shutting Down System \r\n" + ex);

               }
               base.OnShutdown();
           }

This seems to work about 50% of the time, the other 50% I get the following error:

System.ServiceModel.Security.SecurityNegotiationException: Could not establish secure         channel for SSL/TLS with authority '"ENDPOINT ADDRESS REMOVED'. --->     System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
   at System.Net.HttpWebRequest.GetResponse()
   at    System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitF orReply(TimeSpan timeout)
 --- End of inner exception stack trace ---

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebExceptoon webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Ascertain.AscertainAgent.IService.updateInvStatus(String hostNM, String invValue)
   at Ascertain.AscertainAgent.ServiceClient.updateInvStatus(String hostNM, String invValue) in C:\Users\daladd\Documents\CS\Ascertain\Ascertain\Ascertain\Service References\AscertainAgent\Reference.cs:line 209
   at Ascertain.Service1.OnShutdown() in       "FILE LOCATION REMOVED"\Service1.cs:line224

Does anyone have any ideas why this would occur?

I ended up following what Dr.Wily had to say. I added a keep alive to my connection and now the code will work every time. Basically windows won't open any new connections during the OnShutDown() method, so if the connection has timed out it wont work properly.

If anyone has an idea around this that would still be amazing because with this method I will have 2000 persistent connections to my central server. Shiraz, I did try changing the registry key to allow for a longer timeout, but time didn't end up being an issue.

This could be related to the time out of the shutdown. If the shutdown takes too long the process gets killed. This could be happening to you.

Try increasing the timeout here:

HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Contro l\\WaitToKillServiceTimeout

There may be an event in the event log saying that the process was killed.

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