ilp .net stream 04 web service v0.2

67
© Tata Consultancy Services ltd.January 25, 2022 1 TCS Internal Introduction to Web Services Version 1.0

Upload: quizzingjunkie

Post on 08-Apr-2015

70 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: ILP .NET Stream 04 Web Service V0.2

© Tata Consultancy Services ltd. April 11, 2023 1TCS Internal

Introduction to Web Services

Version 1.0

Page 2: ILP .NET Stream 04 Web Service V0.2

2

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Objective

This session deals with the Web Services and how VB.NET Web Forms support web services.

In this session we will learn how to create, instantiate and invoke web services.

Page 3: ILP .NET Stream 04 Web Service V0.2

3

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Scope

Introduction to XML Web Services Web Services Architecture Web Service Life Cycle Web Service Protocols XML Web Service Infrastructure Web Service Web Service Description Language (WSDL) Using Web Service from Client end Web Services using Database connectivity Finding existing Web Services

Page 4: ILP .NET Stream 04 Web Service V0.2

4

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Services in ASP.NET

Web services are services that are available on the web. These web services can be used by the user or by the other web sites.

ASP.NET provides support to XML web services that is based on open Internet standards so that it can be accessed and consumed from any client or Internet enabled device.

ASP.NET provides support for Web Services with the .asmx file.

Page 5: ILP .NET Stream 04 Web Service V0.2

5

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

XML Web Services

In simple words, web services are services that are available on the web. These web services can be used by the user or by the other web sites.

ASP.NET provides support to XML web services that is based on open Internet standards so that it can be accessed and consumed from any client or Internet enabled device.

ASP.NET provides support for Web Services with the .asmx file.

Page 6: ILP .NET Stream 04 Web Service V0.2

6

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

XML Web Services

An XML Web service is a programmable entity that provides a particular element of functionality and is accessible to any number of potentially disparate systems using ubiquitous Internet standards.

An XML Web service can be used either internally by a single application or exposed externally over the Internet for use by any number of applications.

XML Web services use XML-based messaging as a fundamental means of data communication to help bridge the differences that exist between systems that use incongruent component models, operating systems, and programming languages.

Page 7: ILP .NET Stream 04 Web Service V0.2

7

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

XML Web Services

By using XML-based messaging as the mechanism by which the service is created and accessed, both the XML Web service client and the XML Web service provider are freed from need of any knowledge of each other beyond inputs, outputs and location.

Services provided by a web service are in terms of messages the web service accepts and generates rather than how the service is implemented.

Thus, by focusing totally on messages, web services are language, platform and object agnostic.

Page 8: ILP .NET Stream 04 Web Service V0.2

8

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

XML Web Services

In web services, software functionality is exposed as a service that does not have any concern with the consumer of the service.

In .NET, web services are implemented as a part of ASP.NET that handles all web interfaces. It allows programs to talk to each other directly over the web.

Stock tickers, weather information, current financial rates, shipping status and a host of other types of information could be exposed as web services, ready for integration into any application that needs it.

Page 9: ILP .NET Stream 04 Web Service V0.2

9

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Architecture

Page 10: ILP .NET Stream 04 Web Service V0.2

10

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Architecture

The architecture of the web services deals with the working of the web services.

The user of the web services should have the following information: What is the service? How to use the service? Who is the provider of the service? How can he use that service?

All these aspect are necessary before using any web service. For these information developers write code that is known as plumbing

code.

Page 11: ILP .NET Stream 04 Web Service V0.2

11

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Architecture

To discover and description of any the web service there is a contract between the provider and the customer i.e. the user.

This contract contains the information regarding the service description, terms and conditions between both parties and usage information.

Once the client has both the contract and location of the provider, he can use the web service.

The user should know the rules to properly encode (XML) and transmit (HTTP) the request to the service provider.

Page 12: ILP .NET Stream 04 Web Service V0.2

12

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Architecture

