c# - Issue with RESTful webservice +JSON+SQL stored procedure project -


i know fact there miss. whole project copy&paste of various "how to..."s, knowledge of c# basic @ best , need have working since our standard web service software restful when sending.

my main problem solutions i've stumbled upon code snippets, don't work me - c# knowledge basic don't understand how works, less troubleshoot it. i'm pretty sure i'm not capturing json that's being posted incoming request. otoh may wrong.

requirement: works off iis on ws2012r2, can accept json files via httppost, dump content sql server table , return id row created sender of json. have build on full-blown web service sends , receives multiple json files containing different data, have end in sql server.

what have:

class:

    namespace na.models {     public class note     {         public note() { }          //public guid id { get; set; }         public static string client { get; set; }         public static int case { get; set; }         public static string text { get; set; }         public static int noteid { get; set; }         public static string r1 { get; set; }         public static string r2 { get; set; }         public static string s1 { get; set; }         public static datetime date { get; set; }         public static bool type { get; set; }       }     } 

interface:

    namespace na.models {     interface inoterepository     {         ienumerable<note> getall();         void add(note item);     } } 

repository:

namespace na.models {   class notedatarepository : inoterepository   {      public void add(note item)      {         if (item == null)         {             throw new argumentnullexception("item");         }         else         {             string strconnstring = configurationmanager.connectionstrings["constring"].connectionstring;             sqlconnection con = new sqlconnection(strconnstring);             sqlcommand cmd = new sqlcommand();             cmd.commandtype = commandtype.storedprocedure;             cmd.commandtext = "bl_integrationinsertnote";             cmd.parameters.add("@client", sqldbtype.varchar).value = item.client.trim();             cmd.parameters.add("@case", sqldbtype.varchar).value = item.case;             cmd.parameters.add("@text", sqldbtype.varchar).value = item.text.trim();             cmd.parameters.add("@when", sqldbtype.datetime).value = item.date;             cmd.parameters.add("@ext", sqldbtype.bit).value = item.type;             cmd.parameters.add("@return", sqldbtype.int).direction = parameterdirection.output;             cmd.connection = con;              try             {                 con.open();                 cmd.executenonquery();                 string id = cmd.parameters["@return"].value.tostring();                 string lblmessage = null;                 lblmessage = "record inserted successfully. id = " + id;             }             catch (exception ex)             {                 throw ex;             }                         {                 con.close();                 con.dispose();             }         }             return item;     }      ienumerable<note> inoterepository.getall()     {         throw new notimplementedexception("getitems");     } } } 

controller:

namespace na.controllers {    public class nc : apicontroller    {       [route("addnote")]       [httppost]     public httpresponsemessage postnote(list<note> item)     {         //notejson deserializednote = jsonconvert.deserializeobject<notejson>(item);         //note notesdata = new note(item);         //foreach (deserializednote            notesaccept.models.inoterepository repository = new notesaccept.models.notedatarepository();         item = repository.add(item);         var response = request.createresponse < notesaccept.models.note>(httpstatuscode.created, item);          return response;     }    } } 

when trying send test json service error in return:

500: internal server error, value cannot null @ parameter item

this posttestserver.com dump of request sent:

headers (some may inserted server) request_uri = /post.php query_string =  request_method = post gateway_interface = cgi/1.1 remote_port = 56926 remote_addr = ip http_connection = close http_cache_control = max-age=259200 http_x_forwarded_for = 172.16.3.87 http_via = 1.1 koenig.local (squid/3.3.13) http_expect = 100-continue content_length = 153 http_host = posttestserver.com http_accept = application/json content_type = application/json unique_id = vri0cubamguaabvgeesaaaal request_time_float = 1454945393.4611 request_time = 1454945393  no post params.  == begin post body == [{     "client": "client1",     "case": 1,     "text": "text",     "noteid": 2,     "r1": "r1",     "r2": "r2",     "s1": "s1",     "date": "2015-10-26t09:06:46",     "type":"1" }] == end post body ==  upload contains put data: [{     "client": "client1",     "case": 1,     "text": "text",     "noteid": 2,     "r1": "r1",     "r2": "r2",     "s1": "s1",     "date": "2015-10-26t09:06:46",     "type":"1" }] 

the above dump post request identical 1 i'm sending web service, exception of url. can treated actual request. , here iis log:

fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(user-agent) cs(referer) sc-status sc-substatus sc-win32-status time-taken 2016-02-08 15:49:52 ::1 post /blz/addnote - 80 - ::1 - - 500 0 0 2937

ok, there several things need change make working:

  1. add parameterless constructor note needed deserialization:

    public note() { } 
  2. get rid of "static" in note's fields:

    public static string client { get; set; }

    public static int case { get; set; }

    public static string text { get; set; }

    public static int noteid { get; set; }

    public static string r1 { get; set; }

    public static string r2 { get; set; }

    public static string s1 { get; set; }

    public static datetime date { get; set; }

    public static bool type { get; set; }

  3. don't send json array if want 1 object, won't deserialize. expecting single object, not array, don't send array.

  4. you have type bool, sending string "1", not deserialize true value might expected. either send true/false (not "true"/"false") or change type of type string.

  5. get rid of private item field, don't need it:

    private note item;

  6. get rid of constructors have there

    public note(string json)

    public note(note item)

    not make no sense , won't work, don't need them json deserializer fill fields you.

edit: example, not build because there no more constructor 1 parameter. of course not build, there line

note notesdata = new note(item); 

but not need line. idea behind line? want instance of note class, have in "item" variable. not need create second copy of that. rid of too.

another reason, why won't compile rid of static fields, while still have in add method:

        cmd.parameters.add("@text", sqldbtype.varchar).value = note.text.trim();         cmd.parameters.add("@when", sqldbtype.datetime).value = note.date; 

and quite sure not want that. instead, want use instance of object sent you:

        cmd.parameters.add("@text", sqldbtype.varchar).value = item.text.trim();         cmd.parameters.add("@when", sqldbtype.datetime).value = item.date; 

another thing there no reason why add method return object being added db. feel free change this

   public note add(note item) 

to this

   public void add(note item) 

and not return anything, not need it.

i no expert on sqlconnection , things around it, part not comment. use ef in projects working db. there might problems in part, can't comment on that.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -