System.InvalidOperationException:“无法解析类型为ASP_Core_CosmosDB_Test.Services。AzureKeyVaultService的服务”

时间:2019-12-28 01:17:08

标签: c# asp.net-mvc service interface

为什么我会出错?

IAzureKeyVaultService

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ASP_Core_CosmosDB_Test.Models;

namespace ASP_Core_CosmosDB_Test.Services
{
    public interface IAzureKeyVaultService
    {
        Task<String> GetKey(string id);
    }
}

AzureKeyVaultService

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;


namespace ASP_Core_CosmosDB_Test.Services
{
    public class AzureKeyVaultService : IAzureKeyVaultService
    {

        public async Task<String> GetKey(string id)
        {
            var azureServiceTokenProvider = new AzureServiceTokenProvider();
            var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
            var key = await keyVaultClient.GetSecretAsync(“Url”).ConfigureAwait(false);

            return key.Value.ToString();
        }
    }
}

启动

private readonly IAzureKeyVaultService _AzureKeyVaultService;

public Startup(IAzureKeyVaultService AzureKeyVaultService)
        {
            _AzureKeyVaultService = AzureKeyVaultService;
        }

  private async Task<CosmosDbService> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection)
        {
            string databaseName = configurationSection.GetSection("DatabaseName").Value;
            string containerName = configurationSection.GetSection("ContainerName").Value;
            string account = configurationSection.GetSection("Account").Value;


            string key = await _AzureKeyVaultService.GetKey("Test");
….
}

为什么当我启动ASP.Net Core MVC时尝试激活“ ASP_Core_CosmosDB_Test.Startup”时,收到错误消息“:“无法解析类型为'ASP_Core_CosmosDB_Test.Services.IAzureKeyVaultService'的服务。”

问候 斯蒂芬

0 个答案:

没有答案