Search content within the blog

Monday, April 20, 2009

Web Services - Part II

Create new solution and add new project of type webservice.in this case the webservice name is EmployeeService. Thye namespace consists of two classes 1 an webservice class that has an web method exposed and second a employee class which is not exposed to the client but used in the webservice class. Rest of the code is self explanatory.

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace SampleService
{
///
/// Summary description for EmployeeService
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class EmployeeService : System.Web.Services.WebService
{
private Employee emp= new Employee();
public EmployeeService()
{
emp.Name = string.Empty;
emp.Age = 0;
emp.Salary = 0.0M;
}

[WebMethod(Description = "Accepts a string and returns an object of type employee")]

public Employee GetEmployeeDetails(string strEmpName)
{
Employee objEmployee = new Employee();
objEmployee = emp;

switch (strEmpName.Substring(0, 1))
{
case "A":
case "B":
case "C":
case "D":
case "E":
case "F":
objEmployee.Name = "Mr." + strEmpName;
objEmployee.Age = Convert.ToInt32(Convert.ToChar(strEmpName.Substring(0, 1)));
objEmployee.Salary = 3000.43M;
break;

case "G":
case "H":
case "I":
case "J":
case "K":
case "L":
objEmployee.Name = "Mr." + strEmpName;
objEmployee.Age = Convert.ToInt32(Convert.ToChar(strEmpName.Substring(0, 1)));
objEmployee.Salary = 5000.43M;
break;

default:
objEmployee.Name = "Mr." + strEmpName;
objEmployee.Age = Convert.ToInt32(Convert.ToChar(strEmpName.Substring(0, 1)));
objEmployee.Salary = 7000.43M;
break;



}
return objEmployee;
}


}
//since webmethod is not used here.this clas and any methods inside this class are not exposed to the client.
public class Employee
{
private string _name;
private int _age;
private decimal _salary;

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}

public decimal Salary
{
get
{
return _salary;
}
set
{
_salary = value;
}
}
}
}

run the webservice by right clicking the .asmx file and choosing view in browser.

Now to access that webservice.
ASPX page code

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

<!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="frmEmplyeeServices" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>

ASPX.cs code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using localhost;
public partial class WebServices_EmployeeService : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Employee localEmployee = null;
EmployeeService objEmpSer = new EmployeeService();
localEmployee=objEmpSer.GetEmployeeDetails("A");
if (localEmployee != null)
{
Label1.Text = "Name:"+localEmployee.Name==string.Empty?"Empty":localEmployee.Name+"<br>";
Label1.Text+= "Age:" + localEmployee.Age.ToString() == string.Empty ? "Empty" : localEmployee.Age.ToString() + "<br>";
Label1.Text+= "Salary:" + localEmployee.Salary.ToString() == string.Empty ? "Empty" : localEmployee.Salary.ToString() + "<br>";
}

}
}

No comments:

Post a Comment