Library tutorials & articles

Building XML Web Services Using C# and ASP.NET

Other web service features

Web Service Attribute
Almost all the time when you're developing for the "real" world, you'll want to provide a description for your web services. You might also want to change the default namespace and give your web service a different name. All of these can be changed using WebService attributes. You can add an attribute and its appropriate properties just before the class declaration, like this:

[WebService(Name="Hello World", Description="Hello World Application developed by James Yang", Namespace="devArticles")]

public class HelloWorld : WebService


When you view the web service page now, it will look like this:

The web service page with web service attributes set

Notice how the name has changed from HelloWorld to Hello World? The description attribute that we specified is also shown. The instructions on how to change the namespace attribute are gone as well, and the namespace is also changed.

The biggest change however, is on the deletion of all of the instructions and template descriptions for the web service. IIS and the compiler assume that by specifying a namespace, our web service is no longer in the development stages and therefore removes unnecessary developer information for us.

Web Method Attribute
Attributes can also be added to each method individually. The available properties are:
  • Description
  • EnableSession
  • MessageName
  • TransactionOption
  • CacheDuration
  • BufferResponse
If you change the line

[WebMethod]

to

[WebMethod(Description ="Hello World Method")]

... then you will see the following change on our web service page:

The web method description attribute

Back to the web method attributes, MessageName is simply an alias for the method. This is particularly useful for parameter-overloaded methods, as requests through a browser cannot display two methods with the same name. The CacheDuration property sets the amount of time that the data will be cached for. This property is an integer and therefore it must contain a whole numeric value. The BufferResponse property is a Boolean property. If set to true, output is buffered and only transmitted once.

The TransactionOption property allows us to specify how transactions will be supported in this method. The available options are:
  • Disabled
  • Notsupported
  • Supported
  • Required
  • RequiredNew
The EnableSession property is a Boolean property and allows us to enable sessions (this requires the use of the proxy object, which will be discussed in another article). Without the proxy object, this web property cannot be implemented as the property is discarded as soon as the value is assigned to it. Without a proxy object, only one request is allowed per session, and assigning a value to the property itself is a request and therefore the session is ended just after.

Web Properties
A web property can be created like this:

string _property;

public string HelloWorldProperty
{
[WebMethod(EnableSession=true)]
get
{
return _property;
}
[WebMethod(EnableSession=true)]
set
{
_property = value;
}
}


Notice how the EnableSession property of our WebMethod attribute is set to true? If you open up your web service page again, then you will see that get and set have been implemented as two separate methods:

Our property implementation

Protocols
ASP.NET supports three different protocol types:
  • HTTP-GET
  • HTTP-POST
  • SOAP
When we pass a parameter on the web services page that we looked at earlier, we're actually passing the value using the HTTP-GET protocol. You can see this from the URL of the returned page, for example:

http://ws1/helloworld/helloworld.asmx/HelloWorldMethod?

"?" is used to pass values with the HTTP_GET protocol. We can pass parameters on the web service web page if we modify the showPost flag of defaultwsdlhelppagegenerator.aspx to true, which located in the c:\winnt\system32\Microsoft.NET\Framework\[Version] directory.

One important thing to notice here is that the two methods shown above return the result in XML format and not SOAP. Results returned as XML can be used in an application by treating the data as an XML document, but there is a better way to use web services.. it's called SOAP.

SOAP is a 3 rd generation protocol that can be used to communicate with web services. It's actually composed of lightweight XML that's dedicated to the transportation of a data structures information and the actual data itself. SOAP is sent using the HTTP-POST protocol:

POST /helloworld/helloworld.asmx HTTP/1.1
Host: ws1
Content-Type: text/xml; charset=utf-8
Content-Length: length goes here
SOAPAction: "devArticles/HelloWorldMethod"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorldMethod xmlns="devArticles" />
</soap:Body>
</soap:Envelope>


.NET understands SOAP and behind the scenes it organizes the resultant data into the right type of application defined data for us. For example, if we use a proxy object to invoke our web service then SOAP is automatically used. If the web service returns a string, then .NET extracts this value and assigns it to a new string object. This string object can then be used in our applications, without even knowing that SOAP was handling the web service request for us!

SOAP and the web service session actually allow us to create an instance of our web service and it automatically calls the appropriate methods for us behind the scenes. This process is called XML Serialization.

For example, if we setup a proxy object for the HelloWorld web service that we created earlier, we could use the following code to invoke it:

HelloWorld hw = new HelloWorld
hw.HelloWorldProperty = "Property Value";
Console.WriteLine (hw.HelloWorldProperty);
hw.HelloWorldMethod();


For the example above to work we would need to use a proxy object, which I will cover in another article.

Comments

  1. 21 Mar 2005 at 21:56
    I got a problem where even after I changed the name, the tempuri persisted, and just figured out that you have to get rid of cached images from:

    C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET FilesNET

    Just wanted to mention this in case someone look for an answer for this.
  2. 22 Jan 2004 at 05:25


    Hi....


    You have to  write the query to check the password. It will be better  if you  use StoredProcedure ..


    Sample of the  stored procedure
    CREATE PROCEDURE _checkpass
    (
    @password varchar(10)
    )


    As
    select password from table where password=@password
    if @@rowcount<1
    select @status=0
    else
    select@status=1
    Go


    Once you write the above stored pro with sql server


    user the following code... in your  application


    myCommand = new SqlCommand("_checkpass", myConnection);
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar, 50));
           myCommand.Parameters["@password"].Value =txtpass.Value;


    myCommand.Connection.Open();


           try
           {
           myCommand.ExecuteNonQuery();


    if((int)status.Value==0)
    {
    Go Ahead........
    }
    else
    {
    Stopped Functioning
    }





  3. 09 Jan 2003 at 20:58
    Posted question without "fully" reading article!!
  4. 09 Jan 2003 at 20:54

    silly question, but as I am new to SQL and C#, how would I be able to check an existing password, that was previously added to your database table and check the result to athenticate the user?

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Building XML Web Services Using C# and ASP.NET .

Leave a comment

Sign in or Join us (it's free).

James Yang James is a student at Georgia Institute of Technology, majoring in Computer Science. He is an MCSE, MCDBA, MCSA and CCNA.

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!