ServiceReference 类

定义

注册要在网页中使用的 Web 服务。Registers a Web service for use in a Web page.

public ref class ServiceReference
public class ServiceReference
type ServiceReference = class
Public Class ServiceReference
继承
ServiceReference

示例

下面的示例演示如何在页标记中添加对 Web 服务的引用以从脚本中调用 Web 服务方法。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>

注解

若要从 ECMAScript (JavaScript)中调用 Web 服务方法,必须在 ASP.NET 页中包含服务引用,并将ScriptServiceAttribute特性应用于 Web 服务类定义。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. 如果在 ASP.NET 页内的ScriptManagerScriptManagerProxy控件中包含对 Web 服务的服务引用,JavaScript 对象将在浏览器中实例化。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:

  • 将 JavaScript 中的异步请求用于 Web 服务方法,Make asynchronous requests in JavaScript to Web service methods,

  • 初始化服务器数据类型的代理的实例,该实例特定于用作调用 Web 方法的输入参数。Initialize instances of proxies of server data types, in particular for use as input parameters for invoking Web methods.

备注

ServiceReference控件只能用于同一个域中的服务。The ServiceReference control can only be used for services in the same domain.

可以通过将<asp:ServiceReference>元素添加<Services>到页面中<asp:ScriptManager>元素内的元素,然后设置其Path属性(如下面的示例中所示),以声明方式定义 Web 服务位置。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>  

您可以使用InlineScript属性来指示代理生成脚本是否作为内联脚本块包含在页中,或由单独的请求获取。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.

还可以通过使用ServiceReference ScriptManagerProxy.Services Add ScriptManager.ServicesServiceReferenceCollection的方法,通过或集合以编程方式添加对象。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.

构造函数

ServiceReference()

初始化 ServiceReference 类的新实例。Initializes a new instance of the ServiceReference class.

ServiceReference(String)

使用指定的路径初始化 ServiceReference 类的新实例。Initializes a new instance of the ServiceReference class with a specified path.

属性

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

获取或设置引用的 Web 服务的路径。Gets or sets the path of the referenced Web service.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetProxyScript(ScriptManager, Control)

提供可作为自定义项被重写的派生 ServiceReference 对象中的代理脚本。Provides a proxy script from a derived ServiceReference object that can be overridden as a customization.

GetProxyUrl(ScriptManager, Control)

提供可作为自定义项被重写的派生 ServiceReference 对象中的代理 URL。Provides a proxy URL from a derived ServiceReference object can be overridden as a customization.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个字符串,该字符串表示 Path 属性或类型名称的值。Returns a string that represents the value of the Path property or the type name.

适用于

另请参阅