ServiceReference Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Registra un servizio Web da usare in una pagina Web.
public ref class ServiceReference
public class ServiceReference
type ServiceReference = class
Public Class ServiceReference
- Ereditarietà
-
ServiceReference
Esempio
Nell'esempio seguente viene illustrato come aggiungere un riferimento a un servizio Web nel markup di pagina per chiamare i metodi del servizio Web dallo 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>
Commenti
Per chiamare i metodi del servizio Web da ECMAScript (JavaScript), è necessario includere un riferimento al servizio nella pagina ASP.NET e applicare l'attributo ScriptServiceAttribute alla definizione della classe del servizio Web. Se si include un riferimento al servizio a un servizio Web nel controllo o ScriptManagerProxy all'interno ScriptManager della pagina ASP.NET, verrà creata un'istanza degli oggetti JavaScript nel browser.
Gli oggetti proxy verranno usati per eseguire le operazioni seguenti:
Effettuare richieste asincrone in JavaScript ai metodi del servizio Web,
Inizializzare istanze di proxy di tipi di dati del server, in particolare per l'uso come parametri di input per richiamare metodi Web.
Annotazioni
Il ServiceReference controllo può essere usato solo per i servizi nello stesso dominio.
È possibile definire la posizione del servizio Web in modo dichiarativo aggiungendo un <asp:ServiceReference> elemento all'elemento all'interno dell'elemento <Services><asp:ScriptManager> nella pagina e quindi impostandone Path l'attributo, come illustrato nell'esempio seguente.
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="~/WebServices/SimpleWebService.asmx" />
</Services>
</asp:ScriptManager>
È possibile usare la InlineScript proprietà per indicare se lo script di generazione proxy è incluso nella pagina come blocco di script inline o viene ottenuto da una richiesta separata.
È anche possibile aggiungere a livello di codice un ServiceReference oggetto tramite l'insieme ScriptManager.Services o ScriptManagerProxy.Services usando il Add metodo della ServiceReferenceCollection classe .
Costruttori
| Nome | Descrizione |
|---|---|
| ServiceReference() |
Inizializza una nuova istanza della classe ServiceReference. |
| ServiceReference(String) |
Inizializza una nuova istanza della ServiceReference classe con un percorso specificato. |
Proprietà
| Nome | Descrizione |
|---|---|
| InlineScript |
Ottiene o imposta un valore che indica se lo script di generazione proxy è incluso nella pagina come blocco di script inline o viene ottenuto da una richiesta separata. |
| Path |
Ottiene o imposta il percorso del servizio Web a cui si fa riferimento. |
Metodi
| Nome | Descrizione |
|---|---|
| Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente. (Ereditato da Object) |
| GetHashCode() |
Funge da funzione hash predefinita. (Ereditato da Object) |
| GetProxyScript(ScriptManager, Control) |
Fornisce uno script proxy da un oggetto derivato ServiceReference che può essere sottoposto a override come personalizzazione. |
| GetProxyUrl(ScriptManager, Control) |
Fornisce un URL proxy da un oggetto derivato ServiceReference può essere sottoposto a override come personalizzazione. |
| GetType() |
Ottiene il Type dell'istanza corrente. (Ereditato da Object) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| ToString() |
Restituisce una stringa che rappresenta il valore della Path proprietà o il nome del tipo. |