Web Services are applications that provide services on the internet. Web services allow for programmatic access of business logic over the Web. Web services typically rely on XML-based protocols, messages, and interface descriptions for communication and access. SOAP over HTTP is the most commonly used protocol for invoking Web services. SOAP defines a standardized format in XML which can be exchanged between two entities over standard protocols such as HTTP.

Example: Google search engine’s web service, e.g., allows other applications to delegate the task of searching over the internet to Google web service and use the result produced by it in their own applications.

What are Web Services?

  • Web services are application components
  • Web services communicate using open protocols
  • Web services are self-contained and self-describing
  • Web services can be discovered using UDDI
  • Web services can be used by other applications
  • XML is the basis for Web services

Web services platform elements:

  • SOAP (Simple Object Access Protocol)
  • UDDI (Universal Description, Discovery and Integration)
  • WSDL (Web Services Description Language)

SOAP is an XML-based protocol to let applications exchange information over HTTP.

Or more simple: SOAP is a protocol for accessing a Web Service.

  • SOAP stands for Simple Object Access Protocol
  • SOAP is a communication protocol
  • SOAP is a format for sending messages
  • SOAP is designed to communicate via Internet
  • SOAP is platform independent
  • SOAP is language independent
  • SOAP is based on XML
  • SOAP is simple and extensible
  • SOAP allows you to get around firewalls
  • SOAP is a W3C standard

WSDL is an XML-based language for locating and describing Web services.

  • WSDL stands for Web Services Description Language
  • WSDL is based on XML
  • WSDL is used to describe Web services
  • WSDL is used to locate Web services
  • WSDL is a W3C standard

UDDI – Universal Description, Discovery and Integration. It is an XML-based standard for describing, publishing, and finding Web services. It is platform independent, open framework and specification for a distributed registry of Web services

  • UDDI stands for Universal Description, Discovery and Integration
  • UDDI is a directory for storing information about web services
  • UDDI is a directory of web service interfaces described by WSDL
  • UDDI communicates via SOAP
  • UDDI is built into the Microsoft .NET platform

Web services use XML to code and to decode data, and SOAP to transport it (using open protocols).

Interoperability has Highest Priority

  • When all major platforms could access the Web using Web browsers, different platforms could interact. For these platforms to work together, Web-applications were developed.
  • Web-applications are simply applications that run on the web. These are built around the Web browser standards and can be used by any browser on any platform.

Uses of Web service:

  • Application integration Web services within an intranet are commonly used to integrate business applications running on different platforms.

For example, a .NET client running on Windows 2000 can easily invoke a Java Web service running on a mainframe or Unix machine to retrieve data from a legacy application.

  • Business integration Web services allow trading partners to engage in e-business allowing them to leverage the existing Internet infrastructure. Organizations can send electronic purchase orders to suppliers and receive electronic invoices. Doing e-business with Web services means a low barrier to entry because Web services can be added to existing applications running on any platform without changing legacy code.
  • Commercial Web services focus on selling content and business services to clients over the Internet similar to familiar Web pages. Unlike Web pages, commercial Web services target applications as their direct users.

Reusable application-components.

  • Web services can offer application-components like: currency conversion, weather reports, or even language translation as services.

Connect existing software.

  • Web services can help to solve the interoperability problem by giving different applications a way to link their data.
  • With Web services you can exchange data between different applications and different platforms.

DISCO:

DISCO is the abbreviated form of Discovery. It is basically used to club or group common services together on a server and provides links to the schema documents of the services it describes may require.

Disco.exe:

The Web Services Discovery tool discovers the URLs of XML Web services located on a Web server and saves documents related to each XML Web service on a local disk.

Create a Web Service:

This sample explains about the creation of sample web service and consuming it.

Step 1: Create a new web service by clicking File->New->WebSite and select “ASP.Net Web Service”

Step 2:

Create a class and methods which is need to be exposed as service. Decorate the class with “WebService” and methods with “WebMethod” attribute.

