ServiceReference Clase
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Registra un servicio web para su uso en una página web.
public ref class ServiceReference
public class ServiceReference
type ServiceReference = class
Public Class ServiceReference
- Herencia
-
ServiceReference
Ejemplos
En el ejemplo siguiente se muestra cómo agregar una referencia a un servicio web en el marcado de página para llamar a los métodos de servicio web desde el 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>
Comentarios
Para llamar a métodos de servicio web desde ECMAScript (JavaScript), debe incluir una referencia de servicio en la página ASP.NET y aplicar el ScriptServiceAttribute atributo a la definición de clase de servicio web. Si incluye una referencia de servicio a un servicio web en el ScriptManager control o ScriptManagerProxy dentro de la página ASP.NET, se crearán instancias de objetos JavaScript en el explorador.
Los objetos proxy se usarán para hacer lo siguiente:
Realización de solicitudes asincrónicas en JavaScript a métodos de servicio web,
Inicialice instancias de servidores proxy de tipos de datos de servidor, en particular para su uso como parámetros de entrada para invocar métodos web.
Nota:
El ServiceReference control solo se puede usar para los servicios del mismo dominio.
Puede definir la ubicación del servicio web mediante declaración agregando un <asp:ServiceReference> elemento al elemento dentro del <Services> elemento de la <asp:ScriptManager> página y, a continuación, estableciendo su Path atributo, como se muestra en el ejemplo siguiente.
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="~/WebServices/SimpleWebService.asmx" />
</Services>
</asp:ScriptManager>
Puede usar la InlineScript propiedad para indicar si el script de generación de proxy se incluye en la página como un bloque de script insertado o se obtiene mediante una solicitud independiente.
También puede agregar mediante programación un ServiceReference objeto a través de la ScriptManager.Services colección o ScriptManagerProxy.Services mediante el Add método de la ServiceReferenceCollection clase .
Constructores
| Nombre | Description |
|---|---|
| ServiceReference() |
Inicializa una nueva instancia de la clase ServiceReference. |
| ServiceReference(String) |
Inicializa una nueva instancia de la ServiceReference clase con una ruta de acceso especificada. |
Propiedades
| Nombre | Description |
|---|---|
| InlineScript |
Obtiene o establece un valor que indica si el script de generación de proxy se incluye en la página como un bloque de script insertado o se obtiene mediante una solicitud independiente. |
| Path |
Obtiene o establece la ruta de acceso del servicio web al que se hace referencia. |
Métodos
| Nombre | Description |
|---|---|
| Equals(Object) |
Determina si el objeto especificado es igual al objeto actual. (Heredado de Object) |
| GetHashCode() |
Actúa como función hash predeterminada. (Heredado de Object) |
| GetProxyScript(ScriptManager, Control) |
Proporciona un script de proxy de un objeto derivado ServiceReference que se puede invalidar como una personalización. |
| GetProxyUrl(ScriptManager, Control) |
Proporciona una dirección URL de proxy de un objeto derivado ServiceReference que se puede invalidar como una personalización. |
| GetType() |
Obtiene el Type de la instancia actual. (Heredado de Object) |
| MemberwiseClone() |
Crea una copia superficial del Objectactual. (Heredado de Object) |
| ToString() |
Devuelve una cadena que representa el valor de la Path propiedad o el nombre de tipo. |