ServiceReference Class

Definition

Registers a Web service for use in a Web page.

public ref class ServiceReference
public class ServiceReference
type ServiceReference = class
Public Class ServiceReference
Inheritance
ServiceReference

Examples

The following example demonstrates how to add a reference to a Web service in the page markup to call the Web service methods from script.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  
    <head id="Head1" runat="server"> 
        <style type="text/css">
            body {  font: 11pt Trebuchet MS;
                    font-color: #000000;
                    padding-top: 72px;
                    text-align: center }
  
            .text { font: 8pt Trebuchet MS }
        </style>  
        <title>Calling Web Methods</title>    
    </head>
    
    <body>
        <form id="Form1" runat="server">
        
            <asp:ScriptManager runat="server" ID="scriptManagerId">
                <Scripts>
                    <asp:ScriptReference Path="Scripts.js" />
                </Scripts>
                <Services>
                    <asp:ServiceReference  Path="WebService.asmx" />
                </Services>                
            </asp:ScriptManager>
            
            <div>
                <h2>Calling Web Methods</h2>
                 
               <table>
                    <tr align="left">
                        <td>Method that does not return a value:</td>
                        <td>
                            <!-- Getting no retun value from 
                            the Web service. --> 
                            <button id="Button1"  
                                onclick="GetNoReturn()">No Return</button>
                        </td>
                    </tr>
                    
                    <tr align="left">
                        <td>Method that returns a value:</td>
                        <td>
                            <!-- Getting a retun value from 
                            the Web service. --> 
                            <button id="Button2" 
                                onclick="GetTime(); return false;">Server Time</button>
                        </td>
                   </tr>
                   
                   <tr align="left">
                        <td>Method that takes parameters:</td>
                        <td>
                            <!-- Passing simple parameter types to 
                            the Web service. --> 
                            <button id="Button3" 
                                onclick="Add(20, 30); return false;">Add</button>
                        </td>
                       
                    </tr>
                   
                    <tr align="left">
                        <td>Method that returns XML data:</td>
                        <td>   
                             <!-- Get Xml. --> 
                            <button id="Button4" 
                                onclick="GetXmlDocument(); return false;">Get Xml</button>
                        </td>
                    </tr>
                    <tr align="left">
                        <td>Method that uses GET:</td>
                        <td>   
                             <!-- Making a GET Web request. --> 
                            <button id="Button5" 
                                onclick="MakeGetRequest(); return false;">Make GET Request</button>
                        </td>
                    </tr>
                    
                </table>
         
            </div>
        </form>
        
        <hr/>
        
        <div>
            <span id="ResultId"></span>
        </div>   
        
    </body>
    
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  
    <head id="Head1" runat="server"> 
        <style type="text/css">
            body {  font: 11pt Trebuchet MS;
                    font-color: #000000;
                    padding-top: 72px;
                    text-align: center }
  
            .text { font: 8pt Trebuchet MS }
        </style>  
        <title>Calling Web Methods</title>    
    </head>
    
    <body>
        <form id="Form1" runat="server">
        
            <asp:ScriptManager runat="server" ID="scriptManagerId">
                <Scripts>
                    <asp:ScriptReference Path="Scripts.js" />
                </Scripts>
                <Services>
                    <asp:ServiceReference  Path="WebService.asmx" />
                </Services>                
            </asp:ScriptManager>
            
            <div>
                <h2>Calling Web Methods</h2>
                 
               <table>
                    <tr align="left">
                        <td>Method that does not return a value:</td>
                        <td>
                            <!-- Getting no retun value from 
                            the Web service. --> 
                            <button id="Button1"  
                                onclick="GetNoReturn()">No Return</button>
                        </td>
                    </tr>
                    
                    <tr align="left">
                        <td>Method that returns a value:</td>
                        <td>
                            <!-- Getting a retun value from 
                            the Web service. --> 
                            <button id="Button2" 
                                onclick="GetTime(); return false;">Server Time</button>
                        </td>
                   </tr>
                   
                   <tr align="left">
                        <td>Method that takes parameters:</td>
                        <td>
                            <!-- Passing simple parameter types to 
                            the Web service. --> 
                            <button id="Button3" 
                                onclick="Add(20, 30); return false;">Add</button>
                        </td>
                       
                    </tr>
                   
                    <tr align="left">
                        <td>Method that returns XML data:</td>
                        <td>   
                             <!-- Get Xml. --> 
                            <button id="Button4" 
                                onclick="GetXmlDocument(); return false;">Get Xml</button>
                        </td>
                    </tr>
                    <tr align="left">
                        <td>Method that uses GET:</td>
                        <td>   
                             <!-- Making a GET Web request. --> 
                            <button id="Button5" 
                                onclick="MakeGetRequest(); return false;">Make GET Request</button>
                        </td>
                    </tr>
                    
                </table>
         
            </div>
        </form>
        
        <hr/>
        
        <div>
            <span id="ResultId"></span>
        </div>   
        
    </body>
    
</html>

Remarks

To call Web service methods from ECMAScript (JavaScript), you must include a service reference in the ASP.NET page and apply the ScriptServiceAttribute attribute to the Web service class definition. If you include a service reference to a Web service in the ScriptManager or ScriptManagerProxy control inside the ASP.NET page, JavaScript objects will be instantiated in the browser.

The proxy objects will be used to do the following:

  • Make asynchronous requests in JavaScript to Web service methods,

  • Initialize instances of proxies of server data types, in particular for use as input parameters for invoking Web methods.

Note

The ServiceReference control can only be used for services in the same domain.

You can define the Web service location declaratively by adding an <asp:ServiceReference> element to the <Services> element inside the <asp:ScriptManager> element on the page, and then setting its Path attribute, as shown in the following example.

<asp:ScriptManager runat="server" ID="scriptManager">  
  <Services>  
    <asp:ServiceReference Path="~/WebServices/SimpleWebService.asmx" />  
  </Services>  
</asp:ScriptManager>  

You might use the InlineScript property to indicate whether the proxy generation script is included in the page as an inline script block or is obtained by a separate request.

You can also programmatically add a ServiceReference object through the ScriptManager.Services or ScriptManagerProxy.Services collection by using the Add method of the ServiceReferenceCollection class.

Constructors

ServiceReference()

Initializes a new instance of the ServiceReference class.

ServiceReference(String)

Initializes a new instance of the ServiceReference class with a specified path.

Properties

InlineScript

Gets or sets a value that indicates whether the proxy generation script is included in the page as an inline script block or is obtained by a separate request.

Path

Gets or sets the path of the referenced Web service.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetProxyScript(ScriptManager, Control)

Provides a proxy script from a derived ServiceReference object that can be overridden as a customization.

GetProxyUrl(ScriptManager, Control)

Provides a proxy URL from a derived ServiceReference object can be overridden as a customization.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the value of the Path property or the type name.

Applies to

See also