title: HttpContext is null in WCF service running in ASP.NET Compatibility Mode
body: <p>I have a asp.net website that is hosting a WCF service. This service is then accessed from a desktop app. In my service the HttpContext is always null during the execution of the Validate method in my implementation of the UserNamePasswordValidator class. I'm using Username as the client credential type. I need access to the http context in order to get the Url the service was accessed from in order to validate the username and password correctly as the site can be accessed using different Urls and each one has a different user store.</p>
  <p>The following attribute on the class that contains the method that will be called after the validator class (and the validator class as well)</p>
  <pre><code>[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  </code></pre>
  <p>I have a service is configured as follows:</p>
  <pre><code>&lt;system.serviceModel&gt;
    &lt;bindings&gt;
      &lt;wsHttpBinding&gt;
        &lt;binding name=&quot;wsHttpSecurityOptions&quot;&gt;
          &lt;security mode=&quot;Message&quot;&gt;
            &lt;message clientCredentialType=&quot;UserName&quot; establishSecurityContext=&quot;true&quot; negotiateServiceCredential=&quot;true&quot;/&gt;
            &lt;transport clientCredentialType=&quot;Certificate&quot; proxyCredentialType=&quot;None&quot;/&gt;
          &lt;/security&gt;
        &lt;/binding&gt;
      &lt;/wsHttpBinding&gt;
    &lt;/bindings&gt;
    &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
      &lt;behavior name=&quot;SecurityServiceBehavior&quot;&gt;
        &lt;serviceMetadata httpGetEnabled=&quot;true&quot;/&gt;
        &lt;serviceDebug includeExceptionDetailInFaults=&quot;true&quot;/&gt;
        &lt;serviceCredentials&gt;
          &lt;userNameAuthentication userNamePasswordValidationMode=&quot;Custom&quot; customUserNamePasswordValidatorType=&quot;WCFServer.MyAuthenticator&quot; includeWindowsGroups=&quot;false&quot;/&gt;
          &lt;serviceCertificate findValue=&quot;myurl.com&quot; x509FindType=&quot;FindBySubjectName&quot; storeLocation=&quot;LocalMachine&quot; storeName=&quot;My&quot;/&gt;
        &lt;/serviceCredentials&gt;
      &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
    &lt;/behaviors&gt;
    &lt;services&gt;
      &lt;service behaviorConfiguration=&quot;SecurityServiceBehavior&quot; name=&quot;Test.WCF.Actions&quot;&gt;
        &lt;endpoint address=&quot;&quot; binding=&quot;wsHttpBinding&quot; bindingConfiguration=&quot;wsHttpSecurityOptions&quot; contract=&quot;WCFServer.IActions&quot;/&gt;
      &lt;/service&gt;
    &lt;/services&gt;
    &lt;serviceHostingEnvironment aspNetCompatibilityEnabled=&quot;true&quot; /&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  <p>I've seen the <a href="https://web.archive.org/web/20111208201956/http://connect.microsoft.com:80/wcf/feedback/details/491844/when-using-asp-net-compatibility-mode-httpcontext-and-web-configs-arent-handled-properly-on-the-first-hit-only" rel="nofollow noreferrer">HttpContext is not initialised on first call bug</a>  but this happens to me for every call I make to the service, even when I call the same method on the same connection more than once</p>
  <p>Edit: clarified question to answer marc_s's comment and Aliostad's question</p>
  <p>Edit: Added following links that suggest the http context should not be null</p>
  <ul>
  <li><a href="https://web.archive.org/web/20160305051116/http://blogs.msdn.com:80/b/wenlong/archive/2006/01/23/516041.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/wenlong/archive/2006/01/23/516041.aspx</a></li>
  <li><a href="https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-3.5/aa702682%28v=vs.90%29" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa702682(v=VS.90).aspx</a></li>
  </ul>
  <p>Can anyone lend me a hand with this please? I'd rather not have to put the site's Url in the appSettings config section for all my sites.</p>