Stored procedures 在我的CosmosDb中创建了存储过程,如何通过我的.net wep API(连接到我的CosmosDb)访问它们

Stored procedures 在我的CosmosDb中创建了存储过程,如何通过我的.net wep API(连接到我的CosmosDb)访问它们,stored-procedures,azure-cosmosdb,server-side,Stored Procedures,Azure Cosmosdb,Server Side,我已经创建了名为sample的服务器端存储过程(这是默认存储过程),如何在.net web API中调用它?参考: 使用,可以执行存储过程,如下所示: CosmosClient client = new CosmosClient("connection string"); Container container = client.GetContainer("YourDbName", "YourContainerName"); StoredProcedureExecuteResponse resp

我已经创建了名为sample的服务器端存储过程(这是默认存储过程),如何在.net web API中调用它?

参考:

使用,可以执行存储过程,如下所示:

CosmosClient client = new CosmosClient("connection string");
Container container = client.GetContainer("YourDbName", "YourContainerName");
StoredProcedureExecuteResponse response = await container.Scripts.ExecuteStoredProcedureAsync(
                "sample", // your stored procedure name
                new PartitionKey("Partition Key you want to run it on"),
                new dynamic[] { "Your First parameter", "Your second parameter" });

// If your stored procedure returns some information, you can use the Type of the data you expect. For example, if you expect a string
StoredProcedureExecuteResponse<string> responseWithData = await container.Scripts.ExecuteStoredProcedureAsync<string>(
                "sample", // your stored procedure name
                new PartitionKey("Partition Key you want to run it on"),
                new dynamic[] { "Your First parameter", "Your second parameter" });
并将
CosmosClient
实例注入控制器:

public class MyController
{
    private readonly CosmosClient cosmosClient;
    public MyController(CosmosClient cosmosClient)
    {
        this.cosmosClient = cosmosClient;    
    }

    public async Task<IActionResult> MyMethod()
    {
        // execute the stored procedure or use this.cosmosClient

    }
}
公共类MyController
{
私有只读CosmosClient CosmosClient;
公共MyController(CosmosClient CosmosClient)
{
this.cosmosClient=cosmosClient;
}
公共异步任务MyMethod()
{
//执行存储过程或使用此.cosmosClient
}
}

完整的Web API示例:

编辑后的答案更明确地说哈哈哈不,我们都在同一条船上,我们都在学习。示例at是Web API ASP.NET核心应用程序,而不是控制台项目。使用同一个项目了解如何将CosmosClient添加到Web API,请查看Startup.cs文件,它会像我的回答一样注册客户。然后,如果您想在某个API控制器中调用存储过程,可以像我在回答中那样将其注入API方法,并在API方法中使用
container.Scripts.ExecuteStoredProcedureAsync
执行它。很高兴它起到了作用,请记住标记答案,以便将来帮助他人。
public class MyController
{
    private readonly CosmosClient cosmosClient;
    public MyController(CosmosClient cosmosClient)
    {
        this.cosmosClient = cosmosClient;    
    }

    public async Task<IActionResult> MyMethod()
    {
        // execute the stored procedure or use this.cosmosClient

    }
}