问题描述
我目前正在Web应用程序上工作,该应用程序具有一个与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软件包时,所有错误都会消失。我已经对此进行了多次测试。
解决方法
将版本更改为1.0.2消除了依赖注入错误。我仍然收到不支持的服务传输类型警告,但现在可以无例外地查询数据。