asp.net web api - delegatingHandler (webapi) equivalent in servicestack -
i trying migrate servicestack framework asp.net mvc4 webapi framework. have delegatinghandler in webapi equivalent in servicestack?
this validate request , return custom response without going further.
my delegatinghandler
public class xyzdh : delegatinghandler { protected override task<httpresponsemessage> sendasync(httprequestmessage request, cancellationtoken cancellationtoken) { int maxlengthallowed = 200; long? contentlen = request.content.headers.contentlength; if (contentlen > maxlengthallowed) { var defaultresponse = responsehelper.getbaseresponse("content lenght issue", true, uploadlogsizeissue); return task<httpresponsemessage>.factory.startnew(() => { var response = new httpresponsemessage(httpstatuscode.ok) { content = new stringcontent(defaultresponse.tostring(), encoding.utf8, "message/http") }; return response; }); } return base.sendasync(request, cancellationtoken); } }
it's idea glance on servicestack's simple architecture give overall idea of how servicestack put together.
custom hooks, filters , extensibility points
servicestack allows number of custom hooks , extensibility points identified in order of operations wiki page. can use custom filter attribute or global filters lets write directly response after point can call httpresponse.endservicestackrequest()
extension method signal no more processing should happen request.
validators
since you're asking in context of request validation should have @ servicestack's built-in validation allows declarative validation using built-in fluentvalidation.
error handling
in lot of cases throwing normal c# exception need. servicestack provides great story around includes serializing exceptions in service clients. read error handling wiki learn different ways of customizing exception handling.
using global request filter
this how can re-write webapi delegating handler servicestack global request filter:
this.requestfilters.add((httpreq, httpresp, requestdto) => { int maxlengthallowed = 200; if (httpreq.contentlength > maxlengthallowed) { //httpres.statuscode = 200; //no-op, not needed since default //guess "message/http" never heard of httpres.contenttype = "text/plain"; httpres.write("content length issue"); //whatever want in body httpres.endservicestackrequest(); //no more processing request } });
warning: not recommended return 200 ok
invalid http request shown above. if request not valid should 400 badrequest
error servicestack automatically write whenever throw c# exception inheriting argumentexception.
Comments
Post a Comment