AcePL February 2016

Issue with RESTful webservice +JSON+SQL stored procedure project

I know for a fact that there is something I miss. My whole project is somewhat copy&paste of various "how to..."s, my knowledge of C# is basic at best and I need to have it working since our standard web service software is RESTful only when sending.

My main problem is that all solutions I've stumbled upon are actually code snippets, which don't work for me - my C# knowledge is basic so I don't understand how it all works, much less troubleshoot it. I'm pretty sure I'm not even capturing the JSON that's being posted with incoming request. But OTOH I may be wrong.

Requirement: something that works off IIS on WS2012R2, can accept JSON files via HTTPPost, dump content into a SQL Server table and return Id for the row that just was created to the sender of the JSON. I will have to build on it to get full-blown web service that sends and receives multiple JSON files containing different data, all have to end up in SQL Server.

What I 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 ArgumentNullExc        

Answers


Wapac February 2016

OK, so there are several things that you need to change to make it working:

  1. Add parameterless constructor to Note as it will be needed for 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 you want just 1 object, it won't deserialize. You are expecting single object, not array, so don't send array.

  4. You have Type as bool, but you are sending string "1", this will not deserialize to true value as you might expected. Either send true/false (not "true"/"false") or change type of Type to string.

  5. Get rid of that private item field, you don't need it:

    private Note item;

  6. Get rid of those constructors that you have there

    public Note(string json)

    public Note(Note item)

    Not only that they make no sense and won't work, you don't need them as JSON deserializer will fill the fields for you.

EDIT: For example, you say it does not build because there is no more a constructor with one parameter. Of course it does not build, there is this line

Note notesdata = new Note(item);

but you do not need that line. What is the idea behind this line? You want an instance of Note class, but you already have it in "item" variable. You do not need to create a second copy of that. So get rid of this too.

Another reason,

Post Status

Asked in February 2016
Viewed 2,618 times
Voted 9
Answered 1 times

Search




Leave an answer