title: Get Custom Attribute on ASMX Web Service from HTTP Module
body: <p>Here's the situation:</p>
  
  <p>Legacy ASP.NET product. A lot of old ASMX services (among other types of endpoints - ASPX, ASHX, etc). </p>
  
  <p>We're enhancing some security logic. Part of the changes dictate defining the application module to which each ASMX service belongs.  We plan to use the custom attribute shown below for this purpose.</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Class)]
  public class ModuleAssignmentAttribute : Attribute
  {
      public Module[] Modules { get; set; }
  
      public ModuleAssignmentAttribute(params Module[] modules)
      {
          Modules = modules;
      }
  }
  </code></pre>
  
  <p>Below is a sample of how the module will be applied to an ASMX service.</p>
  
  <pre><code>[WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [ModuleAssignment(Module.ApplicationModuleA)]
  public class SomeService : System.Web.Services.WebService
  {
      [WebMethod(true)]
      public string GetValue()
      {
          return "Some Service Value";
      }
  }
  </code></pre>
  
  <p>The HTTP module below will be used to authorize access to the service.</p>
  
  <p>public class MyAuthorizationModule : IHttpModule
      {
          public void Dispose()
          {
              //clean-up code here.
          }</p>
  
  <pre><code>    public void Init(HttpApplication context)
      {
          context.PreRequestHandlerExecute += new EventHandler(OnAuthorizeRequest);
      }
  
      public void OnAuthorizeRequest(object sender, EventArgs e)
      {
          if (HttpContext.Current.Handler == null) return;
  
          Attribute att = Attribute.GetCustomAttribute(HttpContext.Current.Handler.GetType(), typeof(ModuleAssignmentAttribute));
  
          if (att != null)
          {
              Module[] modules = ((ModuleAssignmentAttribute)att).Modules;
  
              // Simulate getting the user's active role ID
              int roleId = 1;
  
              // Simulate performing an authz check
              AuthorizationAgent agent = new AuthorizationAgent();
              bool authorized = agent.AuthorizeRequest(roleId, modules);
              if (!authorized)
              {
                  HttpContext.Current.Response.Clear();
                  HttpContext.Current.Response.StatusCode = 401;
                  HttpContext.Current.ApplicationInstance.CompleteRequest();
              }
          }
      }
  </code></pre>
  
  <p>The problem is that, for ASMX web services, the following line of code from the HTTP module returns null (note that this works for ASPX pages).</p>
  
  <pre><code>Attribute att = Attribute.GetCustomAttribute(HttpContext.Current.Handler.GetType(), typeof(ModuleAssignmentAttribute));
  </code></pre>
  
  <p>The value of HttpContext.Current.Handler.GetType() in this situation is "System.Web.Script.Services.ScriptHandlerFactory+HandlerWrapperWithSession".  That type is apparently unaware of the custom attribute defined on the ASMX service.</p>
  
  <p>Any ideas on how to get the custom attribute from the ASMX service type in this scenario?</p>