c# - Call .fail(error) without throwing exception -
using signalr, there possibility call .fail
instead of .done
when specific values returned hub method?
perhaps using signalr pipeline?
public bool delete(int addressid) { // user should not able delete default address if(addressservice.isdefaultaddressofcustomer(addressid)) return false; // should call .fail() on client addressservice.delete(addressid); return true; // should call .done() on client }
the alternative throw exception avoid since error not server fault, user fault.
assuming convinced exception not right tool you, use custom attribute define mark methods false
return value must translated error, , intercept incoming call buildincoming
hubpipelinemodule
:
from inside there can intercept call original method, inspect if it's marked attribute , if returned false
, if it's case can throw exception there. bottom line is, still throw exception make call .fail()
client-side, exception not bloat business logic. this:
public class failpipelinemodule : hubpipelinemodule { public override func<ihubincominginvokercontext, task<object>> buildincoming(func<ihubincominginvokercontext, task<object>> invoke) { return base.buildincoming(context => { var r = (bool)(invoke(context)).result; if (context.methoddescriptor.attributes.any(a => typeof(failattribute) == a.gettype()) && !r) throw new applicationexception("false"); return task.fromresult((object)r); }); } }
you'll need define failattribute
, use mark hub's method , register failpipelinemodule
@ startup.
Comments
Post a Comment