Ger*_*rit 11 c# asp.net-web-api .net-core asp.net-core azure-cosmosdb
我想在 .Net 6 WebAPI 中使用 CosmosDB SQL-API。是否还有一个 AddCosmos() 方法可用于在不同的服务中注入客户端,或者是否真的有必要使用接口来实现我自己的 cosmos-service 以将客户端注入到我自己的服务类中?
Jas*_*Pan 13
using Microsoft.Azure.Cosmos;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "WebApplication1", Version = "v1" });
});
builder.Services.AddHttpClient();
builder.Services.AddSingleton<CosmosClient>(serviceProvider =>
{
//IHttpClientFactory httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
//CosmosClientOptions cosmosClientOptions = new CosmosClientOptions
//{
//HttpClientFactory = httpClientFactory.CreateClient,
//ConnectionMode = ConnectionMode.Gateway
//};
return new CosmosClient("<cosmosdb_connectionstring>");
// sample code
//return new CosmosClient("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");//, cosmosClientOptions);
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (builder.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication1 v1"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Run Code Online (Sandbox Code Playgroud)
然后你可以注入CosmosClient你的控制器。
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Cosmos;
using Microsoft.WindowsAzure.Storage.Table;
namespace WebApplication1.Controllers;
[ApiController]
[Route("[controller]")]
public class HomeController : ControllerBase
{
public CosmosClient _client;
private readonly ILogger<WeatherForecastController> _logger;
public HomeController(ILogger<WeatherForecastController> logger, CosmosClient client)
{
_logger = logger;
_client = client;
}
[HttpGet]
public async Task<string> getvalue() {
string dbname = "test";
string containername = "container1";
Database database = await _client.CreateDatabaseIfNotExistsAsync(dbname);
Container container = database.GetContainer(containername);
var query = container.GetItemQueryIterator<Test>("SELECT c.id FROM c");
string ss = string.Empty;
while (query.HasMoreResults)
{
FeedResponse<Test> result = await query.ReadNextAsync();
foreach (var item in result) {
ss += item.id;
}
}
return ss;
}
public class Test : TableEntity {
public int id { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
我的 cosmosdb 中的值。
通过api获取值
| 归档时间: |
|
| 查看次数: |
7483 次 |
| 最近记录: |