ASP.NET Web Service Basics #mesameergaikwad

 

ASP.NET Web Service Basics

We can now use ASP.NET to create Web Services based on industrial standards including XML, SOAP, and WSDL.

A Web Service is a software program that uses XML to exchange information with other software via common internet protocols. In a simple sense, Web Services are a way of interacting with objects over the Internet.

A web service is

  • Language-Independent.
  • Protocol Independent.
  • Platform Independent.
  • It assumes a stateless service architecture.
  • Scalable (e.g. multiplying two numbers together to an entire customer-relationship management system).
  • Programmable (encapsulates a task).
  • Based on XML (open, text-based standard).
  • Self-describing (metadata for access and use).
  • Discoverable (search and locate in registries)- the ability of applications and developers to search for and locate desired Web services through registries. This is based on UDDI.

Web Service History

  • Microsoft coined the term “Web services” in June 2000, when the company introduced Web services as a key component of its .Net initiative, a broad new vision for embracing the Internet in the development, engineering, and use of the software.
  • As others began to investigate Web services, it became clear that the technology could revolutionize (be the next stage in) distributed computing.
  • Web services en com a set of related standards that can enable any two computers to communicate and exchange data via a network, such as the Internet.
  • The primary standard used in Web services is the Extensible Markup Language (XML) developed by the World Wide Web Consortium (W3C).
  • Developers use XML tags to describe individual pieces of data, forming XML documents, which are text-based and can be processed on any platform.
  • XML provides the foundation for many-core Web services standards (SOAP, WSDL, and UDDI) and vocabularies (XML-based markup for a specific industry or purpose).
  • Almost every type of business can benefit from Web services such as expediting software development, integrating applications and databases, and automating transactions with suppliers, partners, and clients.

Key Web Service Technologies

  • XML- Describes only data. So, any application that understands XML regardless of the application’s programming language or platform has the ability to format XML in a variety of ways (well-formed or valid).
  • SOAP- Provides a communication mechanism between services and applications.
  • WSDL- Offers a uniform method of describing web services to other programs.
  • UDDI- enables the creation of searchable Web services registries.

When these technologies are deployed together, they allow developers to package applications as services and publish those services on a network.

Web services advantages

  • Use open, text-based standards, which enable components written in various languages and for different platforms to communicate.
  • Promote a modular approach to programming, so multiple organizations can communicate with the same Web service.
  • Comparatively easy and inexpensive to implement, because they employ an existing infrastructure and because most applications can be repackaged as Web services.
  • Significantly reduce the costs of enterprise application (EAI) integration and B2B communications.
  • Implemented incrementally, rather than all at once which lessens the cost and reduces the organizational disruption from an abrupt switch in technologies.
  • The Web Services Interoperability Organization (WS-I) consisting of over 100 vendors promotes interoperability.

Web Services Limitations

  • SOAP, WSDL, UDDI- require further development.
  • Interoperability.
  • Royalty fees.
  • Too slow for use in high-performance situations.
  • Increase traffic on networks.
  • The lack of security standards for Web services.
  • The standard procedure for describing the quality (i.e. levels of performance, reliability, security, etc.) of particular Web services — management of Web services.
  • The standards that drive Web services are still in draft form (always will be in refinement).
  • Some vendors want to retain their intellectual property rights to certain Web services standards.

Web Service Example

A web service can perform almost any kind of task.

  • Web Portal- A web portal might obtain top news headlines from an Associated press web service.
  • Weather Reporting- You can use the Weather Reporting web service to display weather information on your personal website.
  • Stock Quote- You can display the latest update of the Share market with Stock Quote on your website.
  • News Headline: You can display the latest news update by using News Headline Web Service on your website.
  • You can make your own web service and let others use it. For example, you can make Free SMS Sending Service with a footer with your companies advertisement, so whosoever uses this service indirectly advertises your company. You can apply your ideas in N no. of ways to take advantage of it.

Example of Creating Web Service in .Net

Here are samples codes that I use to create and consume ASP.NET Web Service:

Step 1- Create the ASP.NET Web Service Source File

