Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » ASP.NET 2.0/3.5 » The concept of Web service

The concept of Web service

To understand the concept of web service we will see this artical.

Author Rank :
Page Views : 2951
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Web service:

 

Web services are for specific job on internet. It is not fully based on internet.It is capable to use IIS as host. These services are use to perform work remotely on net. Web services are the platform independent.

 

Web service is simply an application that expose a web accessible API. That means you can invoke this application programmatically over the web. Web services allow application to share data using SOAP (simple object access protocol). SOAP is message based protocol on request and response.

 

Credit card validation we can expose as a web service (this is the best example of web service).

 

There are three attributes for the platform independent and all web services follows these attributes.

 

  • HTTP
  • SOAP
  • XML 

SOAP=HTML+XML.

 

Html is a stateless protocol that is use for communication. We use XML for data transfer because all application understand XML format.

 

Web service can invoke single method on a stateless object. When we add reference of any class library then a dll copy in the debug folder. If we do local property false then we can use that .dll with full namespace.

 

.vsdisco will contain the reference of web method of web service.

 

.asmx is the web service extension in .net. There is no user interface of web service.

We can keep our business logic in web service.

 

Where can we find web service?

 

Web services are registered in the UDDI (universal discovery description and integration) directory. So web service can be found from www.uddi.org.

 

Steps for creating the web service:

 

Step 1: we open the web service in Microsoft visual studio2005. And by default we find a Web method. We can create another method.

 

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

 

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

    public Service ()
    {
 

        //Uncomment the following line if using designed components

        //InitializeComponent();

    }

    [WebMethod]

    public string HelloWorld()

    {

        return "Hello World";

    }   

}

 

Step2: we build and debug service and we get that service as follows: 

 

  

 

Step3: After that copy the URL of the service.

 

For Example: http://localhost:1096/service1/Service.asmx?op=sum.

 

Step4: Now we open the asp.net web application.

 

Step5: We add the reference as add web reference.Then new page will be open.

 

Step6: Paste the URL and click on go then we found service after that click on add reference.

 

Step7: AT the last we writen the code of the application. and debug that application. 

 

Example: Write a program for sum of two integers with the help of web service.

 

Open the asp.net web service and create a WebMethd as Sum().

 

Service.cs

 

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

 

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

    public Service ()

    {

        //Uncomment the following line if using designed components

        //InitializeComponent();

    }

    [WebMethod]

    public int sum(int a,int b)

    {

         return a+b;

    }   

}

 

Build and debug this code and copy the URL. Now open the asp.net application.

 

 

Webform.aspx:

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div><table cellpadding="0" cellspacing="0" border="2" width="50%" height="250px">

    <tr><td align="center" valign="top" style="padding-top:20px;">

        <asp:Label ID="Label1" runat="server" Text="Enter the first no."></asp:Label>

        &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br><br><br>

        <asp:Label ID="Label2" runat="server" Text="Enter the second no"></asp:Label>

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br><br><br>

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

        <asp:Button ID="Button1" runat="server" Text="Sum" OnClick="Button1_Click" /><br><br><br>

        &nbsp;<asp:Label ID="Label3" runat="server" Text="Result"></asp:Label>

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;

        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

    </td></tr></table></div>

</form>

</body>

</html> 

 

 

To add web reference:

We add the reference as add web reference. And new page will be open as follows:



Paste the URL and click on go then we found service : 

 

After that click on add reference button then reference will be add.

 

Web form.aspx.cs:

 

Now we add the reference as add web reference. And write the code on the button click as follows

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class _Default : System.Web.UI.Page

{

    localhost.Service obj = new localhost.Service();

    protected void Button1_Click(object sender, EventArgs e)

    {

        int a, b, c;

        a = Int32.Parse(TextBox1.Text);

        b = Int32.Parse(TextBox2.Text);

        //c = obj.sum(a, b);

        TextBox3.Text =(obj.sum(a,b)).ToString();

    }

} 

 

Now we debug the web application and we found the following output: 

 

  

 

Properties of Webmethod:

 

A web method attributes represents a method for web. Web methods have six properties. These are as follows:

 

  • Description
  • Message name
  • Enable session
  • Cache duration
  • Transaction option
  • Buffer response 

Description:

 

[Web service] and [Web method] attribute have a description property.

 

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

 

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

    public Service ()

    {

         //Uncomment the following line if using designed components 

         //InitializeComponent();

    }

    [WebMethod (Description="this method will add")]

    public int sum(int a, int b, int c)

    {

        return a+b+c;

    }   

}

 

Message name:

 

This is useful property, when we want to overload the web method. 

 

[WebMethod]

public int add(int a, int b, int c)

{

    return a + b + c;

}

[WebMethod (MessageName="add")] //message

public int add(int a)

{

    int s = 0;

    for (int i = 0; i <= a; i++)

        s = s + i;

    return s;

}

 

Enable session:

 

This property is used for to enable in the web services. (Remember web services used http protocol this is stateless). if we want to maintain the state between client and server we need to use this property. Default enable session is false.

 

[WebMethod(EnableSession=true)]

public string HelloWorld()

{

    return "Hello World";

} 

 

Cache duration:

 

When we cache the previously accessed result next time the same user or different user asks we can serve from cache. Here result cached 60 milliseconds. If we invoke this method within sixty minutes, it will print same time. This will increase the web service performance

 

[WebMethod(CacheDuration=60)]

public string showtime()

{

    return DateTime.Now.ToShortTimeString();

}

   

Buffer response:

 

This property is Boolean control web method whether or not to buffer the method's response. 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Purushottam Rathore

I am working as a Software Developer and has 4 years of experience on Microsoft Technology and having a Master Degree in Computer Application. I really like to work in the .NET platform. and working with ASP.NET 2.0/3.5, Web Services, WPF, WCF, Silverlight, AJAX, JavaScript, JQuery, Ado.net, MsAccess, SQL Server 2005/2008.

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
good article by prashant On January 12, 2011
this is a real good article for beginner.
Reply | Email | Modify 
Re: good article by Purushottam On January 13, 2011
Thanks prashant.
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.