41
Internet Technologies and Web Application Web Services With ASP.NET Tutorial : Introduction to

Web Services

  • Upload
    taite

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

Tutorial : Introduction to. Web Services. With ASP.NET. Lesson 1: Creating a Web Service. Lesson 1 – Creating a Web Service. Exercise 1.1 – Create and understand a web service Web Service is objects and methods that can be invoked from any client over HTTP. - PowerPoint PPT Presentation

Citation preview

Page 1: Web Services

Inte

rnet

Tech

nolo

gie

s

an

d W

eb

Ap

plicati

on

Web ServicesWith ASP.NET

Tutorial:Introduction to

Page 2: Web Services

Lesson 1:Creating a Web

Service

Page 3: Web Services

Lesson 1 – Creating a Web Service

Exercise 1.1 – Create and understand a web service

Web Service is objects and methods that can be invoked from any client over HTTP.

In this exercise, you will create a simple Web service with “Hello World” method using Visual Studio.NET.

You will test the Web service through its associated documentation web page.

Page 4: Web Services

1. Launch Microsoft Visual Studio.NET 2008.

2. Create new website, choose Visual C#, select location as HTTP and enter site location as http://localhost/itcs426/myService , then click OK.

Lesson 1 – Creating a Web Service

Page 5: Web Services

3. Create new web service, click WebSite ->Add New Item…

4. Select Web Service icon and enter file name as WebService.asmx, then click Add button.

5. The default page of web service class will be created.

Lesson 1 – Creating a Web Service

Page 6: Web Services

1) ASP.NET web service class - kept in App_Code Folder only, used to create web methods

2) “WebService.asmx” file – let the client call web methods

3) “HelloWorld” method – default method in web service class

11

22

33

Lesson 1 – Creating a Web Service

Page 7: Web Services

6. Test the web service, right click on WebService.asmx in solution explorer, then click Set As Start Page, press F5 to run the solution (start debugging).

7. Internet Explorer will start and display a documentation page for the web service as seen in the following illustration.

Lesson 1 – Creating a Web Service

Page 8: Web Services

8. Click “HelloWorld” method to test the operation using the HTTP POST protocol, then click the Invoke button.

9. You will see a resulting XML page that contains the result of the “HelloWorld” method as seen in the following illustration.

Lesson 1 – Creating a Web Service

Page 9: Web Services

Lesson 2:

Passing Parameters and Returning Values in

Web Service

Page 10: Web Services

Exercise 2.1 – Create a web method with parameter passing and returning values

In this exercise, you will create a new web method “HelloUser” which will get the parameter passed from web page and return back the string value.

You will test the Web service through its associated documentation web page.

Lesson 2 – Passing Parameters and Returning Values in Web Service

Page 11: Web Services

1. Create new web method “HelloUser”.[WebMethod]public string HelloUser(string yourName){

return "Hello,"+ yourName;}

2. Press F5 to run the solution. 3. Test the Web service method in browser.

To test the method, click on the link for the method name “HelloUser”, type your name in the text box and then click Invoke button to view the result.

Lesson 2 – Passing Parameters and Returning Values in Web Service

Page 12: Web Services

Lesson 2 – Passing Parameters and Returning Values in Web Service

Page 13: Web Services

Lesson 3:

Adding a Web Reference

Page 14: Web Services

Exercise 3.1 – Add a web reference to the web service

Web reference is proxy class created on the client to connect to the web service running on the server.

In this exercise, you will learn how to add a web reference to the web service.

Lesson 3 – Adding a Web Reference

Page 15: Web Services

1. Click WebSite->Add web Reference.. 2. On Add Web Reference window

Enter URL Web Service: http://HostName/itcs426/myService/WebService.asmx

Host Name: IP Address or domain Web Service Enter your Web Reference name:

WebReferenceClick Add Reference button

Lesson 3 – Adding a Web Reference

Page 16: Web Services

Lesson 3 – Adding a Web Reference

Page 17: Web Services

3. Result in solution explorer.

Lesson 3 – Adding a Web Reference

Page 18: Web Services

Lesson 4:

Calling a Web Service via Web Application

Page 19: Web Services

Exercise 4.1 – Call a web service via web application

In this exercise, you will create an ASP.NET web page that uses the web service you created in lesson 2.

You will test the web service client that will pass the parameter to the web service.

Lesson 4 – Calling a Web Service via Web Application

Page 20: Web Services

1. Open “Default.aspx” page, then add textbox, button, and label in the design view.

Textbox ID: yourName Button ID: callService Label ID: valueReturn

Lesson 4 – Calling a Web Service via Web Application

Page 21: Web Services

2. Create method “callService_Click” for “callService” button.

protected void callService_Click(object sender, EventArgs e)

{//Create new object of web reference for call web service

WebReference.WebService callResult = new WebReference.WebService();

//Call method HelloUser and pass parameter to method//Method HelloUser returns result to label control

valueReturn.Text = callResult.HelloUser(yourName.Text);

}

