.NET Core API 正在抛出 Autofac.Core.DependencyResolutionException

.NET Core API is throwing Autofac.Core.DependencyResolutionException

我有一个 API 和 MongoDB 托管在 Kubernetes 上(特别是 AWS 上的 EKS)。虽然我的 API 运行 在我的本地和 Docker 本地都完全正常,但它不在 Kubernetes 集群上。

当我从 kubernetes logs -f <pod-name> 获取日志时,出现以下异常:

Unexpected generic exception occurred Autofac.Core.DependencyResolutionException: An exception was thrown while activating QueryService -> Database -> DbClient.
       ---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptions`1[DbOptions])' on type 'DbClient'.
       ---> System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
         at MongoDB.Driver.Core.Misc.Ensure.IsNotNull[T](T value, String paramName)
         at MongoDB.Driver.Core.Configuration.ConnectionString..ctor(String connectionString, IDnsResolver dnsResolver)
         at MongoDB.Driver.Core.Configuration.ConnectionString..ctor(String connectionString)
         at MongoDB.Driver.MongoUrlBuilder.Parse(String url)
         at MongoDB.Driver.MongoUrlBuilder..ctor(String url)
         at MongoDB.Driver.MongoUrl..ctor(String url)
         at MongoDB.Driver.MongoClientSettings.FromConnectionString(String connectionString)
         at MongoDB.Driver.MongoClient..ctor(String connectionString)
         at Mongo.DbOperations..ctor(String connectionString, String dataBase, String collection) in /src/Mongo/DbOperations.cs:line 36
         at DbClient..ctor(IOptions`1 DbOptions) in /src/DbClient.cs:line 22
         at lambda_method(Closure , Object[] )
         at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
         --- End of inner exception stack trace ---
         at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
         at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
         at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters, Object& decoratorTarget)
         --- End of inner exception stack trace ---
         at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters, Object& decoratorTarget)
         at Autofac.Core.Resolving.InstanceLookup.Execute()
         at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request)
         at Autofac.Core.Resolving.ResolveOperation.Execute(ResolveRequest request)
         at Autofac.Features.LazyDependencies.LazyRegistrationSource.<>c__DisplayClass5_1`1.<CreateLazyRegistration>b__1()
         at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
         at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
         at System.Lazy`1.CreateValue()
         at Api.Controllers.MainController.<>c__DisplayClass4_0.<<ListLogicalDevicesAsync>b__0>d.MoveNext() in /src/Api/Controllers/Main/List.cs:line 40
      --- End of stack trace from previous location where exception was thrown ---
         at Api.Controllers.StandardController.ProcessExceptionsAsync(Func`1 action) in /src/Api/Controllers/StandardController.cs:line 64

依赖注入应该没有问题,因为我可以在本地 运行 它没有任何问题。那么是不是我为 MongoDB 提供的连接字符串不起作用?还是真的是依赖注入问题?

下面是我在 DbOperation.cs

中的构造函数代码
public DbOperations(string connectionString, string dataBase, string collection)
    var mongoClient = new MongoClient(connectionString);
    var mongoDatabase = mongoClient.GetDatabase(dataBase);
     _collection = mongoDatabase.GetCollection<BsonDocument>(collection);
}

我的DbClient.cs

internal class DbClient : DbOperations, IDbClient
{
    public DbClient(IOptions<DbOptions> dbOptions) : 
        base(dbOptions.Value.ConnectionString,dbOptions.Value.Database, dbOptions.Value.DeviceCollection)
    {
    }
}

我的Startup.cs

//In ConfigureContainer() method
builder.RegisterType<DbClient>().As<IDbClient>();

//In ConfigureServices() method
services.Configure<DbOptions>(configuration.GetSection("MainDb"));

我的DbOptions.cs

public class DbOptions
{
    public string ConnectionString { get; set; }
    public string Database { get; set; }
    public string DeviceCollection { get; set; }
}

我的appsetting.json

  "MainDb": {
    "Database": "main",
    "DeviceCollection": "data"
  }

我的appsetting.Dev.json

  "MainDb": {
    "ConnectionString": "mongodb://<service-name>.default.svc.cluster.local:27017"
  }

请问有什么问题吗?是 MongoDB 连接字符串吗?还是无法获取连接字符串的依赖注入?

谢谢!

根据异常消息和堆栈跟踪,connectionStringnull。检查您的配置——它在您的非开发配置中丢失。