CREATING A .NET WEB SERVICE CONSUMED BY ASP CLIENT

abashedwhimsicalΛογισμικό & κατασκευή λογ/κού

2 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

99 εμφανίσεις

CREATING A .NET WEB SERVICE CONSUMED BY ASP CLIENT

Set up IIS

1. Go to
http://learn.iis.net/page.aspx/28/installing
-
iis
-
7
-
on
-
windows
-
vista
-
and
-
windows
-
7/

and

learn how to

install IIS
(The
IIS features

should include
all about .NET and ASP
)



Open Start
-
>Settings
-
>Control Panel
-
>Administrative tools
-
>Internet Services
Manager.

We will publish this Web Service on IIS running on a local machine. Let's start with

configuring the IIS.

The address
http://localhost

will show the IIS welcome information if
the setting is correct.

If you

have set

the xampp, tomcat, skype
, dropbox

or some other
softwares before
,

there will be a port
con
flict

error. The default port of IIS Default Web
Site is 80.

Here, you have at least two options:

Option 1:

S
top all the services which is occupying port 8
0
(you may need to do it every
time when you power on the computer)


Option 2
: M
odify Default Web Sit
e
to
other ports: Right click the Default Web Site
-
>
Edit Bindings
-
> edit the port
of http 80 to other numbers like 8088, but that means when
entering a website in the Default Web Site, you need to always take the localhost as
http://localhost:8088
,


T
here
are

a lot o
f ways to fix the port conflict
.

Y
ou can find a way from the internet by
yourself.

Here we just use the default port 80.





Expand and right
-
click on [
Default Web Site]; select
Add
Virtual Directory.






Th
e "Virtual Directory Alias" screen opens. Type the virtual directory name

for
example, MyWebServices

and
enter the Physical path like

c:
\
MyWebServices

. and click
OK
.







Writing a Web Service

in .Net


from visual studio
:

Choose

-
>
File
-
>New

Web

Site


Choose .NET Framework 4
-
>ASP .NET Web
Site
,
locate

it

c:
\
MyWebServices
\
WebService

-
>click OK.


Right
click on the project in the solution explorer
-
> add new item



Choose Web Service
, name it
Web
Service
.asmx


Add more WebMethod functions into the

W
ebService.cs


file
(Note that every function
has a head of
[
WebMethod
]
, which means it

can be used by web
)


[WebMethod]


public

int

Add(
int

a,
int

b)


{


return

a + b;


}

[
WebMethod]


public

int

sub(
int

a,
int

b)


{



return

a
-

b;


}

[
WebMethod]


public

int

mul(
int

a,
int

b)


{


return

a * b;


}

[
WebMethod]


public

int

div(
int

a,
int

b)


{


return

a / b;


}

Save and debug the Web Site in Visual Studio by pres
sing

F5


It will show like this




Skip rest of the lab


Now, it is only running in visual studio

s server, let

s

stop visual studio and

run it in IIS.

Here is the step to
setup a website in the IIS:

First
, go to IIS, you can see a folder in

Default Web

Site

-

MyWebServices

-

WebService

(you can try refresh if
it can’
t be seen)

Second
, right click it and choose

Convert to
Application

, click OK

Third
, go to

Application Pools


, check the

Basic Setting


of

Default Web Site


to

.NET 4.0



Now open th
e browser and browse this address

http://localhost/MyWebServices/WebService/WebService.asmx

It will show the exactly same as it shown in visual studio.


After building the Web

Servi
ce

on IIS, we can start a
Consumer

s

website to remotely
call the
[
WebMethod
]
.

Web
-
Based Service Consumer

Use

Visual Studio to create a new website
, name it


Web
C
lient

:


Add a new Item, choose

Web Form(Visual C#)


and name it

Client



Right click on
the website in the Solution Explorer,
click

Add Web Reference

, enter
the service side address click the green button on the right of the address, it will show the
functions in the service site we can call and use. Name the web reference

ServiceReference



Click the
triangle

before

Service Reference


and

WebService.discomap

.
You can see
the wsdl proxy is already
automatically

generated by
V
isual
S
tudio, it helps the client
side to use the function from service side.


Go back to Client.aspx, there is
a file named Client.aspx.cs

under it
, add a using
statement in that file

using

ServiceReference;


Drag Two Textbox, one button, and several Label on the

Client.aspx


and do the
following layout.




Double click on the Button, copy and paste the followin
g code into the function

Button1_Click1





ServiceReference.
WebService

mySvc =
new

WebService
();


Label1.Text = mySvc.Add(
Int32
.Parse(TextBox1.Text),


Int32
.Parse(TextBox2.Text)).ToString();


Label2.Text = mySvc.
sub(
Int32
.Parse(TextBox1.Text),


Int32
.Parse(TextBox2.Text)).ToString();


Label3.Text = mySvc.mul(
Int32
.Parse(TextBox1.Text),


Int32
.Parse(TextBox2.Text)).ToString();


Label4.Text = mySvc.div(
Int32
.Pa
rse(TextBox1.Text),


Int32
.Parse(TextBox2.Text)).ToString();




Press

F5


to debug it,

the result runs like this:



T
hen stop it and test it in the IIS, you also need to
convert the WebClient to Application

First
, go to IIS, you can

see a folder in

Default Web Site

-

MyWebServices

-

WebClient

(you can try refresh if
it can’
t be seen)

Second
, right click it and choose

Convert to
Application”
, click OK

T
h
en you can use browser to see
http://localhost/MyWebServices/WebClient/Client.aspx

And test the function.