I am in the
very early stages of creating a web site called Sapago.com.
I will use it as a place to put sample ASP.NET and ASP.NET web
services applications and you will be able to read articles
here and download source and view samples on the Sapago
site.
So my first example will include a very simple web service
which I will consume using a Win Form client. The client will
generate a SOAP message containing a request for a method call.
The server will invoke the method and serialize the result into
a SOAP message which will be returned to the client.
The first step is to create your web service. In my case I
already have an ASP.NET web site. So I open the project and
add a web service to it. This can be done by right clicking
on the solution in the Solution Explorer but in this case I
click “Project” on the menu and then select “Add
Web Service.”
This creates a simple ASMX file in your project. The ASMX files
represent the interfaces to the web service functionality. I
called my file Services.asmx as I intend to add other methods
to it as I write more samples. Somewhere in the class add a
method called ServerTime. It takes no parameters and returns
a string.
[WebMethod(Description="Gets the server time and returns
it as a string")]
public string ServerTime()
{
string time;
time = System.DateTime.Now.ToLongDateString();
return time;
}
Notice that I have a public method preceded by the WebMethod
attribute enclosed in brackets. Using the [WebMethod] tag indicates
to the compiler that the following method should be exposed
by the web service. This WebMethod attribute includes a number
of properties which can be included within parenthesis following
the attribute name. This example will work fine without any
properties but it I chose to use one and will mention a few
others.
In this case I am using the “Description” property.
This property allows one to set the description of the particular
web method. This is visible on the service splash screen when
you call the ASMX file in IE and in the documentation tag of
the WSDL
file.
|