Once the provider receives the request it must decode the XML and assemble the transport (HTTP) packets in the correct order and perform the other work to remotely invoke the logic that makes up the service.

The whole process is then reversed to respond to the user.

The provider encodes and transports the message, where user receives, decodes and assembles the message.

The following figure shows the process of communication between a client and an XML Web service.

Page 13: ILP .NET Stream 04 Web Service V0.2

13

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Life Cycle

Page 14: ILP .NET Stream 04 Web Service V0.2

14

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Life Cycle

The following steps describe the events that occur when an XML Web service is called:

The client creates a new instance of an XML Web service proxy class. This object resides on the same computer as the client.

The client invokes a method on the proxy class. The infrastructure on the client computer serializes the arguments of

the XML Web service method into a SOAP message and sends it over the network to the XML Web service.

The infrastructure receives the SOAP message and deserializes the XML. It creates an instance of the class implementing the XML Web service and invokes the XML Web service method, passing in the deserialized XML as arguments.

Page 15: ILP .NET Stream 04 Web Service V0.2

15

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Life Cycle

The XML Web service method executes its code, eventually setting the return value and any out parameters.

The infrastructure on the Web server serializes the return value and out parameters into a SOAP message and sends it over the network back to the client.

The XML Web service infrastructure, on the client computer, receives the SOAP message, deserializes the XML into the return value and any out parameters, and passes them to the instance of the proxy class.

The client receives the return value and any out parameters.

Page 16: ILP .NET Stream 04 Web Service V0.2

16

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Protocols

Protocols define rules that specify the behaviour between services. ASP.NET supports three types of protocols for web services.

HTTP-GET This protocol uses HTTP Get verb to send data to the server. The

Queries are appended in the URL and the information is sent as an extension of URL e.g.

? [name=value]&[name=value] Data is returned as an XML document in the following form <?XML version=”1.0”?> <[data type]> [value] <[data type]>

Page 17: ILP .NET Stream 04 Web Service V0.2

17

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Protocols

HTTP-POST This protocol sends the data to the server in the body of

the message, i.e. you don’t see the queries in the URL. It also returns the data in the identical format to HTTP-

Get. SOAP

SOAP (simple object access protocol) is an open specification submitted to World Wide Web also known as W3C.

Microsoft, IBM.Lotus, Developmentor and UserLand and many more have supported this protocol.

Page 18: ILP .NET Stream 04 Web Service V0.2

18

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service Protocols

SOAP defines a serialization format for request/ response semantics using XML and common transport such as HTTP.

This specification defines the rules for request and response semantics that gives platform independent data exchange model.

Using HTTP-GET and HTTP-POST you can send and receive only name/value pairs of data.

Using the rich XML grammar or SOAP you can send almost any type of data.

Page 19: ILP .NET Stream 04 Web Service V0.2

19

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Infrastructure

Loosely Coupled: Two systems are considered loosely coupled if the only mandate imposed on both systems is to understand the aforementioned self-describing, text-based messages.

Ubiquitous Communication: The ability to connect almost any system or device to the Internet will ensure such systems and devices are universally available to any other system or device connected to the Internet.

Universal Data Format: Utilizing self-describing, text-based messages that XML Web services and their clients can share without the need to know what constitutes each underlying systems enables communication between autonomous and disparate systems.

Page 20: ILP .NET Stream 04 Web Service V0.2

20

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Infrastructure

XML Web Services Directories XML Web services directories provide a central location to locate

XML Web services provided by other organizations. XML Web service clients may or may not need to reference an

XML Web service's directory.XML Web Service Discovery

XML Web service discovery is the process of locating, or discovering, one or more related documents that describe a particular XML Web service using the Web Services Description Language (WSDL).

The DISCO specification defines an algorithm.

Page 21: ILP .NET Stream 04 Web Service V0.2

21

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Infrastructure

XML Web Service Description For interacting with a particular XML Web service, it is

necessary to provide a service description that defines what interactions the XML Web service supports.

