Share via


Scripts.ExecuteStoredProcedureStreamAsync Method

Definition

Overloads

ExecuteStoredProcedureStreamAsync(String, PartitionKey, Object[], StoredProcedureRequestOptions, CancellationToken)

Executes a stored procedure against a container as an asynchronous operation in the Azure Cosmos service and obtains a Stream as response.

ExecuteStoredProcedureStreamAsync(String, Stream, PartitionKey, StoredProcedureRequestOptions, CancellationToken)

Executes a stored procedure against a container as an asynchronous operation in the Azure Cosmos service and obtains a Stream as response.

ExecuteStoredProcedureStreamAsync(String, PartitionKey, Object[], StoredProcedureRequestOptions, CancellationToken)

Source:
Scripts.cs

Executes a stored procedure against a container as an asynchronous operation in the Azure Cosmos service and obtains a Stream as response.

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

Parameters

storedProcedureId
String

The identifier of the Stored Procedure to execute.

partitionKey
PartitionKey

The partition key for the item.

parameters
Object[]

An array of dynamic objects representing the parameters for the stored procedure. This can be null if no parameters are required.

requestOptions
StoredProcedureRequestOptions

(Optional) The options for the stored procedure request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

The task object representing the service response for the asynchronous operation which would contain any response set in the stored procedure.

Exceptions

If storedProcedureId or partitionKey are not set.

Examples

This creates and executes a stored procedure that appends a string to the first item returned from the query.

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
ResponseMessage sprocResponse = await scripts.ExecuteStoredProcedureStreamAsync(
                        sprocId,
                        new PartitionKey(testPartitionId),
                        new dynamic[] {"myPrefixString", "myPostfixString"});

using (StreamReader sr = new StreamReader(sprocResponse.Content))
{
    string stringResponse = await sr.ReadToEndAsync();
    Console.WriteLine(stringResponse);
 }

/// 

Applies to

ExecuteStoredProcedureStreamAsync(String, Stream, PartitionKey, StoredProcedureRequestOptions, CancellationToken)

Source:
Scripts.cs

Executes a stored procedure against a container as an asynchronous operation in the Azure Cosmos service and obtains a Stream as response.

public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage> ExecuteStoredProcedureStreamAsync (string storedProcedureId, System.IO.Stream streamPayload, Microsoft.Azure.Cosmos.PartitionKey partitionKey, Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ExecuteStoredProcedureStreamAsync : string * System.IO.Stream * Microsoft.Azure.Cosmos.PartitionKey * Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage>
Public MustOverride Function ExecuteStoredProcedureStreamAsync (storedProcedureId As String, streamPayload As Stream, partitionKey As PartitionKey, Optional requestOptions As StoredProcedureRequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResponseMessage)

Parameters

storedProcedureId
String

The identifier of the Stored Procedure to execute.

streamPayload
Stream

A Stream containing the payload which should represent a JSON array or arraylike object of parameters. This is parsed using JSON.parse and Function.apply uses the result to call the stored procedure. This can be null if no parameters are required.

partitionKey
PartitionKey

The partition key for the item.

requestOptions
StoredProcedureRequestOptions

(Optional) The options for the stored procedure request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

The task object representing the service response for the asynchronous operation which would contain any response set in the stored procedure. The response will contain status code (400) BadRequest if streamPayload represents anything other than a JSON array, object or null.

Exceptions

If storedProcedureId or partitionKey are not set.

Examples

This creates and executes a stored procedure that appends a string to the first item returned from the query.

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);

// Serialize the parameters into a stream
string[] parameters = new string[] { "myPrefixString", "myPostfixString" };
byte[] serializedBytes = JsonSerializer.SerializeToUtf8Bytes(parameters);
MemoryStream streamPayload = new MemoryStream(serializedBytes);

// Execute the stored procedure
ResponseMessage sprocResponse = await scripts.ExecuteStoredProcedureStreamAsync(
                        sprocId,
                        streamPayload,
                        new PartitionKey(testPartitionId));

using (StreamReader sr = new StreamReader(sprocResponse.Content))
{
    string stringResponse = await sr.ReadToEndAsync();
    Console.WriteLine(stringResponse);
 }

/// 

Applies to