Analysis of WebService client application method

Source: Internet
Author: User
Tags reference tostring
web| client. NET platform to support Web service, including the construction and use of Web service. Unlike other development platforms, use. NET platform, you do not need other tools or SDK to complete the development of Web service. The. NET framework itself fully supports Web service, including server-side request processors and support for sending and receiving SOAP messages to clients. This article will take you with. NET to create and use Web service.

Create a Web service in. Net

To be in. NET, you simply select "File/Add New Item" in your solution and pop up the dialog box shown in Figure 1 below:



Figure 1

In this box, select Web Service and specify a name. Vs.net will create a default WebService framework for you. Depending on your needs, you can build the WebService method you need.

The following code is the WebService method that returns all employee information from the Northwind database employees in SQL Server 2000.

[WebMethod]
public string GetEmployees ()
{
String cnstr= "server=njim01;database=northwind;uid=sa;pwd=64084888;";
String rsstring;
SqlConnection cn=new SqlConnection (Cnstr);
SqlDataAdapter Cmd=new SqlDataAdapter ("SELECT * FROM Employees", CN);
DataSet ds=new DataSet ();
DataTable tbl;
Cmd. Fill (ds, "Employees");
Tbl=ds. tables["Employees"];
rsstring= "<table border=\" 0\ "bgcolor=\" blue\ "cellpadding=\"
Cellspacing=\ "1\" ><tr bgcolor=\ "white\" >;
for (int i=0;i<=ds. tables["Employees"]. columns.count-1;i++)
{
rsstring+= "<td>" +ds. tables["Employees"]. Columns[i]. Columnname+ "</td>";
}
rsstring+= "</tr>";
for (int i=0;i<tbl. rows.count;i++)
{
rsstring+= "<tr bgcolor=\" white\ ">";
for (int j=0;j<=ds. tables["Employees"]. columns.count-1;j++)
{
rsstring+= "<td>" +tbl. rows[i][j]+ "</td>";
}
rsstring+= "</tr>";
}
rsstring+= "</table>";
return rsstring;
}



Thus, it is convenient to establish a webservice in the vs.net.

Application of two WebService in client

Through the long-term application and research of vs.net, we summarize the following four possible methods for WebService application in client:

1. Call the WebService method in the same solution;

2. Call the WebService method in different solutions;

3, on the Internet to increase the use of WebService method;

4, use WEBSERVICE.HTC to invoke WebService method.

The above four methods are described below.

1. Call the WebService method in the same solution

First, we need to build a solution called MyTest.sln. The scheme consists of a Web Form named Testform.aspx and a testservice.asmx webservice. The code for Testform.aspx is as follows:

SqlDataAdapter Cmd=new SqlDataAdapter ("SELECT * FROM Employees", CN);
DataSet ds=new DataSet ();
DataTable tbl;
Cmd. Fill (ds, "Employees");
Tbl=ds. tables["Employees"];
rsstring= "<table border=\" 0\ "bgcolor=\" blue\ "cellpadding=\"
Cellspacing=\ "1\" ><tr bgcolor=\ "white\" >;
for (int i=0;i<=ds. tables["Employees"]. columns.count-1;i++)
{
rsstring+= "<td>" +ds. tables["Employees"]. Columns[i]. Columnname+ "</td>";
}
rsstring+= "</tr>";
for (int i=0;i<tbl. rows.count;i++)
{
rsstring+= "<tr bgcolor=\" white\ ">";
for (int j=0;j<=ds. tables["Employees"]. columns.count-1;j++)
{
rsstring+= "<td>" +tbl. rows[i][j]+ "</td>";
}
rsstring+= "</tr>";
}
rsstring+= "</table>";
return rsstring;
}



As you can see from the above code, we only provide a TestWebService button in the Testform form, and when we click TestWebService, we use a <span> named Msg. To display the return value of the method in TestWebService.

In Testwebservice.asmx, we only set up a method with the following code:

[WebMethod]
public string GetEmployees ()
{
String cnstr= "server=njim01;database=northwind;uid=sa;pwd=6408;";
String rsstring;
SqlConnection cn=new SqlConnection (Cnstr);
SqlDataAdapter Cmd=new SqlDataAdapter ("SELECT * FROM Employees", CN);
DataSet ds=new DataSet ();
DataTable tbl;
Cmd. Fill (ds, "Employees");
Tbl=ds. tables["Employees"];
rsstring= "<table border=\" 0\ "bgcolor=\" blue\ "cellpadding=\"
Cellspacing=\ "1\" ><tr bgcolor=\ "white\" >;
for (int i=0;i<=ds. tables["Employees"]. columns.count-1;i++)
{
rsstring+= "<td>" +ds. tables["Employees"]. Columns[i]. Columnname+ "</td>";
}
rsstring+= "</tr>";
for (int i=0;i<tbl. rows.count;i++)
{
rsstring+= "<tr bgcolor=\" white\ ">";
for (int j=0;j<=ds. tables["Employees"]. columns.count-1;j++)
{
rsstring+= "<td>" +tbl. rows[i][j]+ "</td>";
}
rsstring+= "</tr>";
}
rsstring+= "</table>";
return rsstring;
}



