6 Creating XML Web Services

learningsnortSecurity

Nov 3, 2013 (3 years and 5 months ago)

58 views

Web Services


MIT Level 3


School of Computing Science and Engineering


Page
1

6 Creating XML Web Services

6.1 About XML Web Services

A web service is a class that resides on a web server, whose methods and member variables may be
accessed over the Internet. Hence a client application can use such classes to create objects and perfor
m
tasks as if it were any other class. Web services can in effect turn the whole Internet into a virtual
machine that you the developer will write applications for.

6.2 Underlying Technologies

Data is exchanged between client applications and web services
via the universal standard XML. Any
application that supports XML can use Web services. This is a vast improvement on previous
technologies (e.g. CORBA, DCOM, etc) which were harder to use and required sophisticated
proprietary software to handle data exch
anges.


The client application accesses a web service class via a proxy class. This is assembled client side
using a ‘web service description language’ (WSDL) file. WSDL in effect describes to the client the
remote web service.


Requests for data and subs
equent replies, to and from the class, are handled via a ‘simple object access
protocol’ (SOAP). SOAP in effect encapsulates the XML exchanged.



Creating a web service in Visual Studio.NET is much like writing any other class, except…


You inherit from the super class…


System.Web.Services.WebService


And all web accessible methods should be public and prefixed with…


[WebMethod]


For example the web service…


using

System.Web.Services;


namespace

Hello

{


public

class

Service1 : System
.Web.Services.WebService


{



[WebMethod]



public

string

HelloWorld()



{




return

"Hello World";



}


}

}


Web Services


MIT Level 3


School of Computing Science and Engineering


Page
2

provides a method that outputs ‘Hello World”.


The web service file itself consists of an assembly of files that reside within a directory on you
r IIS
web server. For example…



\
Web_Service_Name
\


The contents of this assembly includes…




Web_Service_Name
.asmx

… the entry point for web the service, when referring to the web
service via a browser or IDE your URL will point to this file.




\
bin
\
Web_S
ervice_Name
.dll

… the ‘dynamic link library’ that contains the web service itself




various other project, info and resource files


As well as creating your own web services you can access (and add to) a Universal Description
Discovery Integration Directory

(UDDI) that provides a huge selection of already available services.

Exercise 6.1 Create your first XML Web Service


To create your first web service from IDE main menu select



‘File
-
>New
-
>Project’

Visual C# Projects

[ASP.NET Web Service]


Ensure you a h
ave a suitable project name (e.g. HelloWorld) and IIS web server running.


You are presented with form window, as shown…





Web Services


MIT Level 3


School of Computing Science and Engineering


Page
3

View the code and uncomment the HelloWorld method to get…


namespace

HelloWorld

{


public

class

Service1 : System.Web.Services.We
bService


{


Some auto generated code






[WebMethod]



public

string

HelloWorld()



{




return

"Hello World";



}


}

}


The code shown above creates a web service class called ‘Service1’ that provides the method
‘HelloWorld’ that simply returns a string

containing ‘Hello World”.


When you Build the Web Service an assembly will be created on your target ISS web server. This
assembly will include a file with the suffix .asmx.


When you run the service via




Debug
-
>Start




or by typing the URL for it’s asmx

file directly into a web Browser


you will be presented with a test web page describing the service, including a link to test it’s member
method.


From the subsequent page [Invoke] the method and note the output will be the XML…




<?
xml version="1.0" e
ncoding="utf
-
8"
?>




<
string

xmlns
="
http://tempuri.org/
">
Hello World
</
string
>



Ok so we have a web service that generates some XML when called with the appropriate SOAP
request. From a developers point of view we want to access this web service from with
in an application
to transfer data to and from a server.

Exercise 6.2 Consuming your first XML Web Service

We will now create a Windows Application to consume our ‘Service1’ web service.


Close previous solution and create a new project via



‘File
-
>New
-
>
Project’ … Visual C# Projects … [Windows Application]


And call it something like



TestWebService


On the resulting Form1 drag a Button and double click this to auto generate a Button Event Handler


private

void

button1_Click(
object

sender, System.EventAr
gs e)


