Asp.net Web Services

Source: Internet
Author: User
Nowadays, the Internet is constantly evolving. At the beginning of Internet applications, the web pages we browse are static and cannot interact with each other. With the increasing development of technology, it will provide a programmable web site to web page viewers. These sites will be more closely integrated in the organization, application, service, and driver. These sites will be directly connected to another web site through some application software, compared with traditional web sites, these programmable web sites will become more reusable and intelligent!

The. NET platform provides a runtime environment, namely, the Common Language Runtime (CLR ). For CLR, it provides a built-in mechanism to create a programmable site. For Web application developers and VB programmers, this will be consistent and familiar. This model can be reused or expanded. It contains open Internet standards (HTTP, XML, SOAP, SDL ). So that it can be accessed by web page viewers.
ASP. NET uses the. asmx file to Support Web Services .. The asmx file and the. aspx file both belong to text files. It is included in the. aspx file and is part of the ASP. NET application.

The following is a simple example to introduce the. asmx file. The Code is as follows:

<! -- File name: WebService \ sisam. asmx -->

Web tutorial Network

<% @ WebService Language = "VB" class = "helloworld" %>
Imports system. Web. Services
Public class helloworld: inherits WebService
Public Function <webmethod ()> sayhelloworld () as string
Return ("Hello World ")
End Function
End Class

Note:

1. The encoding must first be declared by WebService to define this file as a web service. In addition, set the programming language type in the same line.

2. Then, introduce the namespace system. Web. Services. Note that this namespace is the most basic element and must contain it.

3. Then, declare the functional module in the service, that is, the class module. The class name here is helloworld. This class comes from the base class WebService and should be of the Public type.

4. Define the Service's accessible methods. Set the custom attribute before the symbol of the expression method. Corresponding to C #, the attribute value is [webmethod]; corresponding to VB, that is. If this attribute is not set, this method cannot be accessed from the service. A local application can use any public class, but only a class with [webmethod] can be remotely accessed through soap.

When a service request occurs, the. asmx file is automatically compiled by the ASP. NET runtime environment. Subsequent requests can be executed by buffered pre-compiled objects.

Web tutorial Network

To test the compiled code, we need a web server that supports ASP. NET. Assume that the Web server is named server1 and there is a virtual directory named test. Follow these steps to start the test:

1. Save the code as helloworld. asmx

2. Put it under the virtual directory bar of the Web server foo

3. Open Internet assumer5 and enter http: // server1/test/helloworld. asmx in the address bar.

At this time, we will see the common methods for this Web Service-that is, the characters marked as webmethod attributes, and the Protocols that can be used to call these methods, such as soap or http get.

In the address bar of Internet Explorer, enter http: // Foo/BAR/helloworld. asmx? After SDL, an XML file with the same information will be generated based on the service description language (SDL) syntax. This SDL file is very important. The client uses it to access the service.

  Access from the client:

In addition to the technology that allows developers to use to create Web Services, Microsoft's. NET Framework provides clients with a set of sophisticated and advanced tools and code to access and use web services. Because Web services are based on open protocol standards such as Simple Object Access Protocol soap (Simple Object Access Protocol) and HTTP, we can use this client technology to use non-Asp.. NET web services. Of course, this also requires high-level synthesis of ASP. NET web services and such client technology.

Network applications

Sdkhas a tool called webserviceutil.exe. We can use it to download a web services SDL Description Language and create a proxy class to express this service. For example, if you enter the following command, you can create a proxy class named helloworld. CS:

Webserviceutil/C: proxy/PA: http://someDomain.com/someFolder/HelloWorld.asmx? SDL

This class looks very similar to the previously created class. It contains a method sayhelloworld, which returns a string. Compile the proxy class into an application, and then call the method of the proxy class. The result is: through HTTP, the proxy class wraps the SOAP request and then receives the soap encoding response, finally, it is collected into a string.

See a client routine for Web Service:

The file accessed by the Client: Clint. aspx. The content is as follows:

<% @ Import namespace = "datatypesservice" %>

<HTML>
<Style>
Div
{
Font: 8pt verdana;
Background-color: cccccc;
Border-color: black;
Border-width: 1;
Border-style: solid;
Padding: 10, 10, 10; web tutorial Network
}

</Style>

<Script language = "VB" runat = "server">

Public sub page_load (sender as object, e as eventargs)

Dim D as datatypes = new datatypes ()
Message1.innerhtml = D. sayhello ()
Message1.innerhtml = message1.innerhtml & D. sayhelloname ("Bob ")
Message3.innerhtml = message3.innerhtml & D. getmode ()

Dim myintarray as INTEGER () = D. getintarray ()
Dim mystring as string = "Contents of the array: <br>"

For I = 0 to myintarray. Length-1
Mystring = mystring & myintarray (I) & "<br>"
Next

Message2.innerhtml = message2.innerhtml & mystring

Dim myorder as order = D. getorder ()
Message4.innerhtml = message4.innerhtml & "<br> orderid:" & myorder. orderid
Message4.innerhtml = message4.innerhtml & "<br> price:" & myorder. Price

Dim myorders as order () = D. getorders ()
Message5.innerhtml = message5.innerhtml & "<br> orderid:" & myorders (0). orderid web tutorial Network
Message5.innerhtml = message5.innerhtml & "<br> price:" & myorders (0). Price

End sub

</SCRIPT>

<Body style = "Font: 10pt verdana">
<H4> using datatypes with Web Services </H4>

<H5> methods that return a primitive (string): </H5>
<Div id = "message1" runat = "server"/>

<H5> methods that return an array of primitives (integers): </H5>
<Div id = "message2" runat = "server"/>

<H5> method that returns an Enum: </H5>
<Div id = "message3" runat = "server"/>

<H5> method that returns a class/struct: </H5>
<Div id = "message4" runat = "server"/>

<H5> method that returns an array of classes/structs: </H5>
<Div id = "message5" runat = "server"/>

</Body>
</Html>

In the client program, we use <% @ import namespace = "datatypesservice" %> to introduce the custom namespace datatypesservice. Then, the method in datatypesservice is called in the program.

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.