Popular Posts

Friday, April 3, 2009

XML Web Services

XML Web Services

A Web Service (XML Web Service) is a unit of code that can be activated using HTTP requests. Stated another way, a Web Service is an application component that can be remotely callable using standard Internet Protocols such as HTTP and XML. One more definition can be, a Web Service is a programmable URL. Web Services came into existence to deliver distributed computing over the Internet. A major advantage of the Web services architecture is, it allows programs written in different languages on different platforms to communicate with each other in a standards-based way. Simply said, a Web service is a software service exposed on the Web through SOAP, described with a WSDL file and registered in UDDI.

Why XML Web Services?

Today, available technologies like Component Object Model (COM), Remote Method Invocation (RMI), Common Object Request Broker Architecture (CORBA) and Internet Inter-ORB Protocol (IIOP) are used to package application logic into reusable components and are called remotely across multiple platforms. The Internet today consists of tremendous number of heterogeneous systems and a key limitation of all the above said technologies is they are not easily interoperable with these different systems and that limits their effectiveness as a standard method for programming the Web. This is because of the dependencies of these technologies on a particular language or a particular Operating System
or Object-model specific protocols. Web Services on the other hand are very different when compared to the said technologies as they are built upon widely accepted standards that can interoperate easily on the Internet. A key to the success of Web Services is that they use a text-based messaging model to communicate which allows them to operate effectively on different platforms.

Example of a Web Service

There is already an example in the ".NET Defined" section of this Web site. Here is another example similar to that. Consider a commerce site that allows consumers to shop online. After all the shopping has been done this site calculates all the charges and the shipping costs based on a variety of shipping options. This site will have alliances with different shipping companies to ship the products to it's consumers. This site might maintain a set of database tables that describe the shipping options and costs for each shipping company based on their location and services. With this approach, whenever there is a change in shipping options or costs of an existing shipping company change or if a new shipping company forms an alliance with this commerce site and provides it's services the Webmaster of the commerce site has to restructure the database and update them to fit the changes. This approach is not only time consuming but also requires the commerce site to invest extra IT costs to maintain it's database. Now, imagine this commerce site programmatically calling a Web Service on it's site provided by the shipping company. What happens with this approach is, the commerce site can calculate shipping costs based on the shipping option that a consumer specifies in his request and returns the costs in real time. This approach eliminates the need for the commerce site to maintain a separate database table for shipping companies and also all the shipping costs are calculated on the shipping company site and returned to the commerce site in real time.

Some other applications of Web Services are:

* Information sources like stock quotes, weather forecasts, sports scores etc that could easily incorporate into applications
* Services that provide commonly needed functionality for other services. Example, user authentication, usage billing etc
* Services that integrate a business system with other partners

The image below shows Web Services Architecture.





Foundational elements of Web Services

The .NET Framework provides an excellent foundation for building and consuming Web Services. A key to the broad-reach capabilities of these Web Services is a foundation built on Internet Standards that does not rely on any platform, protocol or OS. This foundation provides the following capabilities to Web Services:

* A standard method for describing data
* A standard message format for communicating request and response
* A standard method for describing the capabilities of Web Services
* A method to discover what Web Services are available at any site
* A method to describe what sites provide Web Services

Directories in Vb.net

Files in VB .NET

Working with Directories

We will work with the File and Directory classes in this section. We will create a directory and copy a file into the newly created directory.

File Class

The File class in VB .NET allows us to work with files, allowing to copy, delete and create files.

Directory Class

The Directory class in VB .NET allows us to create and work with Folders/Directories. With this class we can create, edit and delete folders and also maintain drives on the machine.

Code to work with File and Directory Class

The following code will create a directory and copy a file into that new directory. To create a new directory we should use the Directory class's CreateDirectory method. Drag two Button's (Button1, Button2), a TextBox and a OpenFileDialog control from the toolbar onto the Form. We will use the TextBox to specify a location to create the Directory when Button1 is clicked and the OpenFileDialog control to open a file and copy it into the newly created Directory when Button2 is clicked. The namespace to be imported is System.IO. The code for that looks like this:

Imports System.IO
Public Class Form1 Inherits System.Windows.Forms.Form

'Windows Form Designer Generated Code

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click

