asp.net Web Service

Source: Internet
Author: User
Tags define array soap return web services client
Asp.net|web

Now the Internet is constantly developing, in the beginning of the Internet application, we browse the Web page is static, not interactive. And now with the development of technology, will be provided to the Web browser a programmable Web site. These sites will be more tightly integrated in organizations, applications, services, and drivers that will connect directly to another Web site via some applications, and these programmable Web sites will become more reusable and more intelligent than traditional Web sites!

The. NET platform provides us with a running environment, the common Language runtime environment (Clr,common Language Runtime). For the CLR, it provides a built-in mechanism to create a programmable site, which is consistent and familiar to Web application developers and VB programmers. This model can be reused or extended. It includes open Internet standards (HTTP, XML, SOAP, SDL). So that it can be accessed by Web surfers.
ASP.net uses the. asmx file to support Web services. The. asmx file belongs to a text file as well as. aspx files. It is included in the. aspx file and becomes part of the ASP.net application.

Here we will give a simple example to introduce the. asmx file, and we'll start with the classic example of "Hello,world", which is the following code:
!--FileName: Webservice\sisam.asmx-->
<%@ 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

Description

1. The encoding must begin with a WebService declaration to define the file as a Web Service. Also, set the type of programming language in the same line.

2. Then, the name space System.Web.Services is introduced. Note that the namespace belongs to the most basic element and must be included.

3. Then, declare the function module in the service, which is the class module, where the class is called HelloWorld. This class is derived from the base class webservice and should be of public type.

4. Finally, define the accessible method for the service. In front of the symbol that represents the method, set the custom attribute. corresponding to the C # language, the attribute value is [WebMethod]; corresponding to VB, that is. If this property is not set, then this method cannot be accessed from the service. A local application can use any class of public type, but only classes with [WebMethod] can be accessed remotely through SOAP.

When a request for a service occurs, the. asmx file is automatically compiled by the ASP.net running environment. Subsequent requests can be executed by the buffered precompiled type object.

To test the written code, we need a Web server that supports asp.net. Suppose the name of this Web server is called Server1, which has a virtual directory test. Follow the steps below to start the test:


1. Save Code as HelloWorld.asmx

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

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

At this point, we'll see a common method for this Web service--those marked as WebMethod attributes, and know the protocols that can be used to invoke these methods, such as SOAP or HTTP GET.

Enter Http://Foo/Bar/HelloWorld.asmx in the Address bar of Internet Explorer? SDL will produce an XML file with the same information based on the service Description language (Description language:sdl) syntax. This SDL file is very important and is used by the client to access the service.

To access from the client:

In addition to the technology that allows developers to create Web services, the Microsoft. NET Framework provides clients with a set of sophisticated and sophisticated tools and code to access and use Web services. Since Web services is based on open protocol standards such as simple Object Access Protocol soap (easy Access Protocol) and HTTP, we can use this client-side technology to use a asp.net web Services. Of course, this also requires a high level of synthesis of asp.net Web services and this client technology.

There is a tool in the SDK called WebServiceUtil.exe, which we can use to download a Web services SDL Description Language and create a proxy class that expresses the service. For example, when we enter the following command, we can create a proxy class called HelloWorld.cs:

webserviceutil/c:proxy/pa:http://somedomain.com/somefolder/helloworld.asmx?sdl

This class looks very similar to the class you created earlier. It contains a method SayHelloWorld, and the method returns a string. Compiling this proxy class into an application and then invoking the method of the proxy class, the result is that the proxy class wraps the SOAP request through HTTP, then receives the SOAP encoding response, and finally assembles into a string.

See a client routine about the Web service:


File accessed by client: Clint.aspx, as follows:


<%@ Import namespace= "Datatypesservice"%>

<style>
Div
{
font:8pt Verdana;
BACKGROUND-COLOR:CCCCCC;
Border-color:black;
Border-width:1;
Border-style:solid;
padding:10,10,10,10;
}

</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
message5.innerhtml = message5.innerhtml & "<BR> Price:" & Myorders (0). Price

End Sub

</script>

<body style= "font:10pt Verdana"

<div id= "Message1" runat= "Server"/>

<div id= "Message2" runat= "Server"/>

Method This <div id= "Message3" runat= "Server"/>

Method <div id= "Message4" runat= "Server"/>

Method This <div id= "Message5" runat= "Server"/>

</body>


In the client program, we use the <%@ Import namespace= "Datatypesservice"%> to introduce Datatypesservice, our custom namespace. Then only the method in the Datatypesservice is invoked 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.