Design policy (3)
Use the output buffer to improve the performance of your XML Web Service. When the output buffer is enabled, the service request results are saved in the output buffer for a specified period of time. If a similar XML Web Service request is generated, the result can be obtained from the buffer without re-calculation. In this way, the XML Web Service feedback time is improved by reducing the processing required by the XML Web Service server. The cache can be executed on both the client and server. The Duration Attribute allows you to specify the time for high-speed buffering to save XML Web Service output.
The command for outputting high-speed Buffering on the client is:
<% @ OutputCache Duration = "60" %> |
The following code example shows how to use the Duration Attribute on the client application to specify the output high-speed buffer to 60 seconds.
[C #] <% @ Page Language = "C #" %> <% @ Import Namespace = "System. Net" %> <% @ OutputCache Duration = "60" VaryByParam = "none" %> <Html> <Script language = "C #" runat = "server"> Void EnterBtn_Click (Object Src, EventArgs e) { MyMath. Math math = new MyMath. Math (); // Call the XML Web service. Float total = math. Add (Convert. ToInt32 (Num1.Text ), Convert. ToInt32 (Num2.Text )); // Display the results in a Label control. Total. Text = "Total:" + total. ToString (); } </Script> <Body> <Form action = "MathClient. aspx" runat = server> <Font face = "Verdana"> Enter the two numbers you want to add and press The Total button. <P> Number 1: <Asp: textbox id = "Num1" runat = server/> + Number 2: <Asp: textbox id = "Num2" runat = server/> = <Asp: button id = "Total_Button" text = "Total" OnClick = "EnterBtn_Click" runat = server/> <P> <Asp: label id = "Total" runat = server/> </Font> </Form> </Body> </Html> [Visual Basic] <% @ Page Language = "VB" %> <% @ Import Namespace = "System. Net" %> <% @ OutputCache Duration = "60" VaryByParam = "none" %> <Html> <Script language = "VB" runat = "server"> Sub EnterBtn_Click (Src As Object, e As EventArgs) Dim math As New MyMath. Math () Call the XML Web service. Dim addtotal As Single = math. Add (Convert. ToInt32 (Num1.Text), Convert. ToInt32 (Num2.Text )) Display the results in a Label control. Total. Text = "Total:" & addtotal. ToString () End Sub </Script> <Body> <Form action = "MathClient. aspx" runat = server> <Font face = "Verdana"> Enter the two numbers you want to add and press The Total button. <P> Number 1: <Asp: textbox id = "Num1" runat = server/> + Number 2: <Asp: textbox id = "Num2" runat = server/> = <Asp: button id = "Total_Button" text = "Total" OnClick = "EnterBtn_Click" runat = server/> <P> <Asp: label id = "Total" runat = server/> </Font> </Form> </Body> </Html> |
You can also use the CacheDuration attribute of the WebMethod attribute class to allow high-speed Buffering on the server. The following code example shows how to use the CacheDuration attribute on the XML Web Service method to specify that the output high-speed buffer is 60 seconds.
[C #] <% @ WebService Language = "C #" Class = "MathService" %> Using System; Using System. Web. Services; Public class MathService: WebService { [WebMethod (CacheDuration = 60)] Public float Add (float a, float B) { Return a + B; } [WebMethod (CacheDuration = 60)] Public float Subtract (float a, float B) { Return a-B; } [WebMethod (CacheDuration = 60)] Public float Multiply (float a, float B) { Return a * B; } [WebMethod (CacheDuration = 60)] Public float Divide (float a, float B) { If (B = 0) return-1; Return a/B; } } [Visual Basic] <% @ WebService Language = "VB" Class = "MathService" %> Imports System Imports System. Web. Services Public Class MathService Inherits WebService <WebMethod (CacheDuration: = 60)> _ Public Function Add (a As Single, B As Single) As Single Return a + B End Function
<WebMethod (CacheDuration: = 60)> _ Public Function Subtract (a As Single, B As Single) As Single Return a-B End Function
<WebMethod (CacheDuration: = 60)> _ Public Function Multiply (a As Single, B As Single) As Single Return a * B End Function
<WebMethod (CacheDuration: = 60)> _ Public Function Divide (a As Single, B As Single) As Single If B = 0 Then Return-1 End If Return a/B End Function End Class |
When designing your XML Web Service, try to follow the structure of the formatting mode.
XML Web Services use SOAP as the main transmission and serialization protocol. A soap message consists of an optional Header and message body. The header contains information that can be processed by the Web server architecture. SOAP does not define any header. The message body contains information processed by the application, such as parameters or return values for XML Web Services.
Provides documents for your XML Web Service, such as a static HTML file, to describe the operations and data structures of your service. This section also describes how to use the XML Web Service. Do not rely on the service description or service help page as your only document.