Using Using Using Using Using Using: Webservice Webservicebinding Wsiprofiles
Using Using Using Using Using Using: Webservice Webservicebinding Wsiprofiles
using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
[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 () {
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public int Add(int a, int b)
{
return a + b;
}
1. Click on service description to see the service description. The content will be in xml format
with <wsdl:definition……>
2.Methods
Detail description: The actual content is contains SOAP and HTTP POST
Add
Test
To test the operation using the HTTP POST protocol, click the 'Invoke' button.
Paramete
Value
r
50
a:
50
b:
Invoke
SOAP 1.2
The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced
with actual values.
<a>int</a>
<b>int</b>
</Add>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
HTTP POST
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced
with actual values.
a=string&b=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
Actual code:
App_code/Service.cs
using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;
[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 () {
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public int Add(int a, int b)
{
return a + b;
}
[WebMethod]
public string GetJobDescription(int jobid)
{
SqlConnection cn = new SqlConnection(@"Data Source=HOME-
F42975EEEA\SQLEXPRESS;Initial Catalog=jobs;Integrated Security=True");
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
return dr[0].ToString();
}
else
{
return "-1";
}
}
[WebMethod]
public DataSet GetJobs()//return type is Dataset
{
SqlConnection cn = new SqlConnection(@"Data Source=HOME-
F42975EEEA\SQLEXPRESS;Initial Catalog=jobs;Integrated Security=True");
cn.Open();
DataSet ds = new DataSet();
}
//1st create a class name as job.cs and use this job.cs class as shown below
[WebMethod]
public job GetJobInfo(int jobid)
{
SqlConnection cn = new SqlConnection(@"Data Source=HOME-
F42975EEEA\SQLEXPRESS;Initial Catalog=jobs;Integrated Security=True");
cn.Open();
obj.JobDescription = dr[0].ToString();
obj.MinLevel = Convert.ToInt32(dr[1]);
obj.MaxLevel = Convert.ToInt32(dr[2]);
obj.JobId = Convert.ToInt32(dr[3]);
}
else
{
obj.JobId = -1;
}
return obj;
}
}
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// Summary description for job
/// </summary>
public class job
{
public job()
{
//
// TODO: Add constructor logic here
//
}//constructor
}
now debug it(test it) you will get following screen of Service
Copy the service url which available in address bar and use it for consumption
Note :
Now don’t close the VS.NET of Web Service i.e if you close the web service the consumption doesn’t
occurs as service is closed. So , open another VS.NET form ALL PROGRAMS.
2. Design following. Design the page as for requirement like what inputs should be taken and what
output should be giver etc….
3. Right click on project and use Add Web Reference and provide the Service URL i.e
http://localhost:2830/Webservice2/Service.asmx (by debugging or testing Service.asmx project)
in the following screen
}
protected void Button1_Click(object sender, EventArgs e)
{//view button code
localhost.Service obj = new localhost.Service();
Label1.Text =
obj.GetJobDescription(int.Parse(TextBox1.Text));
}
}
Default2.aspx
}
protected void Button1_Click(object sender, EventArgs e)
{
localhost.Service obj = new localhost.Service();
GridView1.DataSource = obj.GetJobs();
GridView1.DataBind();
}
}