Você está na página 1de 13

FilenewwebsiteASP.

NET Web serviec(slelct) opens a webservice page

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 () {

//Uncomment the following line if using designed components


//InitializeComponent();
}

[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public int Add(int a, int b)
{
return a + b;
}

Run it normally using debug(this is testing)

opens a Service in Browser

1. Click on service description to see the service description. The content will be in xml format
with <wsdl:definition……>
2.Methods

1.Click here to see xml


content (service description)

click on Add it opens a following window asking inputs for Add()

Provide following input and click on Invoke button


Result will be : 100 in new window(browser) in xml format.

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.

POST /Webservice2/Service.asmx HTTP/1.1


Host: localhost Soap style of creating instance like In c# Eg: demoservice obj = new demos
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>


<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
Calling Add() method in soap style taking integers as input for “a” and “b” as shown in above screen given a=50 an
<Add xmlns="http://tempuri.org/">

<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

<?xml version="1.0" encoding="utf-8"?>


<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
Soap response style ie. Result is int (return type ie 100 for the above input in xml) and displayed in ne
<soap12:Body>
<AddResponse xmlns="http://tempuri.org/">
<AddResult>int</AddResult>
</AddResponse>
</soap12:Body>
</soap12:Envelope>

HTTP POST
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced
with actual values.

POST /Webservice2/Service.asmx/Add HTTP/1.1


Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

a=string&b=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>


<int xmlns="http://tempuri.org/">int</int>

Copy the web service url:

Our web service url is : http://localhost:2830/Webservice2/Service.asmx available in address bar of the


browser
This service url is used for consumption

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 () {

//Uncomment the following line if using designed components


//InitializeComponent();
}

[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();

string str = "select jobdesc from jobs where jobid="+jobid;

SqlCommand cmd = new SqlCommand(str,cn);

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();

SqlDataAdapter da = new SqlDataAdapter("select * from jobs", cn);


da.Fill(ds, "myjobs");
return ds;

}
//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();

string str = "select * from jobs where jobid=" + jobid;

SqlCommand cmd = new SqlCommand(str, cn);

job obj = new job();


SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{

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;

}
}

Right click on App_Code in solution explorerselect a “class” and give name:Job.cs

Write following code get and set for jobid,jobdescription , minlevel,maxlevel:

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

public int JobId { get; set; }


public string JobDescription { get; set; }
public int MinLevel { get; set; }
public int MaxLevel { get; set; }

}
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

Out localhost service url is : http://localhost:2830/Webservice2/Service.asmx

Test it for different methods available in service


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.

Consuming web services:

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.

1. File>new>website>asp.net website (name:ConsumeWebService)

Opens Default.aspx page

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

We will get the Service screen of Service.asmx (seen previous) in ConsumeWebService


Default1.aspx

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


{
protected void Page_Load(object sender, EventArgs e)
{

}
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

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


{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
localhost.Service obj = new localhost.Service();
GridView1.DataSource = obj.GetJobs();
GridView1.DataBind();
}
}

Você também pode gostar