Open Visual Studio 2012 and create a new website.->Select .Net Framework 3.5. ->Select ASP.NET Web Service page -> Then, you have to give the name of your service. In this example, I am giving its name “multiplynumberweb services”. Then click the ok button. A screenshot of this activity is given below.

Step 2- Click on the “ok” button; you will see the following window.

Here (in the above figure), you will note that there is the predefined method “HelloWorld” which returns the string “Hello World”. You can use your own method and can perform various operations. Here I made a simple method that returns the multiplication of two numbers using the code.

Service.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. [WebService(Namespace = “http://tempuri.org/")]
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  8. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  9. // [System.Web.Script.Services.ScriptService]
  10. public class Service : System.Web.Services.WebService
  11. {
  12. [WebMethod]
  13. public int Multiplication(int a,int b)
  14. {
  15. return (a*b);
  16. }
  17. }

Before Debugging the above Web Service see some important term

  1. using System.Web.Services;

This directive allows you to refer to objects in the System.Web.Services namespace without having to fully qualify the request. This statement is optional, but if it is not included, every reference to an object in this namespace must be fully qualified. An example is a next line, which is our class declaration. With the using statement, it looks as follows in C#:

The [WebMethod] attribute

The Service class exposes a single method, the public method Multiplication, which takes two integer arguments and returns the multiplication of two numbers as an integer. To expose a method as a part of a web service, you must decorate it with the WebMethod attribute, which tells the compiler to treat it as such. Any method marked with the WebMethod attribute must be defined as public. Class methods exposed as web services follow the same object-oriented rules as any other class, and therefore methods marked private, protected, or internal are not accessible and will return an error if you attempt to expose them using the WebMethod attribute.

In the Solution Explorer, you will see

Service.asmx- which contains the following code:

  1. <%@ WebService Language=”C#” CodeBehind=”~/App_Code/Service.cs” Class=”Service” %>

The page directive WebService is required and the class is the name of the .NET Class to expose the Web Service, each method exposes as Web Service Class Method needs to have a declarative attribute statement.

The web service directive is similar to the Page directive that begins most .aspx pages. For the Multiplication web service to work, you must assign values to two WebService directive attributes: Language and Class.

The required Language attribute lets .NET know which programming language the class has been written in. As you might guess, the acceptable values for the language attribute are currently C#, VB, and JS for JScript.NET.

The Class attribute, also required, tells ASP.NET the name of the class to expose as a web service. Because a web service application can comprise multiple classes, some of which may not be web services, you must tell .NET which class to expose, a step analogous to declaring a Main() method to indicate the entry point of a .NET console application or component. Note that even if your web service contains only one class, setting this attribute is required.

Now back to our web service.

Step 3- Build Web Service and Run the Web Service for testing by pressing F5 function key.

Copy the URL of this web service for further use.

Click on the Multiplication button to test the web service.

Enter the value of a and b.

By pressing the “Invoke” button an XML file is generated.

Now our web service is ready to use; we just need to create a new website to consume the web service.

Example of Testing Web Service in .Net.

Step 5- Create a Test Web Site by File > New > Web Site > Asp.net Web Site

Name the website, for example, here I have chosen the name “Multiplication”.

Step 6- Right-click Solution Explorer and choose “Add Reference”.

Step 7- Paste the URL of the web service and click on the ‘Go’ button and then ‘Add reference’.

Step 8- Now your web service is ready to use in the Solution Explorer you will see.

Step 9- Go to the design of the Default.aspx page; drag and drop three Textboxes and one button.

Step 10- Go to Default.cs page and on the button click event use the following code.

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. localhost.Service mys = new localhost.Service(); // you need to create the object of the web service
  4. int a = Convert.ToInt32(TextBox1.Text);
  5. int b = Convert.ToInt32(TextBox2.Text);
  6. int c = mys.Multiplication(a, b);
  7. TextBox3.Text = c.ToString();
  8. }

Step 11- After pressing the F5 function key to run the website, you will see:

Enter the number.

Press the show button.

Resources

#mesameergaikwad

Post a Comment

0 Comments