开发者

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

开发者 https://www.devze.com 2023-02-17 06:53 出处:网络
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:

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 so开发者_开发问答lution?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消