title: Windows Service throwing MessageSecurityException
body: <p>We have a Windows Service that makes a WCF service call. Normally, it works fine. However, occasionally when we restart the Windows Service, a <code>MessageSecurityException</code> is thrown and everything goes to toss and it is thrown again repeatedly thereafter.</p>
  
  <p>One solution we found is setting <code>AllowedImpersonationLevel</code> to "Impersonation". However, we don't use any impersonation. Also, it is a random issue and not always reproducible. Hence, this solution would be irrelevant.</p>
  
  <p>Any light that someone can shed on this would be highly appreciated.</p>
  
  <p>The stack trace of the error message:</p>
  
  <blockquote>
    <p>2010-12-24 23:18:42,581 [3] ERROR
    [DataSync] – An error occured during
    Data Sync [(null)]
    System.ServiceModel.Security.MessageSecurityException:
    The HTTP request is unauthorized with
    client authentication scheme
    ‘Negotiate’. The authentication header
    received from the server was
    ‘Negotiate,NTLM’. ->
    System.Net.WebException: The remote
    server returned an error: (401)
    Unauthorized. at
    System.Net.HttpWebRequest.GetResponse()
    at
    System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan
    timeout) —End of inner exception stack
    trace --</p>
    
    <p>Server stack trace: at
    System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest
    request, HttpWebResponse response,
    WebException responseException,
    HttpChannelFactory factory) at
    System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest
    request, HttpWebResponse response,
    HttpChannelFactory factory,
    WebException responseException,
    ChannelBinding channelBinding) 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.ServiceChannel.Call(String
    action, Boolean oneway,
    ProxyOperationRuntime operation,
    Object[] ins, Object[] outs) at
    System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
    methodCall, ProxyOperationRuntime
    operation) at
    System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage
    message)</p>
  </blockquote>