将 Microsoft.Azure.WebJobs.Extensions.SignalRService 包添加到 Azure Function 项目会破坏我的依赖注入
Adding Microsoft.Azure.WebJobs.Extensions.SignalRService package to Azure Function project breaks my Dependency Injection
我目前正在开发一个 WebApp,其中我有一个 Azure Functions API 后端,它与 Azure Cosmos 数据库对话。我已按照本文所述将依赖注入添加到我的项目中:https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
我的启动 class 看起来像这样:
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 包时,所有错误都消失了。我已经测试过多次了。
将版本更改为 1.0.2 消除了依赖注入错误。我仍然收到不支持的服务传输类型警告,但我现在可以毫无例外地查询数据。
我目前正在开发一个 WebApp,其中我有一个 Azure Functions API 后端,它与 Azure Cosmos 数据库对话。我已按照本文所述将依赖注入添加到我的项目中:https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
我的启动 class 看起来像这样:
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 包时,所有错误都消失了。我已经测试过多次了。
将版本更改为 1.0.2 消除了依赖注入错误。我仍然收到不支持的服务传输类型警告,但我现在可以毫无例外地查询数据。