Asp. NET Create Web Service declaration

Source: Internet
Author: User
Tags define implement integer soap tostring web services client
Asp.net|web|web Services | Create a declaration an XML Web service

When you use ASP.net to create an XML Web service, you put the required @_webservice instructions at the top of the. asmx file, and the presence of the @_ WebService directive is related to the URL address of the implemented XML Web service. Next, you implement the XML Web service class to define the methods and data types that are visible to the XML Web service client. Finally, you add your XML Web service logic to those methods to handle the XML Web service request and return response. The XML Web service class that you define can be included directly in the. asmx file or in a separate file. If you use a standalone file, it must be compiled into binary code. You can choose to apply a WebService attribute to this class to implement the XML Web service. Classes that implement XML Web services can inherit from the WebService class.

Declares an XML Web service where the implementation of this service exists in the same file.

Add a @_webservice directive to the top of an. asmx file that provides the class and program language for implementing an XML Web service in this implementation.

The class attribute can be set to the same assembly file as the @_webservice instruction, or a class in a separate assembly file. If this class exists in a standalone assembly file, it must be placed in the \ Bin directory under the Web application directory where the XML Web service resides. The Language property can be set to C #, VB, and JS, representing C #, Visual Basic.NET, and JScript.NET, respectively.

The following code example sets the Language property of the @_ WebService directive to MyMath for the C#,class property and 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;
}
}
Declares an XML Web service in which the implementation of the service exists in an assembly file.

Add a @_webservice directive to the top of an. asmx file that provides classes for implementing an XML Web service, the implementation contained in the assembly file, and the programming language used in the implementation.

The following @_webservice directive is the only line of code in an. asmx file that specifies that the Myname.mywebservice class exists in the MyAssembly assembly file in the bin directory under 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"%>
Declares an XML Web service in which the implementation of the service exists in an assembly file.

Add a @_webservice directive to the top of an. asmx file that provides classes for implementing an XML Web service, the implementation contained in the assembly file, and the programming language used in the implementation.

The following @_webservice directive is the only line of code in an. asmx file that specifies that the Myname.mywebservice class exists in the MyAssembly assembly file in the bin directory under the Web application of the XML Web service.
Note: If you do not specify a compilation in the @_ WebService directive, then asp.net searches the list of assembly files in the \ Bin directory of the Web application hosting the XML Web service the first time the XML Web service is accessed. So, if you provide the assembly file name, you will improve the system performance for the first time access.

Apply WebService Property

By applying the optional WebService attribute to a class that implements an XML Web service, you can set the default XML domain namespace for this XML Web service by using a string that describes the XML Web service.

It is strongly recommended that this default domain namespace (here http://tempuri.org) be modified before the XML Web service is publicly used. This is important because your XML Web service must be differentiated from other XML Web services that inadvertently use default values as domain namespace.

To set the XML domain namespace of the member XML Web service

Apply a WebService property to the class that implements the XML Web service, and set the Namespace property.

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
Derived from the WebService class

Classes that implement an XML Web service created by using ASP.net can optionally derive from the WebService class to gain access to public asp.net objects such as application, session, user, and context permissions. The application and session properties provide the right to save and receive the life cycle of a Web application or the state of a particular conversation. For more information about status, see the section on managing states in an XML Web service created with ASP.net. The user property contains the identity of the XML Web service caller. An XML Web service can use the caller identity to determine whether a request is authorized. For more information on validation, see the section Strengthening XML Web Services security. The context property provides permission to obtain all the specific HTTP information requested by an XML Web service client.

The following code example uses the context property to obtain the requested time on the server.

[C #]
<%@ WebService language= "C #" class= "Util"%>
Using System;
Using System.Web.Services;

public class Util:webservice {
[WebMethod (description= "Returns the time as stored on the Server", Enablesession=false)]
public string Time ()
{
return Context.Timestamp.TimeOfDay.ToString ();
}
}
[Visual Basic]
<%@ WebService language= "VB" class= "Util"%>
Imports System
Imports System.Web.Services

Public Class Util
Inherits WebService

<webmethod (Description: = "Returns" as stored on the Server, _
EnableSession: = False) > _
Public Function time () as String
Return Context.Timestamp.TimeOfDay.ToString ()
End Function
End Class
Defining XML Web Service methods

The methods used to implement the XML Web service cannot be automatically communicated via the Web, but with XML Web services created with ASP.net, it is easy to be able to do this. To add this functionality, you need to apply a WebMethod attribute to the public method. The method of XML Web services that can communicate with the web is called an XML Web service method.
XML Web service methods are a key component of the messaging infrastructure used by XML Web services. More precisely, a client and an XML Web service use messages, especially SOAP messages, to communicate. The client sends a SOAP request to the XML Web service, and an XML Web service method returns a SOAP response. The XML Web service defines the type of message that it accepts using operations, as defined in the Web Service Description language. These operations are associated with each XML Web service method in an XML Web service. Even though each of these XML Web service methods is defined in ASP.net using a method of a class, the data must be serialized into XML to implement data transmitted over the network. Similarly, it is important to remember that XML Web services are not a substitute for DCOM, and we should say that XML Web services are a messaging infrastructure that spans the use of industry-standard platform communications.

Declaring an XML Web service method

Declares an XML Web service, adding @_webservice directives. For more information, see the section declaring an XML Web service.

Add public methods to the class that implements the XML Web service.

Apply the WebMethod attribute to the public method you want to map to the operation.

The following code example has two public methods, one of which is an XML Web service method. The Multiply method is an XML Web service method because it has a WebMethod attribute applied to it.

[C #]
<%@ WebService language= "C #" class= "Util"%>
Using System;
Using System.Web.Services;
public class Util:webservice
{
public int Add (int a, int b)
{
return a + B;
}

[WebMethod]
public long Multiply (int a, int b)
{
return a * b;
}
}
[Visual Basic]
<%@ WebService language= "VB" class= "Util"%>
Imports System
Imports System.Web.Services
Public Class Util
Inherits WebService

Public Function Add (A As Integer, B as Integer) As Integer
Return a + b
End Function

WebMethod () _
Public Function Multiply (A As Integer, B as Integer) as Long
return a * b
End Function
End Class

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.