Freigeben über


Scripts.ExecuteStoredProcedureAsync<TOutput> Methode

Definition

Führt eine gespeicherte Prozedur für einen Container als asynchronen Vorgang im Azure Cosmos-Dienst aus.

public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.Scripts.StoredProcedureExecuteResponse<TOutput>> ExecuteStoredProcedureAsync<TOutput>(string storedProcedureId, Microsoft.Azure.Cosmos.PartitionKey partitionKey, object[] parameters, Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ExecuteStoredProcedureAsync : string * Microsoft.Azure.Cosmos.PartitionKey * obj[] * Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.Scripts.StoredProcedureExecuteResponse<'Output>>
Public MustOverride Function ExecuteStoredProcedureAsync(Of TOutput) (storedProcedureId As String, partitionKey As PartitionKey, parameters As Object(), Optional requestOptions As StoredProcedureRequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of StoredProcedureExecuteResponse(Of TOutput))

Typparameter

TOutput

Der Rückgabetyp, der json serialisierbar ist.

Parameter

storedProcedureId
String

Der Bezeichner der auszuführenden gespeicherten Prozedur.

partitionKey
PartitionKey

Der Partitionsschlüssel für das Element.

parameters
Object[]

(Optional) Ein Array dynamischer Objekte, die die Parameter für die gespeicherte Prozedur darstellen.

requestOptions
StoredProcedureRequestOptions

(Optional) Die Optionen für die Anforderung der gespeicherten Prozedur.

cancellationToken
CancellationToken

(Optional) CancellationToken stellt die Anforderungsabbruch dar.

Gibt zurück

Das Taskobjekt, das die Dienstantwort für den asynchronen Vorgang darstellt, der einen beliebigen Antwortsatz in der gespeicherten Prozedur enthalten würde.

Ausnahmen

Wenn storedProcedureId oder partitionKey nicht festgelegt sind.

Beispiele

Dadurch wird eine gespeicherte Prozedur erstellt und ausgeführt, die eine Zeichenfolge an das erste von der Abfrage zurückgegebene Element anfüge.

string sprocBody = @"function simple(prefix, postfix)
   {
       var collection = getContext().getCollection();

       // Query documents and take 1st item.
       var isAccepted = collection.queryDocuments(
       collection.getSelfLink(),
       'SELECT * FROM root r',
       function(err, feed, options) {
           if (err)throw err;

           // Check the feed and if it's empty, set the body to 'no docs found',
           // Otherwise just take 1st element from the feed.
           if (!feed || !feed.length) getContext().getResponse().setBody(""no docs found"");
           else getContext().getResponse().setBody(prefix + JSON.stringify(feed[0]) + postfix);
       });

       if (!isAccepted) throw new Error(""The query wasn't accepted by the server. Try again/use continuation token between API and script."");
   }";

Scripts scripts = this.container.Scripts;
string sprocId = "appendString";
StoredProcedureResponse storedProcedureResponse = await scripts.CreateStoredProcedureAsync(
        sprocId,
        sprocBody);

// Execute the stored procedure
StoredProcedureExecuteResponse<string> sprocResponse = await scripts.ExecuteStoredProcedureAsync<string>(
                        sprocId,
                        new PartitionKey(testPartitionId),
                        new dynamic[] {"myPrefixString", "myPostfixString"});

Console.WriteLine(sprocResponse.Resource);
/// 

Gilt für: