What is a webservice:
A webservice is, basically, a
function that you can call remotely (aka, it provides
a 'service').. either from
your computer or from another computer.
And it's really easy to create with .NET.
To set up the webservices stuff
on your computer:
1. In add/remove programs: go to add/remove windows
component. Select IIS (Information
Services… or something like that) and install it. You’ll probably need your Windows XP Pro CD
for that (probably). Once you do that,
c:\Inetpub should be created.
2. Open up the command prompt and type
“C:\WINDOWS\Microsoft.NET\Framework\v1.0.4322\aspnet_regiis.exe –i”. Note: the v1.0.4322
thing might be slightly different - that's ok.
Just look in the Framework directory to see what the number would be on
your computer and type that instead.
To create a webservice:
1. In Visual Studio, go to File > New Project > Visual
C# Project > ASP.NET Webservice
2. Create it somewhere in http://localhost/. This is important. It must be there. Don't put it anywhere else or it won't work. (localhost is a really a substitute
for c:\Inetpub\wwwroot - your webservice needs to be
somewhere in that folder - subfolders are fine as well).
3. Uncomment this code:
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
4. Build > Build Solution. (running it won't do
much good)
To access your webservice via a webclient
1. In any windows app, right click References > Add
Web Reference
2. In the URL, enter "http://localhost/WebService1/Service1.asmx". Click go.
3. Click "Add Reference" once it finds it
4. In your code, type in: l
localhost.Service1 webSvc = new
WindowsApplication2.localhost.Service1();
string s = webSvc.HelloWorld();
Debugging a webservice:
A couple quick notes on webservice:
1. Just
because you can access something (say, a file) from a Windows App on your
computer doesn’t mean that your webservice can. Permissions work a little differently.
2.
The webSvc object I created above looks like a normal
object but it doesn’t act like one. Class
constructors work a little differently. If
I write:
localhost.Service1 webSvc = new
WindowsApplication2.localhost.Service1();
string s = webSvc.HelloWorld1();
string t = webSvc.HelloWorld2();
The constructor will NOT be called with the first line
(contrary to what it seems), but it will be called on BOTH of the next two
lines. This is important, because it
means that information within the webservice ‘object’
will not be retained between calls.