What are Web Services?
Web Services are a means by which logic can be executed over standard web protocols. Remote servers orthe machine your code lives on could process this logic, and the web service can be writter in a different language on a different platform from the consuming program. For example , a Windows desktop alllication written in Visual Basic .NET running on a PC in London could consume a web service written in Java, hosted on a Linux server in Washington.
As well as XML and HTTP, the other important standards that web services rely on are SOAP, WSDL, and UDDI:
- SOAP stands for Simple Object Access Protocol, and it defines the format of the XML used in the procedure call and the web service’s response.
- Web Services’ implementations are described by WSDL, the Web Services Description Language. Using the WSDL information of a web service, developers and tools can determine what methods are exposed by the service and how to use them.
- UDDI, Universal Description, Discovery, and Integration, is the standard upon which directories of web services can be built, allowing developers to ‘shop’ for web services like you would find a web site with Yahoo.
The EaseSoft Barcode Web Serice
We will develop a barcode web service with EaseSoft Barcode .NET Control with Visual Studio .NET how we can consume the web services we create in our windows application and web application.
We will start by creating the web service, so create a new ASP.NET Web Service and call it ImageService. Add a reference to EaseWinControl.dll in the project. To download the EaseWinControl.dll from here, Add a reference to System.Drawing.dll and System.Windows.Forms.dll in the project. In the code for ImageService.asmx, we will also need to add using statements at top of the files: System.Drawing , System.Drawing.Imaging and EaseSoftBarcode:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using EaseSoftBarcode;
using System.Drawing;
using System.Drawing.Imaging;
We will modify the WebMethod that Visual Studio .NET sets up for us. Change the method so it appears as below:
[WebMethod]
public Byte[] BarcodeImage(Symbology symbology, string TextToEncode,int resolution)
{
EaseWinControl barcode= new EaseWinControl();
barcode.SymbologyID= symbology;
barcode.TextToEncode= TextToEncode;
barcode.ResolutionDPI=resolution;
Bitmap bmp = barcode.Picture;
System.IO.MemoryStream outStream = new System.IO.MemoryStream();
bmp.Save(outStream,ImageFormat.Jpeg);return outStream.ToArray();
}
If you want to know more about EaseSoft .NET Barcode control, please go to the website.
Now we will create the client web application, so add an ASP.NET Web Application to the solution call it TestWebService. In the HTML view , delete all but the very top line – as we will be returning an image, we do not want any HTML at all to be output:
<%@ Page language="c#"
Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false"
Inherits="TestWebServiceClient.WebForm1"
%>
Now go back to the Design view and double-click the form, which will take us to the page’s code-behind file. Modify the Page_Load clode to reflect the code shown below:
private void Page_Load(object sender, System.EventArgs e)
{
localhost.ImageService wService;
wService = new localhost.ImageService();
System.Drawing.Bitmap bmp;
Byte[] bytes;
System.IO.MemoryStream inStream;
bytes= wService.BarcodeImage(localhost.Symbology.Code39ASCII,"test123456",96);// if you want to print the barcode picture,better to set the resolution as 300 DPI
inStream = new System.IO.MemoryStream(bytes);
bmp= new System.Drawing.Bitmap(inStream);
Response.ContentType ="image/jpeg";
bmp.Save(Response.OutputStream,bmp.RawFormat);
bmp.Dispose();}
This code creates a new bitmap based on data pulled from the web service using a proxy object (wService) and a stream( inStream ). The final thing we need to do is add a web reference to the project so this application can see the web service. To do this, right -click the test project in the Solution Explorer and choose Add Web Reference…we create the sample web service in our website : http://www.easebarcode/ImageService.asmx .
Create Windows Form Client Application is similar, Add web reference : http://www.easebarcode/ImageService.asmx, get the bitmap from the webservice as following:
com.easebarcode.www.ImageService barcodeservice =new BarCodeClient.com.easebarcode.www.ImageService() ;
System.Byte[] imgBarcode= barcodeservice.BarcodeImage(symbology,TextToEncode.Text.Trim(),Convert.ToInt32(ResolutionDPI.Text));
//Set resolution as 96 DPI for displaying, set as 300 for printing
MemoryStream memStream = new MemoryStream(imgBarcode);
Bitmap bmp =new Bitmap(memStream);
The client site screenshot as following: