c# - Error: System.InvalidOperationException in ASP.NET WebAPI -
i'm trying enter data database , access_token on end of successful call.
when make call passing parameters:
everything goes fine, user gets registered , saved database, , access_token returns user:
but, when add signs +, = or \ in deviceid value exception , nothing saved in database:
{ "message": "an error has occurred.", "exceptionmessage": "error getting value 'readtimeout' on 'microsoft.owin.host.systemweb.callstreams.inputstream'.", "exceptiontype": "newtonsoft.json.jsonserializationexception", "stacktrace": " @ newtonsoft.json.serialization.dynamicvalueprovider.getvalue(object target)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.calculatepropertyvalues(jsonwriter writer, object value, jsoncontainercontract contract, jsonproperty member, jsonproperty property, jsoncontract& membercontract, object& membervalue)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializedictionary(jsonwriter writer, idictionary values, jsondictionarycontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializedictionary(jsonwriter writer, idictionary values, jsondictionarycontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serialize(jsonwriter jsonwriter, object value, type objecttype)\r\n @ newtonsoft.json.jsonserializer.serializeinternal(jsonwriter jsonwriter, object value, type objecttype)\r\n @ newtonsoft.json.jsonserializer.serialize(jsonwriter jsonwriter, object value)\r\n @ system.net.http.formatting.basejsonmediatypeformatter.writetostream(type type, object value, stream writestream, encoding effectiveencoding)\r\n @ system.net.http.formatting.jsonmediatypeformatter.writetostream(type type, object value, stream writestream, encoding effectiveencoding)\r\n @ system.net.http.formatting.basejsonmediatypeformatter.writetostream(type type, object value, stream writestream, httpcontent content)\r\n @ system.net.http.formatting.basejsonmediatypeformatter.writetostreamasync(type type, object value, stream writestream, httpcontent content, transportcontext transportcontext, cancellationtoken cancellationtoken)\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.runtime.compilerservices.taskawaiter.getresult()\r\n @ system.web.http.owin.httpmessagehandleradapter.<bufferresponsecontentasync>d__13.movenext()", "innerexception": { "message": "an error has occurred.", "exceptionmessage": "timeouts not supported on stream.", "exceptiontype": "system.invalidoperationexception", "stacktrace": " @ system.io.stream.get_readtimeout()\r\n @ microsoft.owin.host.systemweb.callstreams.delegatingstream.get_readtimeout()\r\n @ getreadtimeout(object )\r\n @ newtonsoft.json.serialization.dynamicvalueprovider.getvalue(object target)" } }
this model definition call:
public class registration { public string username { get; set; } public string password{ get; set; } public string deviceid { get; set; } public string devicename { get; set; } }
field deviceid gets saved database username, , it's definition nvarchar(1024)
is possible nvarchar doesn't allow characters not letters , numbers? did else had problem this?
edit: method problem is:
[route("registration/request")] public async task<httpresponsemessage> registrationrequest(registration model) { try { matrixlogmanager.info("starting token creating."); var request = httpcontext.current.request; var tokenserviceurl = request.url.getleftpart(uripartial.authority) + request.applicationpath + "/token"; matrixlogmanager.info("checking if model valid."); if (!modelstate.isvalid) { return request.createresponse(badrequest(modelstate)); } using (matrixservicelayerlogin login = new matrixservicelayerlogin()) { if (login.loginuser(model.username, model.password, true, true)) { var personid = login.getpersonid(); matrixlogmanager.debug("user " + model.username + "successfully logged in on matrixsts."); try { using (var authrepo = new authrepository()) { applicationuser appuser = new userfactory().createapplicationuser(model, personid); identityresult result = await authrepo.registeruser(appuser); emailservice.sendemail(appuser); ihttpactionresult errorresult = geterrorresult(result); if (errorresult != null) { return request.createresponse(errorresult); } using (var client = new httpclient()) { var requestparams = new list<keyvaluepair<string, string>> { new keyvaluepair<string, string>("grant_type", "password"), new keyvaluepair<string, string>("username", appuser.username), new keyvaluepair<string, string>("password", "0000") }; var requestparamsformurlencoded = new formurlencodedcontent(requestparams); var tokenserviceresponse = await client.postasync(tokenserviceurl, requestparamsformurlencoded); var responsestring = await tokenserviceresponse.content.readasstringasync(); var responsecode = tokenserviceresponse.statuscode; var responsemsg = new httpresponsemessage(responsecode) { content = new stringcontent(responsestring, encoding.utf8, "application/json") }; responsemsg.headers.add("psk", appuser.psk); return responsemsg; } } } catch (exception ex) { matrixlogmanager.error("error: ", ex); throw ex; } } else { return request.createerrorresponse(httpstatuscode.unauthorized, "invalid username or password."); } } } catch (exception ex) { matrixlogmanager.error(string.format("error while trying registring user: exception = {0} innerexception {1}", ex.message, ex.innerexception.message)); throw; } }
try-catch not catch exception, real exception happens here:
public async task<identityresult> registeruser(applicationuser usermodel) { usermodel.twofactorenabled = true; usermodel.psk = timesensitivepasscode.generatepresharedkey(); var result = await _usermanager.createasync(usermodel, "0000"); return result; }
when line result returns client. i've gues saving in line before doesn't go well. set try-catch in part of code , post exception.
this happens when you're wrapping response twice.
consider method returns task<ihttpactionresult>
instead of task<httpresponsemessage>
, note how wrapping errorresult
twice when calling:
return request.createresponse(errorresult);
this leading believe errors in token service causing issue, when double-wrapping in fact :)
consider following:
[route("registration/request")] public async task<ihttpresult> registrationrequest(registration model) { try { matrixlogmanager.info("starting token creating."); var request = httpcontext.current.request; var tokenserviceurl = request.url.getleftpart(uripartial.authority) + request.applicationpath + "/token"; matrixlogmanager.info("checking if model valid."); if (!modelstate.isvalid) { return badrequest(modelstate); } using (matrixservicelayerlogin login = new matrixservicelayerlogin()) { if (login.loginuser(model.username, model.password, true, true)) { var personid = login.getpersonid(); matrixlogmanager.debug("user " + model.username + "successfully logged in on matrixsts."); try { using (var authrepo = new authrepository()) { applicationuser appuser = new userfactory().createapplicationuser(model, personid); identityresult result = await authrepo.registeruser(appuser); emailservice.sendemail(appuser); ihttpactionresult errorresult = geterrorresult(result); if (errorresult != null) { // major change here return errorresult; } using (var client = new httpclient()) { var requestparams = new list<keyvaluepair<string, string>> { new keyvaluepair<string, string>("grant_type", "password"), new keyvaluepair<string, string>("username", appuser.username), new keyvaluepair<string, string>("password", "0000") }; var requestparamsformurlencoded = new formurlencodedcontent(requestparams); var tokenserviceresponse = await client.postasync(tokenserviceurl, requestparamsformurlencoded); var responsestring = await tokenserviceresponse.content.readasstringasync(); var responsecode = tokenserviceresponse.statuscode; var responsemsg = new httpresponsemessage(responsecode) { content = new stringcontent(responsestring, encoding.utf8, "application/json") }; responsemsg.headers.add("psk", appuser.psk); return responsemsg; } } } catch (exception ex) { matrixlogmanager.error("error: ", ex); throw ex; } } else { return request.createerrorresponse(httpstatuscode.unauthorized, "invalid username or password."); } } } catch (exception ex) { matrixlogmanager.error(string.format("error while trying registring user: exception = {0} innerexception {1}", ex.message, ex.innerexception.message)); throw; } }
Comments
Post a Comment