<p>I have an API and MongoDB that was hosted on Kubernetes (EKS on AWS specifically). While my API runs completely fine on my local and also Docker local, it was not on the Kubernetes cluster.</p>
<p>When I get the logs from <code>kubernetes logs -f &lt;pod-name&gt;</code>, I get below exceptions:</p>
<pre><code>Unexpected generic exception occurred Autofac.Core.DependencyResolutionException: An exception was thrown while activating QueryService -&gt; Database -&gt; DbClient.
       ---&gt; Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.Extensions.Options.IOptions`1[DbOptions])' on type 'DbClient'.
       ---&gt; 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&amp; decoratorTarget)
         --- End of inner exception stack trace ---
         at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters, Object&amp; 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.&lt;&gt;c__DisplayClass5_1`1.&lt;CreateLazyRegistration&gt;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.&lt;&gt;c__DisplayClass4_0.&lt;&lt;ListLogicalDevicesAsync&gt;b__0&gt;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
</code></pre>
<p>The dependency injection should have no problem since I can run it locally without any trouble. So is it the connection string that I provided for MongoDB not working? Or is it really the dependency injection issue?</p>
<p>Below are my code for constructor in <code>DbOperation.cs</code></p>
<pre><code>public DbOperations(string connectionString, string dataBase, string collection)
    var mongoClient = new MongoClient(connectionString);
    var mongoDatabase = mongoClient.GetDatabase(dataBase);
     _collection = mongoDatabase.GetCollection&lt;BsonDocument&gt;(collection);
}
</code></pre>
<p>My <code>DbClient.cs</code></p>
<pre><code>internal class DbClient : DbOperations, IDbClient
{
    public DbClient(IOptions&lt;DbOptions&gt; dbOptions) : 
        base(dbOptions.Value.ConnectionString,dbOptions.Value.Database, dbOptions.Value.DeviceCollection)
    {
    }
}
</code></pre>
<p>My <code>Startup.cs</code></p>
<pre><code>//In ConfigureContainer() method
builder.RegisterType&lt;DbClient&gt;().As&lt;IDbClient&gt;();

//In ConfigureServices() method
services.Configure&lt;DbOptions&gt;(configuration.GetSection(&quot;MainDb&quot;));
</code></pre>
<p>My <code>DbOptions.cs</code></p>
<pre><code>public class DbOptions
{
    public string ConnectionString { get; set; }
    public string Database { get; set; }
    public string DeviceCollection { get; set; }
}
</code></pre>
<p>My <code>appsetting.json</code></p>
<pre><code>  &quot;MainDb&quot;: {
    &quot;Database&quot;: &quot;main&quot;,
    &quot;DeviceCollection&quot;: &quot;data&quot;
  }
</code></pre>
<p>My <code>appsetting.Dev.json</code></p>
<pre><code>  &quot;MainDb&quot;: {
    &quot;ConnectionString&quot;: &quot;mongodb://&lt;service-name&gt;.default.svc.cluster.local:27017&quot;
  }
</code></pre>
<p>May I know is there anything that might gone wrong? Is it the MongoDB connection string? Or the dependency injection that could not get the connection string?</p>
<p>Thanks!</p>

Sivvie Lim
<p>Based on the exception message and the stack trace, <code>connectionString</code> is <code>null</code>. Check your config -- it's missing in your non-dev config.</p>
