DataServiceContext.Execute Método

Definición

Envía una solicitud al servicio de datos para ejecutar un URI concreto.

Sobrecargas

Execute<T>(DataServiceQueryContinuation<T>)

Envía una solicitud al servicio de datos para recuperar la página siguiente de datos en un resultado de consulta paginado.

Execute<TElement>(Uri)

Envía una solicitud al servicio de datos para ejecutar un URI concreto.

Execute<T>(DataServiceQueryContinuation<T>)

Envía una solicitud al servicio de datos para recuperar la página siguiente de datos en un resultado de consulta paginado.

public:
generic <typename T>
 System::Data::Services::Client::QueryOperationResponse<T> ^ Execute(System::Data::Services::Client::DataServiceQueryContinuation<T> ^ continuation);
public System.Data.Services.Client.QueryOperationResponse<T> Execute<T> (System.Data.Services.Client.DataServiceQueryContinuation<T> continuation);
member this.Execute : System.Data.Services.Client.DataServiceQueryContinuation<'T> -> System.Data.Services.Client.QueryOperationResponse<'T>
Public Function Execute(Of T) (continuation As DataServiceQueryContinuation(Of T)) As QueryOperationResponse(Of T)

Parámetros de tipo

T

Tipo devuelto por la consulta.

Parámetros

continuation
DataServiceQueryContinuation<T>

Objeto DataServiceQueryContinuation<T> que representa la siguiente página de datos para devolver del servicio de datos.

Devoluciones

Respuesta que contiene la página siguiente de datos del resultado de la consulta.

Excepciones

Cuando se produce un error durante la ejecución de la solicitud o cuando convierte el contenido del mensaje de respuesta en objetos.

Comentarios

El objeto DataServiceQueryContinuation<T> proporcionado contiene el URI que, cuando se ejecuta, devuelve la página siguiente de datos del resultado de la consulta.

Se aplica a

Execute<TElement>(Uri)

Envía una solicitud al servicio de datos para ejecutar un URI concreto.

public:
generic <typename TElement>
 System::Collections::Generic::IEnumerable<TElement> ^ Execute(Uri ^ requestUri);
public System.Collections.Generic.IEnumerable<TElement> Execute<TElement> (Uri requestUri);
member this.Execute : Uri -> seq<'Element>
Public Function Execute(Of TElement) (requestUri As Uri) As IEnumerable(Of TElement)

Parámetros de tipo

TElement

Tipo devuelto por la consulta.

Parámetros

requestUri
Uri

URI al que se enviará la solicitud de consulta. El URI puede ser cualquier URI de servicio de datos válido. Puede contener parámetros de consulta $.

Devoluciones

IEnumerable<TElement>

Resultados de la operación de consulta.

Excepciones

Cuando no se recibe ninguna respuesta de una solicitud a requestUri.

Cuando requestUri es null.

Cuando requestUri no es un URI válido para el servicio de datos.

Cuando se produce un error durante la ejecución de la solicitud o cuando convierte el contenido del mensaje de respuesta en objetos.

El servicio de datos devuelve un error HTTP 404: Recurso no encontrado.

Ejemplos

En este ejemplo se usa un bucle do…while para cargar las entidades Customers desde los resultados paginados del servicio de datos. Se llama al método Execute usando el URI de vínculo siguiente para recibir la siguiente página de datos.

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
DataServiceQueryContinuation<Customer> token = null;
int pageCount = 0;

try
{
    // Execute the query for all customers and get the response object.
    QueryOperationResponse<Customer> response =
        context.Customers.Execute() as QueryOperationResponse<Customer>;

    // With a paged response from the service, use a do...while loop
    // to enumerate the results before getting the next link.
    do
    {
        // Write the page number.
        Console.WriteLine("Page {0}:", pageCount++);

        // If nextLink is not null, then there is a new page to load.
        if (token != null)
        {
            // Load the new page from the next link URI.
            response = context.Execute<Customer>(token)
                as QueryOperationResponse<Customer>;
        }

        // Enumerate the customers in the response.
        foreach (Customer customer in response)
        {
            Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName);
        }
    }

    // Get the next link, and continue while there is a next link.
    while ((token = response.GetContinuation()) != null);
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Dim token As DataServiceQueryContinuation(Of Customer) = Nothing
Dim pageCount = 0

Try
    ' Execute the query for all customers and get the response object.
    Dim response As QueryOperationResponse(Of Customer) = _
        CType(context.Customers.Execute(), QueryOperationResponse(Of Customer))

    ' With a paged response from the service, use a do...while loop 
    ' to enumerate the results before getting the next link.
    Do
        ' Write the page number.
        Console.WriteLine("Page {0}:", pageCount + 1)

        ' If nextLink is not null, then there is a new page to load.
        If token IsNot Nothing Then
            ' Load the new page from the next link URI.
            response = CType(context.Execute(Of Customer)(token),  _
            QueryOperationResponse(Of Customer))
        End If

        ' Enumerate the customers in the response.
        For Each customer As Customer In response
            Console.WriteLine(vbTab & "Customer Name: {0}", customer.CompanyName)
        Next

        ' Get the next link, and continue while there is a next link.
        token = response.GetContinuation()
    Loop While token IsNot Nothing
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try

Comentarios

El método Execute se emplea para consultar un servicio de datos por URI; el método hace que se emita una solicitud HTTP GET al servicio de datos. El URI de solicitud especificado puede ser absoluto o relativo.

Si el requestUri es absoluto, este método valida si el URI apunta al mismo servicio de datos que se especificó al construir DataServiceContext. Si el requestUri es relativo, este método quita cualquier barra diagonal inicial y anexa requestUri a lo que se proporcionó al construir DataServiceContext. Se anexa una barra diagonal después del URI pasado al constructor DataServiceContext, si aún no hay una.

Cuando este método vuelve, toda la respuesta HTTP para la solicitud se ha leído del flujo de red, pero no se habrá procesado la respuesta; no se ha realizado ninguna resolución de identidades ni materialización de objetos. La resolución de identidades y la materialización de todos los objetos no se producen para una entidad especificada en la respuesta hasta que se enumere.

Consulte también

Se aplica a