0

For a project I'm working on, I have to create a web service in c# asp.net web api 2. Right now, I have a basic web service, that when the client calls the service it returns json. This is what I have:

ProductController

public class ProductController : ApiController
    {
        public List<Dictionary<string, object>> Get()
        {
            ProductRepository er = new ProductRepository();

            return getDataRows(er.getModifiedProducts());

        }


        private List<Dictionary<string, object>> getDataRows(DataTable dt)
        {
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;

            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    string output = Regex.Replace(Convert.ToString(col.ColumnName), "[.]", "\\.");
                    row.Add(output, Convert.ToString(dr[col]));
                }

                rows.Add(row);
            }
            return rows;
        }
    }

When a client does a call to the service, it gets all modified products. When the client has all the data received, I want to update the product status, so that when the client does a second call, the products from the first call aren't sent anymore. Before the service can do the update, It has to be sure that the client has received the complete dataset. I was thinking of something with a callback, but I don't know where to start. Can you please give me some pointers?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
kwv84
  • 943
  • 3
  • 11
  • 25
  • its quite simple. I mean, why do you need to check if the client received the data? they will have received it when you do a return getDataRows() call. if they didn't, then it would be some http protocol level error perhaps (or exception thrown from your code). At that point before returning it, you need to have some logic to see its been sent and to not include it in the next request. Alternatively, the client may wish to send back a response code from the previous request but you need to track this by sending some token to uniquely identify the client/call. – Ahmed ilyas Jun 05 '15 at 10:53
  • 3
    @kwv84 this is client-server communication by request-reply pattern. In Web API you can not check at server site if data is reached to client or not. Here you can make another ajax call from client side from the success action of your first ajax request to server. – Rohit Sonaje Jun 05 '15 at 10:53
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jun 05 '15 at 10:58
  • A better design might be for the client to tell you, in each request, which items they've already seen. – Damien_The_Unbeliever Jun 05 '15 at 10:58

1 Answers1

0

I don't know what you mean by updating the status on the server side. It looks like you only need it to send the client only the new products (whatever that means) in the next invocation of the service. That's a violation of the commnon pattern of Web API being stateless. For more information on stateful services, and tto see how you can do it, and why you shouldn't do it, see this SO Q&As: Can we make Restful webservice stateful? and ASP.NET Web API session or something?

It would be a much better solution to include a list of the keys of the products already downloaded to the client in each request, so that the server knows which ones not to send in the response. I don't know the details of your problem, but perhaps, if the new products have an incremental id, a newer date, or something like that you simply have to send a single piece of info so that the server knows what to send to the client.

Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • @JoteBe The thing is that the client can have the products (productcode is unique), but something in the product changes on the server (price, description, etc.). Now, to not expose all products, I thought, I only let the server offer the products with status modified==true. Now, when the client does a call, and all goes well, the server doesn't know that the client has written the data in it's own database. So I thought, if the client tells the server that it has written the modified items, then I can change the modified status on the server. – kwv84 Jun 05 '15 at 11:58
  • The client can make an API call to the server to inform it that he has received and updated the data. What is the problem not to do it like this? – JotaBe Jun 05 '15 at 12:02
  • @JoteBe I'm pretty new to Web Api's so I try to understand. The client (that is a php application btw) makes a call and gets the products. In my controller I have an other function. So the client makes a new call to that function and sends the product id's that it has updated. Then, the web api function receives the id's and updates the database and reports succes. – kwv84 Jun 05 '15 at 13:01
  • That's the idea. Don't forget to check the succes response in the client, so that, if the response doesn't arrive, you can send the ids again to the server. – JotaBe Jun 08 '15 at 08:25