{



}


To have access to our remote web service we need Visual Studio.NET to have a reference to it.


Web Services


MIT Level 3


School of Computing Science and Engineering


Page
4

From main menu select…



Project
-
>Add Web Reference


This will give the ‘Add Web Reference’ dialog box shown.




In the Address field enter the UR
L for your web service,


e.g.
http://your host’s ip address/WebService1/Service1.asmx




You will hence be shown details of the web service including it’s name and available methods.




Hence click on [Add Reference] to make this available to your Windows Application.


Take time to look at the Web Service Description File (WSDL) file via


‘View
-
>Solution Explorer’


as shown….


Web Services


MIT Level 3


School of Computing Science and Engineering


Page
5



Although fairly complex, you can see that this
XML

sch
ema
file
defines the data types transferred
with statements such as


<
s:element

name
="HelloWorldResponse">



<
s:complexType
>


<
s:sequence
>


<
s:element

minOccurs
="0"

maxOccurs
="1"

name
="HelloWorldResult"

type
="s:string"

/>


</
s:sequence
>


</
s:comple
xType
>

</
s:element
>



Essentially Visual Studio uses the Web Services ‘Web Service Description File’ (WSDL file) to create
a proxy class on your client machine that represents the remote web service. You can view this proxy
class via…


‘View
-
>Class View’


For example
WebReference1.Service1

or
localhost.Service1


Next, as in previous weeks, use the proxy class to declare and instantiate and object representing the
web service.


We can do this within the Button’s event handler, for example…


WebReference1
.S
ervice1 myws=
new

WebReference1.Service1();


Hence you can access the object’s HelloWorld method with



myws.HelloWorld()


For example we could use it in a message box



MessageBox.Show(myws.HelloWorld());


Build and run the application, which will show ‘H
ello World’ delivered from our web service when the
Button is clicked.

Web Services


MIT Level 3


School of Computing Science and Engineering


Page
6



We have hence used a client side application to accesses some functionality on a remote server simply
by creating and using an object.

Exercise 6.3

Just like othe
r method
s,

Web service methods can be defined to take arguments
and

return values such
as ints, doubles, strings, and even objects and arrays.


Extend your web service to include a method that takes as it’s input argument a wind speed and returns
a string

describing the wind conditions.


Beaufort Scale

Force

Windspeed

Description

0

0


Calm

1

1
-

3


Light Air

2

4
-

6


Light Breeze

3

7
-

10


Gentle Breeze

4

11
-

16


Moderate Breeze

5

17
-

21


Fresh Breeze

6

22
-

27


Strong Breeze

7

28
-

33


Near Gale

8

34
-

40


Gale

9

41
-

47


Strong Gale

10

48
-

55


Storm

11

56
-

63


Violent Storm

12

64+


Hurricane


Hence extend your windows application to consume the web service giving the user access to a textbox
for inputting data and
a button to invoke the web method. I
n

the button event handler use
System.Convert.ToDouble( )

to get input as number.


double windspeed=System.Convert.ToDouble(textBox1.Text);


and use try and catch to handle invalid inputs.


In keeping with the weather theme, add to your web service methods
to convert to and from Celsius
and Fahrenheit respectively.


To convert Fahrenheit to Celsius, subtract 32 degrees and divide by 1.8.


To convert Celsius to Fahrenheit, multiply by 1.8 and add 32 degrees.


If you have time
add graphics to your consuming a
pplication. For example you could drag a
PictureBox object onto the design area hence load different images depending on the wind or
temperature with statements such as…


Web Services


MIT Level 3


School of Computing Science and Engineering


Page
7

pictureBox1.Image=Image.FromFile("D:
\
\
Temp
\
\
NiceDay.jpg");


Or

pictureBox1.Image=Imag
e.FromFile("NiceDay.jpg");


w
here the jpg image is in the same directory as you
r

web service
’s

executable

(i.e. bin/Debug or bin/Release).


If you have time
also
try
consuming your web service from an ‘ASP.NET Web Application’ instead of
a ‘Windows applic
ation’. What are the advantages and disadvantages of doing this?