ServiceStack Validation Feature Throws Exception -
i trying implement validation feature in servicestack validate requestdto's before calling db operations. when try validate request dto
validationresult result = this.addbookinglimitvalidator.validate(request);
the code automatically throws validation error automatically. can not debug service happening behind scenes ? can change behaviour or doing wrong here. thanks.
my request dto :
[route("/bookinglimit", "post")] [authenticate] public class addbookinglimit : ireturn<addbookinglimitresponse> { public int shiftid { get; set; } public datetime date { get; set; } public int limit { get; set; } }
my response dto :
public class addbookinglimitresponse { public int id { get; set; } public responsestatus responsestatus { get; set; } }
validation class :
public class addbookinglimitvalidator : abstractvalidator<addbookinglimit> { public addbookinglimitvalidator() { rulefor(r => r.limit).greaterthan(0).withmessage("limit 0 dan büyük olmalıdır"); } }
service implementation :
public addbookinglimitresponse post(addbookinglimit request) { validationresult result = this.addbookinglimitvalidator.validate(request); shift shift = new shiftrepository().get(request.shiftid); bookinglimit bookinglimit = new bookinglimit { restaurantid = base.usersession.restaurantid, shiftid = request.shiftid, startdate = request.date.addhours(shift.starthour.hour).addminutes(shift.starthour.minute), enddate = request.date.addhours(shift.endhour.hour).addminutes(shift.endhour.minute), limit = request.limit, createdate = datetime.now, createdby = base.usersession.userid, status = (byte)status.active }; return new addbookinglimitresponse { id = new bookinglimitrepository().add(bookinglimit) }; }
apphost code :
container.registervalidators(typeof(addbookinglimitvalidator).assembly); plugins.add(new validationfeature());
and consume service in c# code:
try { addbookinglimitresponse response = clienthelper.jsonclient.post(new addbookinglimit { date = datetime.parse(dailybookinglimitdatetextbox.text), limit = convert.toint32(dailybookinglimittextbox.text), shiftid = convert.toint32(dailydaytypeselection.selectedvalue) }); webmanager.showmessage(usermessages.savesuccessful.formatstring(fields.bookinglimit)); } catch (webserviceexception ex) { webmanager.showmessage(ex.responsestatus.message); }
right, servicestack validates request dto before service gets called if validationfeature
enabled.
to manually invoke validator in service, have remove line apphost
first:
plugins.add(new validationfeature());
please make sure validator property in service has type ivalidator<>
, otherwise won't injected ioc container if register validators container.registervalidators(typeof(addbookinglimitvalidator).assembly)
.
public class testservice : service { public ivalidator<request> validator { get; set; } public requestresponse post(request request) { validator.validate(request); ... } }
Comments
Post a Comment