XML Web service clients must know how to interact with an XML Web service.

XML Web Service Wire Formats To enable universal communication, XML Web services

communicate using open wire formats, which are protocols understandable by any system capable of supporting the most common Web standards.

Page 22: ILP .NET Stream 04 Web Service V0.2

22

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service

CreationSteps to create “Calculations” Web Service using VS.NET are: Start a new project of type “ASP .NET Web Service” and

rename it to Calculations. As soon as, you select “ASP .NET Web Service” project,

you have to specify the URL for the creation of your web service.

By default, this will take localhost (mapped to c:\Inetpub\wwwroot). But, if you want, you can create a new virtual directory for your web service.

Page 23: ILP .NET Stream 04 Web Service V0.2

23

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service

After hosting your Web Service, you will get the following screen with five files included into the project.

Page 24: ILP .NET Stream 04 Web Service V0.2

24

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service

Go to the code view of .asmx file. In the code view, one namespace “System.Web.Services” is already included into the Calculations web service.

Imports System.Web.Services

This namespace contains the definitions of classes, which enable us to create XML Web Service using ASP.NET. Also, change the class name to Calculations.

You need to keep some points in mind : All web services are saved with an extension of .asmx. Methods that are to be made available in web service must

have <Web Method> attribute before them.

Page 25: ILP .NET Stream 04 Web Service V0.2

25

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service

So write the following four web method below the default web method.

<WebMethod()> Public Function AddNumber(ByVal value1 As Integer, _ ByVal value2 As Integer) As Integer

AddNumber = value1 + value2End Function

<WebMethod()> Public Function SubstractNumber(ByVal value1 As Integer, _ ByVal value2 As Integer) As Integer

SubstractNumber = value1 - value2End Function

Page 26: ILP .NET Stream 04 Web Service V0.2

26

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service

<WebMethod()> Public Function MultiplyNumber(ByVal value1 As _ Integer, ByVal value2 As Integer) As Integer

MultiplyNumber = value1 * value2End Function

<WebMethod()> Public Function DivideNumber(ByVal value1 As _ Integer, ByVal value2 As Integer) As Integer

DivideNumber = value1 / value2End Function

Page 27: ILP .NET Stream 04 Web Service V0.2

27

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service

Save and build the web service using build option from the build menu.

Building a web service will create an assembly (DLL) for the web service in its physical location.

Page 28: ILP .NET Stream 04 Web Service V0.2

28

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Testing

Testing a web service means whether the web service is working as per defined logic or not.

There is no need of deployment of a web service at the client side for testing. Deployment of service has to be done after testing the Web Service.

To test a web service, just run the “Calculations” project. Click on SubstractNumber and see the result. This will open

another screen, which will ask you to input required parameters required for the SubstractNumber logic.

Enter values in both the text boxes and click on "invoke" button, to invoke the logic.

Page 29: ILP .NET Stream 04 Web Service V0.2

29

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Testing

This will open the following screen. This screen is using .XML file to show result onto the screen.

Same way, you can test other methods.

Page 30: ILP .NET Stream 04 Web Service V0.2

30

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

WSDL

Service description language is the public contract for web services. It is an XML document that describes following information.

Service transports: The protocols e.g. HTTP GET is used to access a web

service and their respective end points. Invocation semantics: Defines how requests are made to the service and how

response is given to the user. Call order: Defines the semantics of how a client and server

communicate for a request.

Page 31: ILP .NET Stream 04 Web Service V0.2

31

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using Web Service from Client End

Web Services are created which any client from any location can use

For this, you also have to use the web service into your project/application.

We can consume a Web Service into our project in two ways: Generation of Proxy class Adding Web Reference

Page 32: ILP .NET Stream 04 Web Service V0.2

32

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Generation of Proxy class

In this method, you have to copy .wsdl file into your system and also perform the following steps. Using wsdl.exe create a proxy class of the web service

