I am currently working on a WebApp where I have an Azure Function API backend which talks to an Azure Cosmos Database. I have added Dependency Injection to the my project as described in this article: https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
My Startup class looks like this:
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")
)
)
);
}
}
}
And here is an example of how I use it in my functions:
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);
}
}
}
Everything was working as expected until I needed to add SignalR for one specific page which requires live data. After just installing the SignalR package Microsoft.Azure.WebJobs.Extensions.SignalRService, I noticed this error in the console:
[2020-10-14T07:05:00.381] Unsupported service transport type: . Use default Transient instead.
When I try to access the API endpoints, I get a 500 error on all endpoints and the following error in console:
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'.
I have updated all my packages to the latest versions, and tried to reinstall all packages but I still get the same error. When I remove the SignalR package, all the errors go away. I have tested this multiple times.
Changing the version to 1.0.2 removed the Dependency Injection error. I still get the Unsupported service transport type warning, but I can now query the data without exceptions.