Search code examples
c#web-serviceswcfhttpmodulebasichttpbinding

HttpModule in WCF project just works once


Please consider this code:

<system.web>
    <compilation debug="true" targetFramework="4.6"/>
    <httpRuntime targetFramework="4.5.2"/>
   <authentication mode="None" />
  </system.web>
  <system.serviceModel>
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

     <services>
      <service name="MyNameSpace.Services.Service1" behaviorConfiguration="ServiceBehavior">

        <endpoint  binding="basicHttpBinding"  contract="MyNameSpace.Services.ISrv"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpBinding" scheme="http"/>
    </protocolMapping>
    </system.serviceModel>
        <system.webServer>
            <modules runAllManagedModulesForAllRequests="true">
                <add name="AuthSecurity" type="MyNameSpace.CustomAuthorization"  />
            </modules>
        <directoryBrowse enabled="true"/>
    </system.webServer>

and the HttpModule code is:

namespace MyNameSpace
{
    public class CustomAuthorization : IHttpModule
    {
        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
           CheckAccess();
        }

        private bool CheckAccess()
        {
            HttpContext c = HttpContext.Current;

            if (HttpContext.Current.Handler != null)   // <--Break Point
            {
                string authHeader = HttpContext.Current.Request.Headers["Authorization"];
                ...
            } 
            return false;
        }
    }
}

and in the client I wrote this code:

Service1client client = new Service1client();

client.ClientCredentials.UserName.UserName = "mmmm"
client.ClientCredentials.UserName.Password = "nnnn";

var tmp = client.DoWork(1);

the problem is after running the project, Service returns the correct result but HttpModule code didn't execute.

When I use a breakpoint in HttpModule it hits during Application_Start event. But after that it doesn't hit any more and its code doesn't execute.

Where is the problem?

Thanks


Solution

  • As I understand it, you defined just what to do at application start; if you want to check incoming requests, try to add these methods to your HttpModule and set breakpoints to check whether those methods are hit:

    private void Application_BeginRequest(Object source, 
             EventArgs e)
        {
             CheckAccess();
        }
    
        private void Application_EndRequest(Object source, EventArgs e)
        {
             CheckAccess();
        }
    

    Of course you need to register those methods at application start:

            application.BeginRequest += 
            (new EventHandler(this.Application_BeginRequest));
        application.EndRequest += 
            (new EventHandler(this.Application_EndRequest));