file (.wsdl). Adding reference to the .dll: Compile this proxy class and

save the resulting .dll file in the bin directory. Now create a new ASP.Net web Application and open

the Web Form which will use the web service.

Page 33: ILP .NET Stream 04 Web Service V0.2

33

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Generation of Proxy class

Saving .WSDL file In the address bar of the browser, type the URL of the “Web

Service”. E.g . type http://localhost/Calculations/Calculations.asmx This will open the web service in the browser. Now append the above address with “?wsdl”. This will open the another window in which the XML schema

is generated for your Web Service. Save this file with an extension of .wsdl. This .wsdl file will now be used to create a proxy class.

Page 34: ILP .NET Stream 04 Web Service V0.2

34

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Generation of Proxy class

Creation of proxy class In ASP.NET there is a utility file known as wsdl.exe. This file is a

command line utility that creates a stub or proxy class from an SDL document.

We can compile this proxy class and use it to call web services without any network code.

This utility supports few commands that are used to while using this exe file, below given table covers all these switches.

For XML Web service clients, the proxy class handles the work of mapping parameters to XML elements and then sending the SOAP message over the network.

Page 35: ILP .NET Stream 04 Web Service V0.2

35

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Generation of Proxy class

By default, the proxy class uses SOAP over HTTP to communicate with the XML Web service.

However, Wsdl.exe can generate proxy classes to communicate with an XML Web service, using either the HTTP-GET protocol or HTTP-POST protocol.

From a command prompt, use Wsdl.exe to create a proxy class, specifying (at a minimum) the URL to an XML Web service or a service description, or the path to a saved service description.

The general syntax for this utility is as follows: Wsdl /language:language /protocol:protocol

/namespace:myNameSpace /out:filename /username:username /password:password /domain:domain <url or path>

Page 36: ILP .NET Stream 04 Web Service V0.2

36

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Compilation of Proxy class

Now we have the proxy class, this class needs to be compiled into .dll.

Then you can either save the resulting .dll file into the bin directory of your ASP.NET Project in which you will use this web service or add reference of the resulting .dll into the project.

To compile a proxy class to a .dll, write the following line at the Visual Studio .NET Command Prompt.

Vbc # /t:library # /R:System.DLL # /R:System.Web.Services.DLL # /R:System.Xml.DLL # /out:Calculations.DLL Calculations.vb

This command will generate calculations.dll file in the same folder where we have .vb file.

Page 37: ILP .NET Stream 04 Web Service V0.2

37

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Use Compiled proxy class at client side

After this step Calculations.dll is ready. In order to use this dll copy it from its present location to bin

directory in your project directory or set the reference to this library.

Start a new “ASP.NET Web Application” project and rename it to TestCalculationWebService.

Add a reference of the resulting .dll into the project by right clicking onto the reference or onto the project.

Now click on “Add Reference” option and a dialog box will be displayed. Click on the first tab i.e., .NET Components and click on Browse button.

Page 38: ILP .NET Stream 04 Web Service V0.2

38

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Use Compiled proxy class at client side

Select the calculations.dll file from the path in which you have stored the .dll file.

Now, design the following interface to test the functionality of the resulting .dll file and give proper name to each control used in the Form.

Page 39: ILP .NET Stream 04 Web Service V0.2

39

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Use Compiled proxy class at client side

In the code window of the form, first import the namespace of the web service. This namespace is created while creating a proxy class.

Imports CalculationSpace

Declare one variable of integer type and other of Calculation class type. These declarations should be made at the module –level.

Dim result As Integer

Dim cal As CalculationSpace.Calculations

Now, write the code in each of the button’s click event handler. After writing the code, save and run the application. This will show the

following screen to the user.

Page 40: ILP .NET Stream 04 Web Service V0.2

40

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Use Compiled proxy class at client side

Similarly, you can call other functions by clicking on different buttons.

Page 41: ILP .NET Stream 04 Web Service V0.2

41

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Adding Web Reference

