Home > Backend Development > XML/RSS Tutorial > Detailed introduction to XML Web Service graphic code examples

Detailed introduction to XML Web Service graphic code examples

黄舟
Release: 2017-03-09 17:03:05
Original
1664 people have browsed it

##For the first time I met XML Web Service and thought People who want to get started quickly may want to quickly understand how to create and call it. This article will use a small example to describe how to use Visual Studio 2008 to create Web Service and how to call it. The Web Service in the example will return an image according to the request of the client program.

1. CreateWeb ServiceProject

Open VS2008, select the File/New/Project menu item, in the opened New Project dialog box , select Visual C# -> Web -> ASP.NET Web Service Application, then enter the project name (Name), and the storage location (Position) and solution name (Solution Name), click "OK" to generate the project. In this example we use AnnotationWebService as the name of the project and solution (see Figure 1).

XML Web Service示例(图1)

#Picture 1New ProjectDialog box

2.       Add aWeb Service

Click in Solution Explorer of VS2008 AnnotationWebService item, select the Project/Add new item menu item, in the opened Add New Item dialog box, select Web/Web Service, Then enter the name (Name) of Web Service and click "Add" to add a Web Service. In this example, we use ImageService as the name of Web Service (see Figure 2).

XML Web Service示例(图2)

figure 2:Add New ItemDialog

#After that, we will see such a project directory in Solution Explorer (see Figure 3). (Note: When creating a project, the system will add a Web Service by default with the name Service1. You can click the Delete item in its right-click menu to delete it. delete.)

XML Web Service示例(图3)

Picture 3Solution Explorer

#3.

Encoding for Web Service

Right-click ImageService.asmx and select View Markup to open this file. We can see the following line:

<%@ WebService Language="C#" CodeBehind="ImageService.asmx.cs" Class="AnnotationWebService.ImageService" %>
Copy after login

It indicates that the code of ImageService is in ImageService. asmx.cs file. We right-click ImageService.asmx, select View Code, open the ImageService.asmx.cs file, and add our service code. In this example, we write a Method GetImage that reads the image based on the given file name and returns it to the client (see code below).


##

using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
 
namespace AnnotationWebService
{
    /// <summary>
    /// Summary description for ImageService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 ImageService : System.Web.Services.WebService
    {
 
        [WebMethod(Description="Request an image by name")]
        public byte[] GetImage(string imageFileName)
        {
            byte[] imageArray = GetBinaryFile(imageFileName);
            if (imageArray.Length < 2)
            {
                throw new SoapException("Could not open image on server.", SoapException.ServerFaultCode);
            }
            else
            {
                return imageArray;
            }
        }
 
        private byte[] GetBinaryFile(string fileName)
        {
            string fullPathFileName = HttpContext.Current.Request.PhysicalApplicationPath + fileName;
            if (File.Exists(fullPathFileName))
            {
                try
                {
                    FileStream fileStream = File.OpenRead(fullPathFileName);
                    return ConvertStreamToByteBuffer(fileStream);
                }
                catch
                {
                    return new byte[0];
                }
            }
            else
            {
                return new byte[0];
            }
        }
 
        public byte[] ConvertStreamToByteBuffer(Stream imageStream)
        {
            int imageByte;
            MemoryStream tempStream = new MemoryStream();
            while ((imageByte = imageStream.ReadByte()) != -1)
            {
                tempStream.WriteByte((byte)imageByte);
            }
            return tempStream.ToArray();
        }
    }
}
Copy after login

##4. InIISAdd virtual directory(Virtual Directory)

Open the IIS console program, right-click Default Web Site, and select Add New/Virtual Directory menu item , enter the virtual directory alias (Alias) in the opened Virtual Directory Caption Wizard dialog box, in this example we enter AnnotationWebService, click "Next ", then select the directory where ImageService.asmx is located, and then click "Next" until "Finish". (Note: The above description is based on the XP SP3 environment.)

##5.      Create a proxy for Web Service(Proxy)

In VS2008, open a Windows application solution (.sln), in this example we Open a solution called AnnotationApp.Right-click on the project to call Web Service (for example, in this example we choose to use DataLib) and select the Add Web Reference menu item (if it has never been added After Web Reference, you may not see the Add Web Reference menu item. We can first select the Add Service Reference menu item, and then in the pop-up Add Click "Advanced" in the Service Reference dialog box, and then click "Add Web Reference" in the pop-up Service Reference Settings dialog box). In the Add Web Reference dialog box that pops up, enter the URL of the Web Service we want to call. In this example we enter:

##http://localhost/AnnotationWebService/ImageService.asmx

Then click "Go", ImageService will be displayed in the Web Page below, in Web reference nameEnter the name referenced by Web in the edit box. In order to avoid using the name ImageService again, here we enter ImageWebService (see Figure 4), and then click "Add Reference" to add a Web reference.

XML Web Service示例(图4)

#Picture 4Add Web ReferenceDialog box

This will add a Web Reference in Solution Explorer (see Figure 5).

XML Web Service示例(图5)

#Picture 5: Web Reference was added

##The reference added is Image The proxy code of Service, which includes a class with the same name as ImageService, is derived from System.Web.Services.Protocols.SoapHttpClientProtocol. In this way, the GetImage method of ImageService can be called in the client code just like calling the method in its own Assembly.

6. 客户程序调用Web Service

在客户程序中需要调取图像的地方增加如下代码(注:代码中的Image类不是.Net Framework类库中的Image类,是客户程序中的一个类):

    ImageService imageService = new ImageService();
            Bitmap bitmap;
            try
            {
                byte[] image = imageService.GetImage("half-bred panthers.jpg");
                MemoryStream memoryStream = new MemoryStream(image);
                bitmap = new Bitmap(memoryStream);
                _image = new Image(_viewportTransformer, bitmap);
            }
            catch (WebException e)
            {
                // Exception handling
            }
Copy after login

 

然后,可以将图像显示出来。

 

7.运行客户程序来测试Web Service调用

 

编译运行客户程序,Web Service被成功调用并返回所调用的图像(见图6)。

 

XML Web Service示例(图6)

Figure 6: Running results


#

The above is the detailed content of Detailed introduction to XML Web Service graphic code examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template