c# - Executing one Method At the Start of Several other Methods -


i dont have right words have tried best.

say have function in c# such dowork , within function want call function such checkscore(). however, checkscore() generic function want call multiple places.

so in class when create function dowork2, dowork3 there way can execure checkscore() first line instead of typing everytime?

for eg. can avoid

string dowork2(){ checkscore() } 

instead have

string dowork2(){} 

but checkscore() executed anyways?

one potential, though still not foolproof, method abstract security checks attributes. way can decorate methods like:

[checktoken] public string dowork() {   .... } 

this isn't best answer because still requires attribute method. instead create attribute web service class, execute [checktoken] on method call of class.

[checktoken] public class mywebservice {    ... } 

the issue here if have methods want execute different security checks, or no security checks.

a c# web service framework has pretty security features baked framework service stack. http://www.servicestack.net/ has security attributes built in can use, , promotes clean separation of concerns.

another robust option involves intercepting method calls. c# has class "contextboundobject" can used purpose. you'd need have class inherit contextboundobject, , can start dynamically intercept method calls , perform security checking based upon context of method call being made , parameters. contextboundobject add overhead calls, you'll need factor decision. method interception great things security, performance monitoring, health checks, method retries, , other cross cutting concerns.

here's simple getting-started article on contextboundobject (and aspect oriented programming). http://www.codeproject.com/articles/8414/the-simplest-aop-scenario-in-c

for j...

i wouldn't have method code query result. since we're talking web service, there's pipeline involved request initiated client, request sent service, service initializes handlers, deserializes request, routes request appropriate method, executes method, serializes response, , returns response client (this big simplification..). frameworks i've seen have hooks specify attributes on service methods checked @ point prior method execution , can used handle security (ie, return 401 http code web service). believe said he's using wcf , while it's been while since i've used wcf, know can done - see http://msdn.microsoft.com/en-us/library/ms733071.aspx

so derive custom security attribute wcf security attribute , create own authentication logic based upon token, he'd have grab headers of request. servicestack makes super easy, i'd imagine it's not hard using wcf either. chances someone's done wcf , code out there somewhere.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -