Library tutorials & articles
Building XML Web Services Using C# and ASP.NET
- Introduction
- What is a web service?
- A simple web service
- Other web service features
- Real world application
- Conclusion
Other web service features
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:
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
[WebMethod]
to
[WebMethod(Description ="Hello World Method")]
... then you will see the following change on our web service page:
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
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:
Protocols
ASP.NET supports three different protocol types:
- HTTP-GET
- HTTP-POST
- SOAP
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.
Related articles
Related discussion
-
Create a Site Search Engine in ASP.NET
by Soundguy53 (64 replies)
-
Read HSQLDB data into a webpage
by joe90 (3 replies)
-
Chart insertation in a windows form...
by pdhanik (1 replies)
-
Writing Plugin-Based Applications
by haneen (12 replies)
-
ASP .NET Web Service Error Message ,"Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'."
by salil15august (1 replies)
Related podcasts
-
Writing FaceBook Applications with .NET - Interview with Mel Sampat, author of Outsync
In this episode, Scott talks with Mel Sampat, a Program Manager at Microsoft who's written OutSync, an application that syncs faces between Outlook, Facebook, and indirectly Windows SmartPhones. They chat about what it takes to write your own FaceBook application using ASP.NET or WinForms.
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.
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.
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
}
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?
This thread is for discussions of Building XML Web Services Using C# and ASP.NET .