private void Button4_Click (object sender, System.EventArgs e)
{
TestWebService webservice=new TestWebService ();
Msg. Innerhtml=webservice.getcustomers ();
}



In fact, the invocation of the WebService method in this scenario in the same solution is defined and invoked in a class manner.

2. Invoking the WebService method in different solutions it is possible that our webservice is not built in the same solution, as we have established in previous scenarios. Now we're going to call its methods in the new solution.

This can be divided into two categories:

1, although WebService is not in the solution to call it, but on the same physical host;

2. WebService and solutions are not on the same physical host, as we are going to invoke the Microsoft WebService sample, which is:

http://chs.gotdotnet.com/quickstart/aspplus/samples/

Services/dataservice/vb/dataservice.asmx

Position. How do I call it?

1 There are also two methods of calling on the same physical host:

① directly references a DLL that contains a webservice solution. For example we are in another name

A webservice called Math.asmx was established for the Webservice.sln solution, in which we wrote a method:

[WebMethod]
Public float Add (float x,float y)
{
return x+y;
}



Here we'll call this method in the Testform.aspx form:

First, we add a new button to the Testform.aspx form, named Reference.

Second, we add Webservice.dll to the MyTest.sln solution reference.

Third, the Reference_click () code for the reference button is as follows:

private void Reference_click (object sender, System.EventArgs e)
{
float X,y;
Webservice.math math1=new Webservice.math ();
/* This defines the WebService object math1*/
x=11.88f;
y=23.19f;
Msg. Innerhtml=math1.add (x,y). ToString ();
/* This calls the Add () method of the Math1 object and displays the result in <span> on the name MSG.
}



When we click the Reference button, the result of the Add () method call is displayed in the <span> tag named Msg.

②web references in fact, Vs.net provides an incredibly powerful feature that makes it unnecessary to refer directly to a WebService solution DLL. All we need to do is right-click on reference and select Add Web Reference ... to pop up the window shown in Figure 2 below:



Figure 2

In the Address bar, enter the location where you want to invoke WebService, as

Http://localhost:8088/webservice/math.asmx

At this point, a "Web References" folder is added under MyTest.sln, and there is a "localhost1" item under this folder, as shown in Figure 3:





Figure 3

The localhost1 under the Web References folder in the figure is a webservice reference to another solution. Let's look at how to invoke the WebService method in the Testform.aspx form in this way:

First, we add a new button to the Testform.aspx form, named Localhost1.

The encoding for the Localhost1_click () event of the Localhost1 button is as follows:

private void Localhost1_click (object sender, System.EventArgs e)
{
float X,y;
Localhost1.math math1=new Localhost1.math ();
/* This defines the WebService object math1*/
x=16.1f;
y=17.89f;
Msg. Innerhtml=math1.add (x,y). ToString ();
/* This calls the Add () method of the Math1 object and displays the result in <span> on the name MSG.
}



When we click the Localhost1 button, the code also completes the corresponding method call.

3, on the Internet to increase the use of WebService method in fact, the "Web reference" approach in the 2nd scenario is a special case of the current situation. Because, when we change the address of the Web reference address bar to point to a webservice on a host on the Internet, the WebService method is raised on the Internet.

Let's call the WebService sample in the Microsoft ASP.net QuickStart, when we add a Web reference, enter the following WebService address in the Address bar:

http://chs.gotdotnet.com/quickstart/aspplus/samples/

Services/dataservice/vb/dataservice.asmx

As shown in Figure 3, you can see in the Web References folder that you have added a new item, "Com.gotdotnet.chs," and add a button to the Testform.aspx form Gowebservice, at Gowebservice Click () The code for the event is as follows:

private void Gowebservice_click (object sender, System.EventArgs e)
{
Com.gotdotnet.chs.DataService gows=new Com.gotdotnet.chs.DataService ();
/* This defines the WebService object gows*/

DataSet ds=new DataSet ();
Ds=gows.gettitleauthors ();
/* This calls the Gows object's GetTitleAuthors () method, which returns a DataSet object.
Msg. Innerhtml=ds. tables["Authors"]. Rows.Count.ToString ();
* * Here will be the number of Authors table records on the <span> on the name of MSG * *
}



When we click the Gowebservice button, the call to the WebService sample from the Microsoft ASP.net QuickStart on the internet will also appear on MSG.

4. Call the WebService method with Webservice.htc This method is not in the details, please refer to the Microsoft site.

<b> three closing remarks </b>

In short, WebService is very powerful, but it is difficult to find a description of how the WebService method is invoked on the client, which causes most developers to use the 4th method, I have summed up the above methods according to my application experience of vs.net, which is only for vs.net developers ' Reference!


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.