我有一个 Azure 函数,它正在查询一个肯定保存数据的 cosmos db mongo 集合。
但是,当通过我的 azure 函数查询时,该函数返回一个空数组。
我的功能看起来像:
[FunctionName("GetAll")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequest req,
TraceWriter log, [Inject(typeof(IWarehouseStateRepository))]IWarehouseStateRepository repoW)
{
log.Info("GetAll Function is processing a request");
var warehouses = repoW.GetAllWarehouses();
return warehouses != null ?
(ActionResult)new OkObjectResult(warehouses) :
new BadRequestObjectResult("Something went wrong with this request");
}
我正在为我的仓库存储库注入一个自定义类型,该类型依赖于如下所示的 db 配置上下文:
public class StateContext : IStateContext
{
private readonly IMongoDatabase _database = null;
private readonly string _connectionString;
private readonly string _dbName;
private readonly string _collectionName;
public StateContext()
{
_connectionString = Environment.GetEnvironmentVariable("AggregateDbConnectionDev");
_dbName = Environment.GetEnvironmentVariable("AggregateDbNameDev");
_collectionName = Environment.GetEnvironmentVariable("AggregateDbCollectionDev");
MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(_connectionString));
settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
var client = new MongoClient(settings);
if (client != null)
_database = client.GetDatabase(_dbName);
}
public IMongoCollection<WarehouseState> WarehouseState
{
get
{
return _database.GetCollection<WarehouseState>(_collectionName);
}
}
}
该函数不会引发任何错误或异常。
和回购查询:
public IEnumerable<WarehouseState> GetAllWarehouses()
{
var warehouses = _context.WarehouseState.Find(_ => true).ToList();
return warehouses;
}
有没有人知道这里可能出现什么问题?我是否需要使用 azure 功能以不同的方式设置 cosmos Db 连接?
在将逻辑移至 azure 函数之前,此设置已在之前有效。
我使用与上面相同的代码插入到集合中,并将它插入到数据库中具有相同集合名称的新集合中.. 仍然不知道为什么,但我想它现在可以工作了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句