[WebService(Namespace = “http://tempuri.org/”)]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script,

//using ASP.NET AJAX, uncomment the following line.

// [System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService

{

    public Service () {

       //Uncomment the following line if using designed components

        //InitializeComponent();

    }

   [WebMethod]

    public string HelloWorld() {

        return “Hello World”;

    }

 [WebMethod ]

    public string SayHello(string name) {

        return “Hello ” + name;

    }

}

Step 3:

Run the web service

Step 4: Create the client application to consume the service by clicking File->New->Project and select the Console Application.

Step 5: Right click the project file and select “Add Service Reference”

Step 6:

Create a new instance for proxy class and call the web method “SayHello”

class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.ServiceSoapClient proxy = new
                            ServiceReference1.ServiceSoapClient();
            Console.WriteLine(proxy.SayHello("Ram"));
            Console.ReadLine();
        }
    }
 

Step 7:

Output window are shown

Difference between Add Reference and Add Service reference:

Add Reference is used to add the .Net assemblies and COM components to the project files, where as Add Service Reference is used to create a proxy for the web service.

Where on the Internet would you look for Web services?

http://www.uddi.org

When would you use .NET Remoting and when Web services?

When both service and client are .Net platform, .Net remoting will be more efficient where are if both server and client are different platform use web service for communication

Authentication for Web Services (using SOAP headers):

It had to be simple for the client applications to authenticate, also that the web based administration system had to be used. This prevented me from using the Windows authentication (which is fairly easy to use for the clients of this web service.) By using SOAP headers to pass username and password information, it greatly simplifies any authentication request.

Using the code

I wanted to make it really easy for the client to understand:

protected System.Web.UI.WebControls.DataGrid dgData;

private void Page_Load(object sender, System.EventArgs e)

{

    //simple client

    AuthWebService.WebService webService = new AuthWebService.WebService();

    AuthWebService.AuthHeader authentication = new AuthWebService.AuthHeader();

    authentication.Username = “test”;

    authentication.Password = “test”;

    webService.AuthHeaderValue = authentication;

    //Bind the results – do something here

    DataSet dsData = webService.SensitiveData();

    dgData.DataSource = dsData;

    dgData.DataBind();   

}

Basically all the client needs to do is create an authentication object, fill out the username and password, and then pass them to the web service object. The web service code is also pretty simple; the .NET framework lets you create custom SOAP headers by deriving from the SoapHeader class, so we wanted to add a username and password:

using System.Web.Services.Protocols;

public class AuthHeader : SoapHeader

{

    public string Username;

    public string Password;

}

The next step is to identify the web services that need the authentication, in the example I’ve included it’s the method SensitiveData. To force the use of our new SOAP header we need to add the following attribute to our method:

[SoapHeader (“Authentication”, Required=true)]

So our full definition for our web service method is:

public AuthHeader Authentication;

[SoapHeader (“Authentication”, Required=true)]

[WebMethod (Description=”Returns some sample data”)]

public DataSet SensitiveData()

{

    DataSet data = new DataSet();

     //Do our authentication

    //this can be via a database or whatever

    if(Authentication.Username == “test” && Authentication.Password == “test”)

    {

        //they are allowed access to our sensitive data

        //just create some dummy data

        DataTable dtTable1 = new DataTable();

        DataColumn drCol1 = new DataColumn(“Data”, System.Type.GetType(“System.String”));

        dtTable1.Columns.Add(drCol1);

        DataRow drRow = dtTable1.NewRow();

        drRow[“Data”] = “Sensitive Data”;

        dtTable1.Rows.Add(drRow);

        dtTable1.AcceptChanges();

        data.Tables.Add(dtTable1);

    }else{

        data = null;  

 }           

  return data;

}

 

I should also mention that when I say SOAP headers, I actually mean the soap:Header element in a SOAP request, it has nothing to do with the HTTP headers sent with the request. The SOAP request looks something like:

<?xml version=”1.0″ encoding=”utf-8″?>

<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”&gt;

  <soap:Header>

    <AUTHHEADER xmlns=”http://tempuri.org/”&gt;

      <USERNAME>string</USERNAME>

      <PASSWORD>string</PASSWORD>

    </AUTHHEADER>

  </soap:Header>

  <soap:Body>

    <SENSITIVEDATA xmlns=”http://tempuri.org/&#8221; />

  </soap:Body>

</soap:Envelope>

Advertisements