Metodo DataServiceContext.Execute<TElement> (Uri)
Invia una richiesta al servizio dati per eseguire un URI specifico.
Non supportato dal client di WCF Data Services 5.0 per Silverlight.
Spazio dei nomi System.Data.Services.Client
Assembly: Microsoft.Data.Services.Client (in Microsoft.Data.Services.Client.dll)
Sintassi
'Dichiarazione
Public Function Execute(Of TElement) ( _
requestUri As Uri _
) As IEnumerable(Of TElement)
'Utilizzo
Dim instance As DataServiceContext
Dim requestUri As Uri
Dim returnValue As IEnumerable(Of TElement)
returnValue = instance.Execute(requestUri)
public IEnumerable<TElement> Execute<TElement>(
Uri requestUri
)
public:
generic<typename TElement>
IEnumerable<TElement>^ Execute(
Uri^ requestUri
)
member Execute :
requestUri:Uri -> IEnumerable<'TElement>
JScript non supporta metodi e tipi generici.
Parametri di tipo
- TElement
Tipo restituito dalla query.
Parametri
- requestUri
Tipo: System.Uri
URI al quale verrà inviata la richiesta della query.Può essere qualsiasi URI del servizio dati valido.Può contenere parametri di query $.
Valore restituito
Tipo: System.Collections.Generic.IEnumerable<TElement>
Risultati dell'operazione di query.
Eccezioni
Eccezione | Condizione |
---|---|
WebException | Se non viene ricevuta una risposta da una richiesta a requestUri. |
ArgumentNullException | Se requestUri è Null. |
ArgumentException | Se requestUri non è un URI valido per il servizio dati. |
InvalidOperationException | Se viene generato un errore durante l'esecuzione della richiesta o la conversione del contenuto del messaggio di risposta in oggetti. |
DataServiceQueryException | Se il servizio dati restituisce un errore HTTP 404: Risorsa non trovata. |
Osservazioni
Il metodo Execute viene utilizzato per eseguire una query su un servizio dati in base all'URI; il metodo comporta la generazione di una richiesta HTTP GET al servizio dati. L'URI della richiesta specificato può essere assoluto o relativo.
Se requestUri è assoluto, questo metodo convalida se l'URI punta allo stesso servizio dati specificato in fase di costruzione di DataServiceContext. Se requestUri è relativo, questo metodo rimuove qualsiasi barra iniziale e aggiunge requestUri a quanto fornito in fase di costruzione di DataServiceContext. Se non è già presente, dopo l'URI passato al costruttore DataServiceContext viene aggiunta una barra.
Se viene restituito questo metodo, tutte le risposte HTTP per la richiesta sono state lette dal flusso di rete, ma non verranno elaborate. Non si verifica alcuna risoluzione di identità o materializzazione di oggetti completa per un'entità specificata nella risposta, finché non viene enumerata.
Esempi
In questo esempio viene utilizzato un ciclo do?while per caricare entità Customers dai risultati di paging del servizio dati. Il metodo Execute viene chiamato tramite l'URI di collegamento successivo per ricevere la successiva pagina di dati.
' 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
// 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);
}
Vedere anche
Riferimento
Spazio dei nomi System.Data.Services.Client