Try
Directory.CreateDirectory(TextBox1.Text)
'Creating a directory by specifying a path in the TextBox, of the form c:\examples
'Instead of using a TextBox you can directly type the location of the directory like this
'Directory.CreateDirectory("c:\examples")
Catch
End Try
MsgBox("Done")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button2.Click
Try
If OpenFileDialog1.ShowDialog <> DialogResult.Cancel Then
File.Copy(OpenFileDialog1.FileName, TextBox1.Text & "\" & _
OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("\")))
'The above line of code uses OpenFileDialog control to open a dialog box where you
'can select a file to copy into the newly created directory
End If
Catch
End Try
MsgBox("File Copied Successfully")
End Sub
End Class

That's all it takes to create a directory and copy a file into the newly created directory in VB.NET. The image below shows output from above code.



The image below shows the directory I created and the file I copied to the new directory using the code above

File Handling

Files in VB .NET

Working with Files

File handling in Visual Basic is based on System.IO namespace with a class library that supports string, character and file manipulation. These classes contain properties, methods and events for creating, copying, moving, and deleting files. Since both strings and numeric data types are supported, they also allow us to incorporate data types in files. The most commonly used classes are FileStream, BinaryReader, BinaryWriter, StreamReader and StreamWriter.

FileStream Class

This class provides access to standard input and output files. We use the members of FileAccess, FileMode and FileShare Enumerations with the constructors of this class to create or open a file. After a file is opened it's FileStream object can be passed to the Binary Reader, BinaryWriter, Streamreader and StreamWriter classes to work with the data in the file. We can also use the FileStreamSeek method to move to various locations in a file which allows to break a file into records each of the same length.

StreamReader and StreamWriter Class

The StreamReader and StreamWriter classes enables us to read or write a sequential stream of characters to or from a file.

BinaryReader and BinaryWriter Class

The BinaryReader and BinaryWriter classes enable us to read and write binary data, raw 0's and 1's, the form in which data is stored on the computer.

The following examples puts some code to work with textual data using FileStream and StreamReader and StreamWriter classes.

Code to create a File

Imports System.IO
'NameSpace required to be imported to work with files
Public Class Form1 Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles MyBase.Load
Dim fs as New FileStream("file.doc", FileMode.Create, FileAccess.Write)
'declaring a FileStream and creating a word document file named file with
'access mode of writing
Dim s as new StreamWriter(fs)
'creating a new StreamWriter and passing the filestream object fs as argument
s.BaseStream.Seek(0,SeekOrigin.End)
'the seek method is used to move the cursor to next position to avoid text to be
'overwritten
s.WriteLine("This is an example of using file handling concepts in VB .NET.")
s.WriteLine("This concept is interesting.")
'writing text to the newly created file
s.Close()
'closing the file
End Sub
End Class

The default location where the files we create are saved is the bin directory of the Windows Application with which we are working. The image below displays that.



Code to create a file and read from it

Drag a Button and a RichTextBox control onto the form. Paste the following code which is shown below.

Imports System.IO
'NameSpace required to be imported to work with files
Public Class Form1 Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal....., Byval.....)Handles Button1.Click
Dim fs as New FileStream("file.doc", FileMode.Create, FileAccess.Write)
'declaring a FileStream and creating a document file named file with
'access mode of writing
Dim s as new StreamWriter(fs)
'creating a new StreamWriter and passing the filestream object fs as argument
s.WriteLine("This is an example of using file handling concepts in VB .NET.")
s.WriteLine("This concept is interesting.")
'writing text to the newly created file
s.Close()
'closing the file

fs=New FileStream("file.doc",FileMode.Open,FileAccess.Read)
'declaring a FileStream to open the file named file.doc with access mode of reading
Dim d as new StreamReader(fs)
'creating a new StreamReader and passing the filestream object fs as argument
d.BaseStream.Seek(0,SeekOrigin.Begin)
'Seek method is used to move the cursor to different positions in a file, in this code, to
'the beginning
while d.peek()>-1
'peek method of StreamReader object tells how much more data is left in the file
RichTextbox1.Text &= d.readLine()
'displaying text from doc file in the RichTextBox
End while
d.close()
End Sub

The image below displays output of the above code.