ServiceReference Klasa

Definicja

Rejestruje usługę sieci Web do użycia na stronie sieci Web.

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

Przykłady

W poniższym przykładzie pokazano, jak dodać odwołanie do usługi sieci Web w znacznikach strony w celu wywołania metod usługi sieci Web ze skryptu.

<%@ 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>

Uwagi

Aby wywołać metody usługi sieci Web z poziomu kodu ECMAScript (JavaScript), należy dołączyć odwołanie do usługi na stronie ASP.NET i zastosować ScriptServiceAttribute atrybut do definicji klasy usługi sieci Web. Jeśli dołączysz odwołanie do usługi sieci Web w ScriptManager kontrolce lub ScriptManagerProxy na stronie ASP.NET, obiekty JavaScript zostaną utworzone w przeglądarce.

Obiekty serwera proxy będą używane do wykonywania następujących czynności:

  • Tworzenie asynchronicznych żądań w języku JavaScript do metod usługi internetowej

  • Inicjuj wystąpienia serwerów proxy typów danych serwera, w szczególności do użycia jako parametry wejściowe do wywoływania metod internetowych.

Uwaga

Kontrolka ServiceReference może być używana tylko w przypadku usług w tej samej domenie.

Lokalizację usługi sieci Web można zdefiniować deklaratywnie, dodając <asp:ServiceReference> element do <Services> elementu wewnątrz <asp:ScriptManager> elementu na stronie, a następnie ustawiając jego Path atrybut, jak pokazano w poniższym przykładzie.

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

Możesz użyć InlineScript właściwości , aby wskazać, czy skrypt generowania serwera proxy znajduje się na stronie jako blok skryptu wbudowanego, czy jest uzyskiwany przez oddzielne żądanie.

Można również programowo dodać ServiceReference obiekt za pośrednictwem ScriptManager.Services kolekcji lub ScriptManagerProxy.Services przy użyciu Add metody ServiceReferenceCollection klasy .

Konstruktory

ServiceReference()

Inicjuje nowe wystąpienie klasy ServiceReference.

ServiceReference(String)

Inicjuje ServiceReference nowe wystąpienie klasy z określoną ścieżką.

Właściwości

InlineScript

Pobiera lub ustawia wartość wskazującą, czy skrypt generowania serwera proxy znajduje się na stronie jako blok skryptu wbudowanego, czy jest uzyskiwany przez oddzielne żądanie.

Path

Pobiera lub ustawia ścieżkę przywołynej usługi sieci Web.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetProxyScript(ScriptManager, Control)

Udostępnia skrypt serwera proxy z obiektu pochodnego ServiceReference , który można zastąpić jako dostosowanie.

GetProxyUrl(ScriptManager, Control)

Udostępnia adres URL serwera proxy z obiektu pochodnego ServiceReference można zastąpić jako dostosowanie.

GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący wartość Path właściwości lub nazwę typu.

Dotyczy

Zobacz też