你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

CosmosScripts.ExecuteStoredProcedureStreamAsync 方法

定义

以异步操作的形式在 Azure Cosmos 服务中针对容器执行存储过程,并获取流作为响应。

public abstract System.Threading.Tasks.Task<Azure.Response> ExecuteStoredProcedureStreamAsync (string storedProcedureId, Azure.Cosmos.PartitionKey partitionKey, object[] parameters, Azure.Cosmos.StoredProcedureRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ExecuteStoredProcedureStreamAsync : string * Azure.Cosmos.PartitionKey * obj[] * Azure.Cosmos.StoredProcedureRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response>
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 Response)

参数

storedProcedureId
String

要执行的存储过程的标识符。

partitionKey
PartitionKey

项的分区键。 PartitionKey

parameters
Object[]

(可选) 表示存储过程参数的动态对象数组。

requestOptions
StoredProcedureRequestOptions

(可选) 存储过程请求的选项 StoredProcedureRequestOptions

cancellationToken
CancellationToken

(表示请求取消的可选) CancellationToken

返回

表示异步操作的服务响应的任务对象,该操作将包含存储过程中的任何响应集。

例外

如果 storedProcedureId 为 或 partitionKey ,则为 。

示例

这会创建并执行一个存储过程,该存储过程将字符串追加到从查询返回的第一个项。

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."");
   }";

CosmosScripts scripts = this.container.Scripts;
string sprocId = "appendString";
Response<StoredProcedureProperties> storedProcedureResponse = await scripts.CreateStoredProcedureAsync(
        sprocId,
        sprocBody);

// Execute the stored procedure
Response sprocResponse = await scripts.ExecuteStoredProcedureStreamAsync(
                        sprocId,
                        new PartitionKey(testPartitionId),
                        new dynamic[] {"myPrefixString", "myPostfixString"});

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

/// 

适用于