Lesson 4 – Calling a Web Service via Web Application

Page 22: Web Services

3. Press F5 to run the solution. 4. Test the application in browser. Enter

your name in textbox and click Result button.

Lesson 4 – Calling a Web Service via Web Application

Page 23: Web Services

Lesson 5:

Modifying a Web Service

Page 24: Web Services

Exercise 5.1 – Modify web service by adding more web methods In this exercise, you will add 2 web

methods to your existing web service class.

“getQuestionList” method will return the arrayList for binding to dropDownList.

“checkAnswer” method will get the selected index from dropDownList as a parameter and return the answer in string format.

Lesson 5 – Modifying a Web Service

Page 25: Web Services

1. Create new web methods “getQuestionList” and “checkAnswer” in WebService class.

[WebMethod] public ArrayList getQuestionList() { ArrayList list = new ArrayList(); list.Add("Select Question"); list.Add("What is your name?"); list.Add("What is your student ID?"); return list; }

Lesson 5 – Modifying a Web Service

Page 26: Web Services

[WebMethod] public string checkAnswer(int questionIndex) { string answer; switch (questionIndex) { case 0: answer = "Select Question"; break; case 1: answer = "My name is Enter Name"; break;

Lesson 5 – Modifying a Web Service

Page 27: Web Services

case 2:answer = "My Student ID : Student ID Number";break;

default:answer = "Select Question";break;

return answer; }

Lesson 5 – Modifying a Web Service

Page 28: Web Services

2. In solution explorer, update web reference by right clicking at App_WebReferences folder and click Update Web\Service.

3. Add dropDownList and Label to web page (Default.aspx) in design view.

dropDownList ID: List QuestiondropDownList AutoPostBack: TrueLabel ID: AnswerValue

Lesson 5 – Modifying a Web Service

Page 29: Web Services

3. Add code in code-behind (default.aspx.cs)

protected void Page_Load(object sender, EventArgs e){ if (!IsPostBack) { WebReference.WebService getDataSource; getDataSource = new

WebReference.WebService(); ListQuestion.DataSource =

getDataSource.getQuestionList(); ListQuestion.DataBind();  }}

Lesson 5 – Modifying a Web Service

Page 30: Web Services

protected void ListQuestion_SelectedIndexChanged(object sender, EventArgs e)

{ WebReference.WebService getAnswer; getAnswer = new WebReference.WebService(); AnswerValue.Text =

getAnswer.checkAnswer(ListQuestion.SelectedIndex);} 

Lesson 5 – Modifying a Web Service

Page 31: Web Services

4. Run and test the result in browser.

Lesson 5 – Modifying a Web Service

Page 32: Web Services

Lesson 6:

Web Services in Real Application

Page 33: Web Services

Exercise 6.1 – Call a web service from another machine and display in gridView.

In this exercise, you will learn to call web service from server and display the result in gridView in the form of table.

Lesson 6 – Web Service in Real Application

Page 34: Web Services

1. Add web reference URL:

http://10.22.6.132/CustomerService/Customer.asmx

Web reference name: CustomerUpdate

2. Create new ASP.NET web form “Customer.aspx” in design page. Add textbox, button, and gridView.

Textbox ID: customerNameButton ID: saveCustomerGridView ID: viewCustomerGridView Set AutoFormat: Classic

Lesson 6 – Web Service in Real Application

Page 35: Web Services

Lesson 6 – Web Service in Real Application

Page 36: Web Services

2. Create new ASP.NET web form “Customer.aspx” in design page. Add textbox, button, and gridView.

Textbox ID: customerNameButton ID: saveCustomerGridView ID: viewCustomerGridView Set AutoFormat: Classic

Lesson 6 – Web Service in Real Application

Page 37: Web Services

Lesson 6 – Web Service in Real Application

Page 38: Web Services

3. Add code in code-behind (default.aspx.cs)

protected void loadCustomer() { CustomerUpdate.Service cus = new

CustomerUpdate.Service(); viewCustomer.DataSource = cus.viewCustomer(); viewCustomer.DataBind(); } protected void Page_Load(object sender, EventArgs

e) { if (!IsPostBack){ loadCustomer(); }  }

Lesson 6 – Web Service in Real Application

Page 39: Web Services

protected void saveCustomer_Click(object sender, EventArgs e)

{ CustomerUpdate.Service cus = new

CustomerUpdate.Service(); int result = cus.updateCustomer(CustomerName.Text,

"Your Student ID");  if (result == 1) { loadCustomer(); } else { Response.Write("Insert faile !"); } }

Lesson 6 – Web Service in Real Application

Page 40: Web Services

3. Run and test in browser

Lesson 6 – Web Service in Real Application

Page 41: Web Services

Inte

rnet

Tech

nolo

gie

s

an

d W

eb

Ap

plicati

on