Alexey Ryazhskikh
Alexey Ryazhskikh

Reputation: 2500

ASP.NET web services: How to execute custom code before web method invoked?

I have many webmethods in one asmx. I need to perform licence checking before some of this webmethods invoked. I can insert following code to each web method that needs checking:

if (!AllRight())
{
   // badRequest
   return;
}

Or I can insert complex filter to HttpModule to detect webmethod by URL. I need something like attribute for webmethod and place where I can handle it. Is there pretty good solution?

Upvotes: 1

Views: 1667

Answers (1)

VinayC
VinayC

Reputation: 49195

IMO, both of these options are good. Using HttpModule is a good approach - detecting web service call by parsing url is really simple - in short, you are looking for particular asmx (and handling WSDL request). If you want to do selective license check then do simple method name sniffing in URL (as opposed to decorating methods with attribute).

Apart from above options, you have couple of alternatives

  1. Uses some Aspect Oriented Programming frameworks (for example, PostSharp) to inject license checking code by using attribute to decorate the method.

  2. Do it at handler level. Essentially, implement a IHttpHandlerFactory and use it for your asmx end points. The implementation will wrap WebServiceHandlerFactory (or ScriptHandlerFactory in ajax cases) and will return a handler that wraps over the handler object provided by underlying handler. But frankly, this is a brittle solution and essentially same as HttpModule but more complicated.

Upvotes: 2

Related Questions