usingSystem;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace fault1
{// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
[FaultContract(typeof(DivideByZeroException))]
double divide(double i, double j);
}
}
implement interface.....
usingSystem;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace fault1
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class Service1 : IService1
{
public double divide(double a, double b)
{
if (b == 0)
{
DivideByZeroException dib=new DivideByZeroException();
throw new FaultException<DivideByZeroException>(dib,"num 2 is less than 1");
}
return a / b;
}
}
}
now run service..
and consume this ...on .aspx page
like this
design of page:
<%@ 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>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</p>
</form>
</body>
</html>
.cs file code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
mukesh.Service1Client obj = new mukesh.Service1Client();
obj.divide(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text));
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
WCF DATA CONTRACT
-------------------------------------------------------------------------------------------------------------------------------------------
open Iservice1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace datacontract
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
Employee GetEmployeeDetails(int EmpId);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class Employee
{
private string m_Name;
private int m_Age;
[DataMember]
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
[DataMember]
public int Age
{
get { return m_Age; }
set { m_Age = value; }
}
}
}
---------------------------------------------------------------------------------------
now do code on ,service.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace datacontract
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class Service1 : IService1
{
SqlConnection con=new SqlConnection (ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
DataTable dt=new DataTable ();
SqlCommand cmd;
SqlDataReader dr;
public Employee GetEmployeeDetails(int empId)
{
Employee empDetail = new Employee();
con.Open();
cmd = new SqlCommand("select* from emp2 where id='" + empId+"'", con);
cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
if (dr.Read())
{
empDetail.Age =Convert.ToInt32(dr["age"].ToString());
empDetail.Name = Convert.ToString(dr["name"].ToString());
}
return empDetail;
}
}
}
No comments:
Post a Comment