1. Trang chủ
  2. » Công Nghệ Thông Tin

Creating a Web Service

5 363 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Creating a web service
Thể loại Manual
Định dạng
Số trang 5
Dung lượng 37,49 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Creating a Web Service In this section, you'll create a Web service that contains a method that returns a DataSet containing rows from the Customers table.. Figure 17.2: Adding a new Web

Trang 1

Creating a Web Service

In this section, you'll create a Web service that contains a method that returns a DataSet containing rows from the Customers table

Start VS NET and select File ➣ New➣ Project In the New Project dialog box, select Visual C# Projects in the Project Types pane on the left, and select ASP.NET Web

Service in the Templates pane on the right Enter

http://localhost/NorthwindWebService in the Location field (see Figure 17.1) Click

OK to continue

Figure 17.1: Creating a Web service in VS NET

Note If you have installed IIS on a computer other than your local machine, then replace

localhost with the name of your remote computer in the Location field

After VS NET has created the new project, open Solution Explorer and delete the

Service1.asmx file from your project; you'll be adding your own asmx file next, and it's easier to simply delete the initial Service1.asmx file

Select Project ➣ Add Web Service, and enter Customers.asmx in the Name field of the Add New Item dialog box (see Figure 17.2) Click Open to continue VS NET adds a file named Customers.asmx to your project

Trang 2

Figure 17.2: Adding a new Web service

Select View ➣ Code to view the C# code in the Customers.asmx.cs file Listing 17.1 shows my example Customers.asmx.cs file

Listing 17.1: CUSTOMERS.ASMX.CS

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Web;

using System.Web.Services;

namespace NorthwindWebService

{

/// <summary>

/// Summary description for Customers

/// </summary>

///

[WebService(Namespace="http://DbProgramming/NorthwindWebService")]

public class Customers : System.Web.Services.WebService

{

public Customers()

{

//CODEGEN: This call is required by the ASP.NET Web Services Designer InitializeComponent();

}

#region Component Designer generated code

//Required by the Web Services Designer

private IContainer components = null;

Trang 3

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor

/// </summary>

private void InitializeComponent()

{

}

/// <summary>

/// Clean up any resources being used

/// </summary>

protected override void Dispose(bool disposing)

{

if(disposing && components != null)

{

components.Dispose();

}

base.Dispose(disposing);

}

#endregion

// WEB SERVICE EXAMPLE

// The HelloWorld() example service returns the string Hello World

// To build, uncomment the following lines then save and build the project

// To test this web service, press F5

// [WebMethod]

// public string HelloWorld()

// {

// return "Hello World";

// }

}

Notice that the Customers class is derived from the System.Web.Services.WebService class, which indicates that the Customers class forms part of a Web service

Near the end of Listing 1.1, you'll notice a method named HelloWorld() that is

commented out This commented code shows you how to write a method that is exposed

by your Web service You'll notice that a line containing [WebMethod] is placed before

Trang 4

the method, which indicates that the method would be exposed by the Web service Of course, because the HelloWorld() method is commented out, the method isn't compiled and therefore isn't actually exposed by the Web service

Replace the example HelloWorld() method in your code with the RetrieveCustomers() method shown in Listing 17.2 RetrieveCustomers() connects to the Northwind database and returns a DataSet containing rows from the Customers table You pass a WHERE clause to the RetrieveCustomers() method in the whereClause parameter; this WHERE clause is then used in the SELECT statement to limit the rows retrieved from the

Customers table

Listing 17.2: CUSTOMERSWEBSERVICE.CS

[WebMethod]

public DataSet RetrieveCustomers(string whereClause)

{

SqlConnection mySqlConnection =

new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");

string selectString =

"SELECT CustomerID, CompanyName, Country "+

"FROM Customers "+

"WHERE "+ whereClause;

SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

mySqlCommand.CommandText = selectString;

SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

mySqlDataAdapter.SelectCommand = mySqlCommand;

DataSet myDataSet = new DataSet();

mySqlConnection.Open();

mySqlDataAdapter.Fill(myDataSet, "Customers");

mySqlConnection.Close();

return myDataSet;

}

Note You'll need to change the string used to create the mySqlConnection object in your

code to connect to your Northwind database

Because the code uses classes in the System.Data.SqlClient namespace, you'll also need

to add the following line near the top of your Customers.asmx.cs file:

using System.Data.SqlClient;

Trang 5

By default, a Web service uses a namespace of http://tempuri.org, and you should change that to the URL used by your organization The following example sets the namespace for the Web service to http://DbProgramming/NorthwindWebService:

[WebService(Namespace="http://DbProgramming/NorthwindWebService")]

public class Customers : System.Web.Services.WebService

Notice that you set the Namespace in a line placed before the Customers class Go ahead and add a line similar to the previous one to your own code

Build your Web service by selecting Build ➣ Build Solution

That's it! You've built your Web service

Ngày đăng: 28/10/2013, 19:15

TỪ KHÓA LIÊN QUAN