Using IE Web services to build asp.net applications

Source: Internet
Author: User
Tags soap web services

One of the limitations that developers encounter when building a business site is that they can only use the browser as a user interface. For example, in many cases, users want to retrieve information from the server after performing certain actions, such as entering an employee number. To do this, they will send the page back to the server, retrieve the employee information, and refresh the page with the information retrieved from the server. Although the current approach to refreshing the entire page is common, it is inefficient because the Web page refreshes and renders the entire page content, even if a small portion of the page actually changes. You can notice this inefficiency when searching for a category or search engine. Its delay and waste of resources are obvious. However, if the same functionality can be done without refreshing the browser page, the user experience will be greatly improved. For this to happen, we need to execute a section of the server code without leaving the current page, which is what web service behavior does. In this case, the code snippet executed on the server is the code for the Web service method, and the role of the browser is to invoke the server code without leaving or refreshing the current page.

When using Web service behavior, you only need to send a request to execute a specific Web service method from a Web page of a client browser. On the server side, ASP. NET runtime receives the request and invokes the Web service method with the associated parameters. After the Web service executes, it communicates the results to the caller, and the results are displayed or processed by the browser. As a result, you can establish a typical client/server communication without having to ignore the stateless (stateless) features of the underlying HTTP protocol. Another advantage of Web service behavior is that it requires only one file (WEBSERVICE.HTC) to exist on the client to implement the functionality. When using Web service methods, you can also invoke Web service methods asynchronously. This capability is powerful and can be used to build a rich user experience on the client side. For example, when a user continues to work on a transaction on the same page, you can use the Web service behavior to let the server validate some data. Once the function call returns, you can get the execution results and communicate the results to the user.

Web Service Behavior

Web service behavior is the ability to implement HTML component (HTC) files as collateral behavior, which can be used for Internet Explorer 5 and later versions. As mentioned earlier, Web service behavior provides a way to invoke remote Web methods across platforms by leveraging industry-standard protocols such as HTTP, soap, and XML. One of the important features of Web service behavior is that it allows you to use these features without deep soap knowledge. Web Services basically simplify the remote invocation of a Web service by handling SOAP packet traffic between the browser and the Web service. You don't have to worry about the aggregation (assembling) and decomposition (disassembling) of SOAP messages. All code that handles SOAP details is encapsulated in behavior, simplifying client script in the main web page.

Web service behavior is a JavaScript file embedded in a Web page using a specific IE behavior syntax. By exposing properties and methods to client script, Web service behavior aggregates the message and decomposes the response information sent back by the Web service. The objects exposed by the behavior can not only initiate a clear error-handling method, but also provide a simple access to the returned data. Web service behavior receives method calls from client script and uses SOAP messages to send requests to the Web service. The result returns the client script and the process continues. The Web page can then use the information in any desired situation, such as updating parts of the page, sending error messages, and so on.

A key feature of Web service behavior is that it allows client script to access the Web service without navigating to another URL. The following list details important ways to support Web service behavior:

Createuseoptions (set up the option to use)--allows us to save user authentication information across remote method calls. It can be useful when we use SSL to communicate with a remote Web service.

Callservice (Invoke Service)--allows us to invoke the remote Web service asynchronously.

Useservice (using services)-allows us to create a "friendly" name for the service when invoking the Web service.

To use behavior in IE 5.0 and above Web pages, you must download the WEBSERVICE.HTC behavior file and save it in the same folder as your Web page. This file can be downloaded from the link below: HTTP://MSDN.MICROSOFT.COM/DOWNLOADS/SAMPLES/INTERNET/BEHAVIORS/LIBRARY/WEBSERVICE/WEBSERVICE.HTC.

Implementation process

You've learned some basics of Web service behavior, and you can now look at a sample application that demonstrates how to use Web service behavior in an asp.net application. In this example, you will build a simple application that allows you to retrieve employee information from the Northwind database. The sample application also allows you to search employee information based on an employee ID.

The establishment process of Employee Web service

In this section, you need to first create a new Visual C # Web service project called Employeewebservice. After the project is established, you need to change the default Web service class name Service1 to EmployeeService. Then you need to import the following namespaces to perform data access and process XML data.

Using System.Data.SqlClient;
Using System.Xml;
[WebMethod]
Public XmlDocument getempdetailsbyempid (int EmployeeID)
{
String connstring =
system.configuration.configurationsettings.appsettings["ConnectionString"];
SqlConnection SqlConnection = new SqlConnection (connstring);
Try
{
DataSet Employeedataset = new DataSet ("Employeesroot");
Pass in the name of the stored procedure you want to execute and the SqlConnection object as a parameter
SqlDataAdapter adapter = new SqlDataAdapter ();
SqlCommand command = new SqlCommand ("SELECT * from Employees Where EmployeeID =" + employeeid.tostring (), SqlConnection);
Set the properties of a SqlCommand object
Command.commandtype = CommandType.Text;
Adapter. SelectCommand = command;
Populating a DataSet with values returned by stored procedures
Adapter. Fill (Employeedataset, "Employees");
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.loadxml (Employeedataset.getxml ());
return xmldoc;
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
if (sqlconnection.state = = ConnectionState.Open)
{
Sqlconnection.close ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.