2

我目前正在开发一个 WebApp,其中我有一个与 Azure Cosmos 数据库对话的 Azure Function API 后端。如本文所述,我已将依赖注入添加到我的项目中:https ://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection

我的 Startup 类如下所示:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PoolScoreTrackerV2.API.Config;
using PoolScoreTrackerV2.Functions.Services;
using System;

[assembly: FunctionsStartup(typeof(PoolScoreTrackerV2.Functions.Startup))]

namespace PoolScoreTrackerV2.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddFilter(level => true);
            });

            builder.Services.AddOptions<CosmosDbOptions>()
                .Configure<IConfiguration>((settings, configuration) =>
                {
                    configuration.GetSection("CosmosDb").Bind(settings);
                });

            builder.Services.AddSingleton<ICosmosDbService>(
                new CosmosDbService(
                    new CosmosClient(
                        Environment.GetEnvironmentVariable("CosmosDb:CosmosDbConnection")
                        )
                    )
                );
        }
    }
}

这是我如何在我的函数中使用它的示例:

namespace PoolScoreTrackerV2.API.Functions.Matches
{
    public class GetMatches
    {
        private readonly ICosmosDbService _cosmosDbService;
        private readonly ILogger<GetMatches> _logger;
        private readonly CosmosDbOptions _settings;

        public GetMatches(ICosmosDbService cosmosDbService, ILogger<GetMatches> logger, IOptions<CosmosDbOptions> settings)
        {
            _cosmosDbService = cosmosDbService;
            _logger = logger;
            _settings = settings.Value;
            _cosmosDbService.InitializeCollection(_settings.DatabaseId, _settings.ContainerId);
        }

        [FunctionName("GetMatches")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "matches")] HttpRequest req, ClaimsPrincipal principal)
        {
            if (ValidationExtensions.AuthorizeUser(principal) == false)
            {
                return new UnauthorizedResult();
            }

            var data = await _cosmosDbService.GetAsync<Match>("SELECT * FROM m where m.type = 'match'");

            _logger.LogInformation("GET Matches");

            return new OkObjectResult(data);
        }
    }
}

一切都按预期工作,直到我需要为需要实时数据的特定页面添加 SignalR。在刚刚安装 SignalR 包 Microsoft.Azure.WebJobs.Extensions.SignalRService 之后,我在控制台中注意到了这个错误:

[2020-10-14T07:05:00.381] Unsupported service transport type: . Use default Transient instead.

当我尝试访问 API 端点时,我在所有端点上都收到 500 错误,并且在控制台中出现以下错误:

Executed 'GetMatches' (Failed, Id=3a10737c-7abe-4e18-aeb6-1d442b36ef7a, Duration=43ms)
[2020-10-14T07:05:08.289] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'PoolScoreTrackerV2.Functions.Services.ICosmosDbService' while attempting to activate 'PoolScoreTrackerV2.API.Functions.Matches.GetMatches'.

我已将所有软件包更新到最新版本,并尝试重新安装所有软件包,但仍然出现相同的错误。当我删除 SignalR 包时,所有错误都消失了。我已经对此进行了多次测试。

4

1 回答 1

1

将版本更改为 1.0.2 消除了依赖注入错误。我仍然收到不支持的服务传输类型警告,但我现在可以毫无例外地查询数据。

于 2020-10-14T08:19:15.367 回答