In this method, you just need to add a “web reference” into your project.

Once reference is added into the project, you can use the web service as a normal referenced file.

Let us see how to use a web service by adding a web reference.

Add a web reference to your project by specifying the complete path of your requested Web Service.

As soon as, you click on “Add Web Reference”, you will get a “Add Web Reference” dialog.

Write the following URL in the address combo. http://localhost/Calculations/Calculations.asmx

Page 42: ILP .NET Stream 04 Web Service V0.2

42

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Adding Web Reference

As soon as you click on “Add Reference” button, this will add the reference of the specified Web Service into your project. This will add three files into the project (.disco, .wsdl, .map)

Now, design the same interface as shown in the previous example to test the “Calculations” web service

Page 43: ILP .NET Stream 04 Web Service V0.2

43

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Adding Web Reference

Declare two module-level variables, one of type integer and other is of type “web service”. Write the following two lines just below the form declaration.

Dim cal As New localhost.Calculations() Dim result As Integer Now, write the respective code on the click event handler of the

buttons.Private Sub cmdAdd_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles cmdAdd.Click result = cal.AddNumber(CInt(txtValue1.Text), _ CInt(txtValue2.Text)) lblResult.Text = "Sum is " & resultEnd Sub

Page 44: ILP .NET Stream 04 Web Service V0.2

44

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Adding Web Reference

Private Sub cmdSubstrat_Click(ByVal sender As Object, _ByVal e As System.EventArgs) Handles cmdSubstrat.Click result = cal.SubstractNumber(CInt(txtValue1.Text), _ CInt(txtValue2.Text)) lblResult.Text = "Substraction is " & resultEnd Sub

Private Sub cmdMultiply_Click(ByVal sender As _System.Object, ByVal e As System.EventArgs) Handles _cmdMultiply.Clickresult = cal.MultiplyNumber(CInt(txtValue1.Text), _ CInt(txtValue2.Text)) lblResult.Text = "Multiplication is " & resultEnd Sub

Page 45: ILP .NET Stream 04 Web Service V0.2

45

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Adding Web Reference

Private Sub cmdDivide_Click(ByVal sender As _

System.Object, ByVal e As System.EventArgs) Handles _

cmdDivide.Click

result = cal.DivideNumber(CInt(txtValue1.Text), _

CInt(txtValue2.Text))

lblResult.Text = "Division is " & result

End Sub

Save and Run the application. Now, enter two integers inside the text boxes and you will see the same output as in the previous example.

Page 46: ILP .NET Stream 04 Web Service V0.2

46

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Web Service using Database Connection

We can create database connectivity either in connected mode or in disconnected mode.

Database connectivity in connected mode is made by using DataReader object and Database connectivity is disconnected mode is made by using DataAdapter object.

Using DataReader This Web Service will accept two arguments, one is user

name and other is password. Both of variables accept string values.

Page 47: ILP .NET Stream 04 Web Service V0.2

47

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataReader

This service will check the existence of user with its corresponding password in the database.

If the user is valid, then a new form is displayed otherwise a message box will come, showing an error message “Not a valid user…try again”.

But, before you start creating the above web service, you must have one database on SQL server with the name “MyData”.

This database must also contain one table ValidUsers, in which details of valid users with their passwords are already stored.

Page 48: ILP .NET Stream 04 Web Service V0.2

48

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataReader

Steps to create and use “LoginForm” web service: Start a new project of type “ASP .NET Web Service” project and

rename it to “LoginForm”. Open the .asmx file and change the class name to “LoginForm”. Import System.Data.SqlClient namespace in .asmx file. Imports System.Data.SqlClient

Declare threee module-level variables of SqlConnection, SqlCommand, SqlDataReader class. Also declare one Boolean type variable also.

Now, write a ValidUser web method which checks for the existence of the user with its corresponding password.

Save and build the service.

Page 49: ILP .NET Stream 04 Web Service V0.2

49

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataReader

To test, whether “LoginForm” web service is working or not. Run the Web service.

Click on “ValidUser” to test the web method. This will open the following screen, which asks you to enter both the input parameters required for “ValidUser” web method.

Page 50: ILP .NET Stream 04 Web Service V0.2

50

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataReader

Click on “invoke” button to invoke the functionality. Now, start another project of type “ASP.NET Web

Application” and create the following interface to test the “LoginForm” web service from the client side.

Page 51: ILP .NET Stream 04 Web Service V0.2

51

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataReader

Assign relevant name to each control and also change the textMode property of textbox

Now, add a reference of the “LoginForm” web service through “Add Web Reference” dialog box.

Now, open the code view of “TestLogin” and write the code in the click event handler of respective buttons.

Now, save and run the application.

Page 52: ILP .NET Stream 04 Web Service V0.2

52

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataAdapter

This Web Service will implement the concept of disconnected database connectivity.

In “GetShowDetails” web service, we have two web methods, GetData() and ShowData().

GetData() method reads all the user created table from the “pubs” database and returns a dataset.

ShowData() methods reads the complete table.

Page 53: ILP .NET Stream 04 Web Service V0.2

53

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataAdapter

Steps to create and use “GetShowDetails” web service: Start a new “ASP .NET Web Service” project and rename it

to “GetShowDetails”. Go to the code view of .asmx file and change the class

name to “GetShowDetails” Import System.Data.SqlClient namespace. This namespace

is required for making connectivity with database.Imports System.Data.SqlClient

Write the following two web methods.

Page 54: ILP .NET Stream 04 Web Service V0.2

54

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataAdapter

<WebMethod()> Public Function GetData() As DataSet Dim str As String str = "data source=tilesd;initial _ catalog=pubs;UID=sa;pwd=;" con = New SqlConnection(str) Dim query As String query = "select name from sysobjects where xtype='U'" Dim DataAdap As SqlDataAdapter = New _ SqlDataAdapter(query, con) Dim dset As DataSet = New DataSet("myDS") DataAdap.Fill(dset) GetData = dsetEnd Function

Page 55: ILP .NET Stream 04 Web Service V0.2

55

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataAdapter

<WebMethod()> Public Function ShowData(ByVal name123 _As String) As DataSet Dim con As SqlConnection Dim str As String str = "data source=tilesd;initial catalog=pubs;_ UID=sa;pwd=;" con = New SqlConnection(str)

Dim query As Stringquery = "select * from " & name123Dim DataAdap As SqlDataAdapter = New _SqlDataAdapter(query, con)Dim ds As DataSet = New DataSet(name123)DataAdap.Fill(ds)ShowData = ds

End Function

Page 56: ILP .NET Stream 04 Web Service V0.2

56

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataAdapter

Save and run the web service. Both the web methods ,GetData and ShowData display in the browser.

You can test the web service, as you did in the previous example. Now, we will use this web service from the client side. But this time, we save the .wsdl file into our system and create and

compile the proxy class. For this, we need .wsdl file. Create a proxy class using wsdl utility from “Visual Studio .NET

Command Prompt”. Write the following code at the command prompt to generate a proxy

class. D:\>wsdl /l:VB /n:GetShowDetailsSpace /o:GetShowDetails.vb

GetShowDetails.wsdl

Page 57: ILP .NET Stream 04 Web Service V0.2

57

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataAdapter

Now compile the proxy file into resulting .dll file by using vbc utility. Write the following line at “Visual Studio .NET Command Prompt”

D:\>vbc /t:library /R:System.DLL /R:System.Web.Services.DLL /R:System.Xml.DLL /R:System.Data.DLL /out:GetShowDetails.DLL GetShowDetails.vb

Either you can store your resulting GetShowDetails.DLL file into the bin directory of the project or you can add a reference of the resulting GetShowDetails.DLL in the project. We will add reference of .dll file.

