Thursday 13 June 2019

MVC - Simple Web Service Example - Create and Consume web service in less then 4 minutes

Watch this example on YouTube



1. Publisher

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication11
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public int Add2Numbers(int n1, int n2)
        {
            return n1 + n2;
        }
    }
}
2. Consumer
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            localhost.WebService1 test = new localhost.WebService1();
            var res = test.Add2Numbers(2, 5);
            return View();
        }

No comments:

Post a Comment