2

I read lots of about Web Api. For example i understand Web Service is a kind of Web Api or Web Api is more flexible.

But i didn't get that: Is Web Api future of Web Service?

For example one of our client needs data from our main database. Normally i use a Web Service for this -simple- purpose but this time i created a Web Api project. I got and service data plus i figured out how it works with Entity or Identity etc. But it's not simple as a web service. I think our client will think same thing also because of identity thing. So why should i prefer Web Api vs Web Service or should i prefer Web Api in this -simple- case?

LacOniC
  • 824
  • 12
  • 21
  • 1
    Already asked a lot, please use google. Possible duplicate of [What is the difference between a web API and a web service?](http://stackoverflow.com/questions/19336347/what-is-the-difference-between-a-web-api-and-a-web-service) – L-Four Aug 12 '15 at 14:20
  • 1
    I read them. I just didn't get when to use web service when to use web api. They don't answer this question directly. For example do you use still web services? If you use why don't you use web api? – LacOniC Aug 12 '15 at 14:29
  • While we try to build software, we should think about reusability. Web apis can be used under multiple platforms say mobile phones, tablets and other portable devices as it transfers data in the form of json. Xml is not light weight and creating a restful web service is not as easy as creating a web api. You don't have to worry about the endpoint problems here in web api. This causes the code to be easily maintainable. you can convince your client that the code is easily maintainable that reduces maintenance cost. – staticvoidmain Sep 21 '15 at 17:33

2 Answers2

1

This kind of depends what you mean by 'web service', but for now I'm going to assume you mean the old .net SOAP services.

If you are building something new today (September 2015) you are almost certainly better off using an asp.net web API. This is a standard REST-style service which can be called by almost any HTTP enabled client with no requirements for local software or understanding of how the service works, this is the whole point of the REST architectural style. I blogged a little about web API and REST here: http://blogs.msdn.com/b/martinkearn/archive/2015/01/05/introduction-to-rest-and-net-web-api.aspx

In your case of a simple service that adds CRUD operations to a database using entity framework. This can be very easily achieved with Web API. You can actually scaffold this whole thing based on a simple model.

To answer your specific question, Yes I believe that in eth asp.net world at least, web API is the future of web services. In fact web services have now been dropped in favour of web API.

Web API supports the .net identity model (I blogged on this here: http://blogs.msdn.com/b/martinkearn/archive/2015/03/25/securing-and-working-securely-with-web-api.aspx) and entity framework.

Hope this helps, if it does please mark as an answer or let me know of any more details you need.

Martin Kearn
  • 2,313
  • 1
  • 21
  • 35
0

 public class Service1 : System.Web.Services.WebService
    {

        private List<string> GetLines(string filename) {

            List<string> lines = new List<string>();

            //filename: ime fajla (valute.txt) SA EXT
            using (StreamReader sr = new StreamReader(Server.MapPath("podaci/" + filename))) {
                string line;
                while ((line = sr.ReadLine()) != null) {
                    lines.Add(line);
                }
            }

            return lines;

        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public double ProcitajKursNaDan(DateTime datum, string valuta) {
            List<string> podaci = GetLines("valute.txt");

            double kurs = 0.0;

            // Pronalazenje upisa
            for (int i = 0; i < podaci.Count; i++) {
                string[] linija = podaci[i].Split('|');
                /* Датум[0] | Oznaka valute[1] | Kurs[2] */
                string dat = linija[0];
                string val = linija[1];
                string vrednost = linija[2];

                // Uklanjanje viska
                dat = dat.Trim(); 
                val = val.Trim(); 
                vrednost = vrednost.Trim();

                // Konverzija:
                DateTime datIzFajla = DateTime.ParseExact(dat, "d/M/yyyy", null);

                double kursIzFajla = Convert.ToDouble(vrednost);


                if (DateTime.Compare(datIzFajla, datum) == 0 && val == valuta)
                    kurs = kursIzFajla;
            }


            return kurs;
        }

        [WebMethod]
        public bool UpisiKursNaDan(DateTime datum, string valuta, double Kurs) {
            string date = datum.ToString("d/M/yyyy");
            string linijaZaUpis = date + " | " + valuta + " | " + Kurs.ToString();

            bool success = false;

            try
            {
                StreamWriter sw = new StreamWriter(Server.MapPath("podaci/valute.txt"), true);
                sw.WriteLine(linijaZaUpis);
                sw.Close();

                success = true;
            }
            catch {
                success = false;
            }
            return success;
        }

        [WebMethod]
        public List<string> ProcitajSveValute() {
            List<string> linije = GetLines("valute.txt");

            List<string> ValuteIzFajla = new List<string>();

            for (int i = 0; i < linije.Count; i++) {
                string linija = linije[i];
                string valuta = linija.Split('|')[1];
                valuta = valuta.Trim();
                ValuteIzFajla.Add(valuta);
            }

            List<string> ValuteKraj = ValuteIzFajla.Distinct().ToList();
            return ValuteKraj;
        }
    }
}
//using A10App.localhost;

//namespace A10App
//{
//    public partial class pregledkursa : System.Web.UI.Page
//    {
//        protected void Page_Load(object sender, EventArgs e)
//        {
//            if (!this.IsPostBack) { 
//                Service1 servis = new Service1();
//                List<string> valute = servis.ProcitajSveValute().ToList();
//                for (int i = 0; i < valute.Count; i++)
//                    DropDownList1.Items.Add(valute[i]);
//            }
//        }

//        protected void Button1_Click(object sender, EventArgs e)
//        {
//            string datum = TextBox1.Text;
//            string valuta = DropDownList1.Text;

//            Service1 servis = new Service1();

//            double kurs = servis.ProcitajKursNaDan(DateTime.ParseExact(datum, "d/M/yyyy", null), valuta);

//            if (kurs != 0.0)
//                Label2.Text = kurs.ToString();
//            else
//                Label2.Text = "Nije pronadjen kurs";

//        }
//    }
//}
//namespace A10App
//{
//    public partial class azuriranjeliste : System.Web.UI.Page
//    {
//        protected void Page_Load(object sender, EventArgs e)
//        {
//            if (!this.IsPostBack)
//            {
//                Service1 servis = new Service1();
//                List<string> valute = servis.ProcitajSveValute().ToList();
//                for (int i = 0; i < valute.Count; i++)
//                    DropDownList1.Items.Add(valute[i]);
//            }
//        }

//        protected void Button1_Click(object sender, EventArgs e)
//        {
//            string datum = TextBox1.Text;
//            string valuta = DropDownList1.Text;
//            string kurs = TextBox2.Text;

//            Service1 servis = new Service1();

//            servis.UpisiKursNaDan(DateTime.ParseExact(datum, "d/M/yyyy", null), valuta, Convert.ToDouble(kurs));

//        }
//    }
//}
Marko
  • 18
  • 2