Design the following interface to test “GetShowDetails” web service.

Page 58: ILP .NET Stream 04 Web Service V0.2

58

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataAdapter

Open the code view of the above Form and import the following two namespaces.

Imports System.Data.SqlClient Imports GetShowDetailsSpace

Page 59: ILP .NET Stream 04 Web Service V0.2

59

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataAdapter

Create and initialize one module-level variable of GetShowDetails, resulting class.

Dim GetShow As New GetShowDetailsSpace.GetShowDetails()

Now, write the following lines of code, in the Form_load event handler. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _

As System.EventArgs) Handles MyBase.Load

Dim dset As DataSet

dset = GetShow.GetData()

Dim dt As DataTable

Dim dc As DataColumn

Dim dr As DataRow

Page 60: ILP .NET Stream 04 Web Service V0.2

60

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Using DataAdapter

For Each dt In dset.Tables

For Each dc In dt.Columns

For Each dr In dt.Rows

cboTable.Items.Add(dr(0))

Next

Next

Next

Dim d As DataSet

d = obj.ShowData(DropDownList1.SelectedItem.Text)

DataGrid1.DataSource = d

DataGrid1.DataBind()

End Sub

Save and run the application.

Page 61: ILP .NET Stream 04 Web Service V0.2

61

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Searching Websites

The Universal Description, Discovery and Integration (UDDI) specification defines a way to publish and discover information about Web services.

UDDI relies on a distributed registry of businesses and their service descriptions that has been implemented in a common XML format.

The UDDI specification consists of several related documents and an XML schema that defines an XMLP based programming protocol for registering and discovering Web services.

Page 62: ILP .NET Stream 04 Web Service V0.2

62

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Searching Websites

Conceptually, the information provided in a UDDI business registration consists of three components:

White Pages, which include address, contact, and known identifiers;

Yellow Pages, which include industrial categorizations based on standard taxonomies; and

Green Pages, which include the technical information about the services that are exposed by the organization.

Businesses individually register information about the Web services that they expose for other businesses to use.

This information can be added to the UDDI business registry through a Web site.

Page 63: ILP .NET Stream 04 Web Service V0.2

63

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Searching Websites

The UDDI business registry is a logically centralized, physically distributed service with multiple root nodes that replicate data with each other regularly.

When a business registers with a single instance of the business registry service, the service shares the data automatically with other UDDI root nodes.

After the data has been distributed, it becomes freely available to anyone who needs to discover which Web services are exposed by a particular business.

Page 64: ILP .NET Stream 04 Web Service V0.2

64

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Searching Websites

For detail information below given site can be referred http://www.uddi.org Also you can find existing Web services at the following Web

sites: http://www.xmethods.net http://www.gotdotnet.com http://dotnet.microsoft.com

Page 65: ILP .NET Stream 04 Web Service V0.2

65

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Summary

The Key Points covered in this session: Web services are based on the Internet protocols. Web services use XML to transfer the data. The NGWS runtime provides built-in support for creating and

exposing Web Services, using a programming abstraction that is consistent and familiar to both ASP.NET Web Forms developers and existing VB users.

ASP.NET provides support for Web Services with the .asmx file.

The class used in the web services is derived from the base class WebService.

Page 66: ILP .NET Stream 04 Web Service V0.2

66

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Summary

Any method that will be accessible as part of the service have the custom attribute “WebMethod” in front of their signatures.

Appending “?wsdl” to the URL into the Internet Explorer address location produces the same information as an XML file, based on the Web Service Description Language (SDL) grammar.

Web Services are based on open protocols such as the Simple Object Access Protocol (SOAP) and HTTP, this client technology can also be used to consume non-ASP.NET Web Services.

Page 67: ILP .NET Stream 04 Web Service V0.2

67

Text in blue and black can be changedPositions in Blue text cannot be alteredText in black can be altered in position and sizes if need beText in yellow ochre is meant for legal matters and updates

TCS Internal

Thank You!