How to Create a very Basic Web Service in C# (Visual Studio)

In this blog I am going to show how we could create a very basic Web Service in c#.

1. First of all Open your Visual Studio and GoTo File and Select New Empty Website and rename and open your new empty website.



2. For adding the web service right-click on the solution and then Select Add- Add New Item- Select Web Service(asmx) file, you will get it's class file open as in figure, remove "Hello World" web default method and add code as below::

using System;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 Service : System.Web.Services.WebService
{
    public Service()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    string[,] stocks = {{"Infy","Infosys","122.35"} ,
    {"TCS","Tata Consultancy Servcices","98.22"},
    {"ASN","Aayush Singh","134.31"},
    {"Wipro","Wipro Limited","117.88"}
                       };

    [WebMethod]
    public double getPrice(string symbol)
    {
        for (int i = 0; i < stocks.GetLength(0); i++)
        {
            if (String.Compare(symbol, stocks[i, 0], true) == 0)
            {
                return Convert.ToDouble(stocks[i, 2]);
            }
        }
        return 0;
    }

    [WebMethod]
    public string getName(string symbol)
    {
        for (int i = 0; i < stocks.GetLength(0); i++)
        {
            if (String.Compare(symbol, stocks[i, 0], true) == 0)
            {
                return stocks[i, 1];
            }
        }
        return "Stock not found";
    }

}


3. And at the end check your WebService(.asmx) code in your solution, it sholud be like the same way below in .asmx page::

<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>

4. Now, run your Service.cs page. You will get list of all your Web Methods in browser, click on your method and enter data and press Invoke button you will get result as shown below::




Comments

  1. Heyy thanks for providing this nice info.. i found your site in google search while searching for my web designing company in karimnagar website.. Its nice thanks...

    ReplyDelete
  2. I have been getting a lot of useful and informative material in your website.web design tutorials

    ReplyDelete
  3. Writing a blog is a bit of craftsmanship and the essayist has without a doubt aced this aptitude.
    responsive wordpress website

    ReplyDelete
  4. Find the best essays on is my friend's profile page.
    mason soiza

    ReplyDelete

Post a Comment

Popular posts from this blog

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Dumps for Microsoft Dynamics CRM MB2-703 Practice Exam Questions Free

How to import CSV files into DataTable in C#

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

go to top image