How to implement caching and Ajax features in web services

By
In this tutorial ,i have implemented following things which are given below.
  1. How to use caching concepts in web services in asp.net
  2. How to make Ajax enabled web services in asp.net
  3. How to use Script Manager and Update Panel in web service
I have implemented caching concepts in our previous tutorials which are given below. 
The web services that are not Ajax enabled use the SOAP Protocol to send a request to server and get a response but Ajax enabled web services use the JSON format for this purpose.In our coming tutorial i will expain what is JSON and why we use it.  
Here i have shown 'How to use two or more web services in one application'.

There are some steps to implement this concepts which are given below:-
Step -1:-  First open your visual studio --> File -->New -->Select ASP.NET Empty Website and select Visual C# from left window -->OK-->Now Open Solution Explorer-->Now Right click on website --> Add New Items --> Select web service & Visual C# --> Write your web service's name (mywebservice1.asmx) --> click Add button-->write the following codes as given below:- 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for mywebservice1
/// </summary>
[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 mywebservice1 : System.Web.Services.WebService {

    public mywebservice1 () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    [WebMethod]
    public string GetTime()
    {
        return DateTime.Today.ToLongTimeString();
    }
    [WebMethod(CacheDuration=300)]
    public double sum(double i, double j)
    {
        return i + j;
    }
    [WebMethod]
    public string comparasion(string m,string n)
    {
        if (m == n)
            throw new Exception("Please enter different strings");
      return string.Format("first string is : {0},second string value is : {1}", m, n);
    }
    [WebMethod]
    public int average(int p, int q)
    {
        return (p + q) / 2;
    }
Note:- If you want to call web services from script using asp.net Ajax,then uncomment those line.i have already uncommented it.
Step -2:- Now follow step 1 and create another web service webservice1.1.asmx -->write the following codes as given below:-
using System;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for mywebservice2
/// </summary>
[WebService(Namespace = "mywebservice2")]
[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 mywebservice2 : System.Web.Services.WebService {

    public mywebservice2 () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    //[WebMethod]
    //public string HelloWorld() {
    //    return "Hello World";
    //}
    [WebMethod]
    public int average(int p, int q)
    {
        return (p + q) / 2;
    }
}
 
Step -3:- Now open your visual studio --> File -->New -->Select ASP.NET Empty Website and select Visual C# from left window -->OK-->Now Open Solution Explorer-->Now Right click on website --> Add New Web Form (callmorewebservices.aspx) -->First Drag and drop Script Manager and Update Panel after that drag and drop Label and Text Box inside the Update Panel after that Button control on the Form as shown below:-


Form

Step -4:- Now Run each web services such as mywebservice1.asmx ,  webservice1.1.asmx  -->copy the Browser's URL for each --> Now open  callmorewebservices.aspx -->  open your solution Explorer -->Right Click on your application (website) -->Add Web Reference -->Paste web services URL here and configure it.For More Details of configuration here


configuration
Step -5:- Now write the following c# codes inside the callmorewebservices.aspx.cs file for executing each button handlers as given below:-
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using localhost;
using localhost_1;

public partial class callmorewebservices : System.Web.UI.Page
{
    mywebservice1 myservice = new mywebservice1();
    protected void Page_Load(object sender, EventArgs e)
    {
        string st = myservice.GetTime();
        Label2.Text = st;

    }
    protected void Button1_Click(object sender, EventArgs e)
    { 
double str= myservice.sum(Convert.ToInt32(TextBox1.Text),Convert.ToInt32(TextBox2.Text));
       Label1.Text = "Your sum is :-"+str;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string str1 = myservice.comparasion(TextBox1.Text, TextBox2.Text);
        Label1.Text = "your have Entered "+str1;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {  mywebservice2 myservice2 = new mywebservice2();
int i=myservice2.average(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
      Label1.Text = "Average of integer Number is :-" +i;
    }
}

Step -6:- Now Run the application (callmorewebservices.aspx) --> Enter required field details you will see following output as shown below:-




For More:-
  1. How to create Generic handler in asp.net application
  2. View state and hidden fields in asp.net
  3. Validation controls in asp.net
  4. How to use binding Navigator control
  5. File Handling
  6. How to create Data set and data adopter using Menu
  7. Check constraints in SQL Server
  8. Constructor and Destructor 
  9. Data Integrity in sql server
  10. Ado and Ado.net
  11. Ajax in asp.net
  12. Basic Element are used for compilation of c# codes
Download whole attached file
     Download       

0 comments:

Post a Comment

Powered by Blogger.