.Net and Web Services: A how to do document

baasopchoppySecurity

Nov 5, 2013 (4 years and 4 days ago)

92 views


.Net and Web Services: A how to do document

Sources:

http://www.c
-
sharpcorner.com/WebForms/WebServicesP1RSR.asp

http://www.c
-
sharpcorner.com/WebForms/WebServicesP2RSR.asp

http://www.c
-
sharpc
orner.com/WebForms/WebServicesP3
RSR.asp

Assemble

Assembly is nothing but .DLLs or .EXEs. An assembly can contain more then one file.
An assembly also includes Metadata a
nd Manifest.

Metadata is same as IDL (Interface definition Language which is used by COM)
Metadata describes about Classes, Interfaces Methods, and types of Assembly.

Every Assembly contains Manifest, which describes Assembly itself like Assembly
Version
, Authors and External Assembly references.



Assembly is produced when we compile the .NET language Source code, this code is
also called Managed code. When we compile Source code the .NET compilers will
produce MSIL code (Microsoft Intermediate Language)

or simply IL. MSIL is not
specific to any processor. This is the same as JAVA byte code. When we execute this
program this MSIL code gets compiled again using JIT (Just in Time compilation) to
PE (portable executable) or Native code (which machine it is r
unning).



Namespaces



This technique groups related types into namespaces so they can be searched and
referenced more easily.



Every class that you create in Visual C#.NET belongs to a namespace. Usually, your
classes belong to a namespace that has the

same name as your project (which is
also the name given by default to the assembly that will contain your classes).
However, you can change the project
-
wide namespace used for all of your classes by
using the Project Properties



For Example



Using Syst
em; // Here is a system is namespace

Using Ragavan.Tools;

// this is my own namespaces this contains some functions and Methods.



Public class Ragavan

{


Public void Main ()


{




Console.WriteLine (“welcome to Sreeni.NET world”);



// console method is there in System Namespace.


}

}



Namespaces provide a hierarchical way of organizing class names to avoid "name
pollution." That's all they do

they have nothing to do with inheritance or
assemblies



Web Services


Web service is
simply an application that exposes a Web
-
accessible API. That means
you can invoke this application programmatically over the Web. Using SOAP Protocol



XML+HTTP =SOAP (Simple Object Access protocol)



Web services allow applications to share data.



Web
services can be called across platforms and operating systems regardless of
programming language.



.NET is Microsoft's platform for XML Web services.



Using SOAP you can invoke the WebSerices. (HTTP
-
GET, HTTP
-
POST, SOAP)



SOAP

-
Simple Object Access prot
ocol is message
-
based protocol. Based on Request
and Response. Here I am not going to explain more details about
SOAP
.

Web Service Application:



Like Pay per view Channel we can make Software as pay per use, using Web service.
For example, let say
Infov
ision Inc

developed Expensive Software for 3D Virtual
Modeling. Lot of company does not want acquire the licensee because it is expensive
and they need to pay for Support etc… instead of this if Infovision makes this
software as a WebService, most of t
he company will use as pay per use.



Another example Credit card validation we can expose as a webservice. (Good
example for webService)

Using VisualStudio.NET we can easily create WebServices. ASP.NET WebService this
project type will be used to creat
e WebService.

Web Services Architecture









Creating a WebService Using VisualStudio.NET



Description:

Part
-
I

we read about
. NET Framework and WebServices

Basics. Here
we are going to learn how to create a WebService using VS.NET (using
VB
.NET.) and
Consume the Webservice from VB.NET Client. This Article is based on VS.NET RC1
Release.

Step 1:


Launch VS.NET IDE and create a new project
,
New project Dialog Box will open and
select

Project Type as a
Visual Basic projects
and Templates as

ASP.NET
WebService
as shown below.




Then click “
OK
” . This will create a virtual directory under Default web site (IIS).


Step 2:


Select
Service1.asmx

and
double click that will take you to the
code

window. As shown
below. WebService will have.
.asmx

extension




In this code window we can add a method (Webmethod) that exposes to call over
the
Internet

this type of method call
ed
WebMethod.



The Attribute

WebMethod
is very important. This will allow method to invoke over the
Web using SOAP.

First we need to specify a
WebMethod

Attribute and the Function or Sub name

For example:



<WebMethod ()> Public Function Add (Num1 as in
teger, Num2 as integer) as
Integer



Return Num1+Num2

End Function


Step3:



In the code window edit
Add

and
Div

Web methods as shown above, and then buil
d
(compile) the project. If compiling successfully, view
Service1.asmx

in Browser. This
will shows
Add

and
Div

methods. (Using HTTP
-
GET). This is generated by VS.NET


Enter the num1 and num2 values click
Invoke,
this will invoke Add method in
Service1.asmx using HTTP
-
GET (this is different from calling a method through SOAP ).

Result




Using HTTP
-
GET we tested the
service1.asmx

webservice. This default Interface, which
provided by VS.NET. Next we will see how to consume this same service using SOAP.

The following CODE SNIPPET gives the information about
SO
AP

Request

and
Response
Messages.




Consuming the WebService from VB.NET Client


1)


First create a new VB.NET Console Application project.

2)


Construct a SOAP Request Message as Shown below








Every SOAP message has
Envelope, Header, and Body.
The above SOAP Request
Message posted to
http://localhost/Mathfunctions/Services1.asmx

URL. The
corresponding SOAP Response message is show below with Result.

Using
POST

Method SOAP Request Message is posted to the Web Server.

SOAP Response Mes
sage as a result.


Summary:

In this article we understand how to Create and test Web Service Using
VisualStudio.NET. You are welcome to contact me through
sragavan@hotmail.com

with your comments or questions
.







A WebMethod represents a Web method. WebMethod has 6 properties they are



1) Description

2) MessageName

3) EnableSession

4) CacheDuration

5) Tr
ansactionOption

6) BufferResponse


WebMethod Example in VB.NET



<WebMethod ()> public Function SayHello ()


Return "Hello Ragavan"

End Function



<WebMethod>

This Attribute tells that this
SayHello

function
exposable over the
net
. This type of method called
WebMethod
.



Example in C#



[WebMethod]

Public string SayHello ()

{


return "Hello Ragavan";

}



Description



Both the
[WebService]
, and
[WebMethod]

attrib
utes have a Description Property.
With this property we can associate documentation with our web Service and
WebMethod.

For example you can use Description Attribute to describe the Webmethod.



[WebMethod (Description="This method will add three intege
r")]

Public int Add (int a, int b, int c)


{

return a+b+c;

}

MessageName



This property useful when we want overloading the WebMethods.



For Example



<WebMethod()>
Public

Function

SayHello(
ByVal

sName
As

String
)
As

String





Return

"Hi" & " " & sN
ame

End

Function



<WebMethod(MessageName:="SayHello")>
Public

Function

SayHello()
As String




Return

"Hi Ragavan"

End

Function



EnableSession



This Property used for to enable the session in WebServices.

(Remember WebServiecs uses HTTP protocol th
is is stateless) .if we want to
maintain the state between client and server we need to use this property. Default
Enablesession is
false



[WebMethod (EnableSession=true)]

Public string SayHiToMS ()

{

return " Hello to .NET ";

}



CacheDuration


When we

cache the previously accessed result. Next time the same user or different
user asks we can
serve

from cache. Here result cached 60 milliseconds. if we
invoke this method with in 60 milliseconds it will print same time . This will increase
the Web Servi
ce performance.


<WebMethod(CacheDuration:=60)>
Public

Function

PrintTime()
As

String


Return

Now().ToString


End

Function


TransactionOption


TransactionOption Can be one of five modes:

Disabled

NotSupported

Supported

Required

RequiresNew


Ev
en though there are five modes, web methods can only participate as the root
object in a transaction. This means both Required and RequiresNew result in a new
transaction being created for the web method. The Disabled, NotSupported, and
Supported settings
result in no transaction being used for the web method. The
TransactionOption property of a web method is set to Disabled by default.



BufferResponse


(boolean) Controls whether or not to buffer the method's response

WebServices Capability


Capability

Web services

Invoke single method on a stateless object

Yes

Invoke multiple methods on a stateful object

No

Have all clients invoke methods on the same
serverside

No

object




Pass through firewalls

Yes

Uses HTTP for communication

Yes

Us
es raw TCP socket for communication

No

Use IIS as host

Yes

Allow custom host

No

Uses SOAP
-
compliant formatting of data

Yes

Uses smaller binary formatting of data

No

Retrieve partial copy of data from complex object

Yes

Retrieve complete c
opy of complex object

No