Declare XML Web Service (1)
Declare an XML Web Service
When you use ASP. NET to create an XML Web Service, you need to put the @ _ WebService command at the top of the. asmx file .. The existence of the asmx file and the @ _ WebService command are related to the URL of the implemented XML Web Service. Next, you implement the XML Web Service class to define the methods and data types visible to the XML Web Service client. Finally, you add your XML Web Service logic to those methods to process XML Web Service requests and returned responses. The XML Web Service class you define can be directly included in the. asmx file or in a separate file. If you use an independent file, it must be compiled as a binary code. You can apply a WebService attribute to this class to implement the XML Web Service. Classes that implement XML Web Services can inherit from WebService classes.
Declare an XML Web Service. The implementation of this service exists in the same file.
Add a @ _ WebService command to the top of a. asmx file, specifying the class and programming language for implementing the XML Web Service in this implementation.
The Class attribute can be set to the same Assembly file as the @ _ WebService command, or a Class in a separate Assembly file. If this class exists in an independent Assembly file, it must be placed in the Bin directory of the Web application directory where the XML Web Service is located. The Language attribute can be set to C #, VB, and JS, indicating C #, Visual Basic. NET, and JScript. NET respectively.
In the following code example, set the Language attribute of the @ _ WebService command to C #, and the Class attribute to MyMath, Which is saved in the same file.
<% @ WebService Language = "C #" Class = "MyMath" %> Using System. Web. Services; Public class MyMath { [WebMethod] Public int Add (int num1, int num2 ){ Return num1 + num2; } } |
Declare an XML Web Service. Its implementation exists in an assembly file.
Add a @ _ WebService command to the top of a. asmx file, which specifies the class for implementing the XML Web Service, the implementation contained in the Assembly file, and the program language used in this implementation.
The following @ _ WebService command is. the unique line of code in the asmx file, specifying MyName. the MyWebService class exists in the MyAssembly Assembly file in the Bin directory of the Web application of the XML Web Service.
[C #] <% @ WebService Language = "C #" Class = "MyName. MyWebService, MyAssembly" %> [Visual Basic] <% @ WebService Language = "VB" Class = "MyName. MyWebService, MyAssembly" %> |
Declare an XML Web Service. Its implementation exists in an assembly file.
Add a @ _ WebService command to the top of a. asmx file, which specifies the class for implementing the XML Web Service, the implementation contained in the Assembly file, and the program language used in this implementation.
The following @ _ WebService command is. the unique line of code in the asmx file, specifying MyName. the MyWebService class exists in the MyAssembly Assembly file in the Bin directory of the Web application of the XML Web Service.
Note: If you do not specify an assembly in the @ _ WebService command, ASP. NET searches for the list of assembly files in the Bin directory of the Web application that stores the XML Web Service when the XML Web Service is accessed for the first time. Therefore, if you provide the Assembly file name, you will improve the system performance during the first access.
Application WebService attributes
You can use an optional WebService attribute to implement an XML Web Service class. You can use a string describing the XML Web Service to set the default XML Domain Name Space for the XML Web Service.
It is strongly recommended that this default domain name space (http://tempuri.org here) be modified before the XML Web Service is publicly available. This is very important because your XML Web Service must be separated from other XML Web Services that inadvertently use the default value as the domain name space.
Set the XML Domain Name Space for the XML Web Service member
Apply a WebService attribute to the class that implements the XML Web Service and set the Namespace attribute.
The following code example sets the XML Domain Name Space to http://www.contoso.com /.
[C #] <% @ WebService Language = "C #" Class = "Math" Debug = true %> Using System. Web. Services; Using System;
[WebService (Namespace = "http://www.contoso.com/")] Public class Math { [WebMethod] Public int Add (int num1, int num2 ){ Return num1 + num2; } } [Visual Basic] <% @ WebService Language = "VB" Class = "Math" %> Imports System. Web. Services Imports System
<WebService (Namespace: = "http://www.contoso.com/")> _ Public Class Math <WebMethod ()> Public Function Add (num1 As Integer, num2 As Integer) As Integer Return num1 + num2 End Function End Class |