C
C#2y ago
shivam51

❔ Registering a class with string parameter for DI.

This I have a class MongoRepository which accepts a MongoDB database and collection name in the constructor parameters. How do I fix this file?
c#

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using Play.Catalog.Service.Entities;
using Play.Catalog.Service.Settings;

namespace Play.Catalog.Service.Repositories
{
public static class Extensions
{
public static IServiceCollection AddMongo(this IServiceCollection services)
{
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.String));

services.AddSingleton(provider =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
// This configuration compiles to be null.
var mongoDbSettings = configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>();
var mongoClient = new MongoClient(mongoDbSettings.ConnectionString);
return mongoClient.GetDatabase("service");
});
return services;
}
}
}
c#

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using Play.Catalog.Service.Entities;
using Play.Catalog.Service.Settings;

namespace Play.Catalog.Service.Repositories
{
public static class Extensions
{
public static IServiceCollection AddMongo(this IServiceCollection services)
{
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.String));

services.AddSingleton(provider =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
// This configuration compiles to be null.
var mongoDbSettings = configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>();
var mongoClient = new MongoClient(mongoDbSettings.ConnectionString);
return mongoClient.GetDatabase("service");
});
return services;
}
}
}
I am using this file in the Program.cs as
builder.Services.AddMongo();
builder.Services.AddMongo();
i
5 Replies
shivam51
shivam51OP2y ago
shivam51
shivam51OP2y ago
The mongoDbSettings is coming null. My Program.cs part
builder.Services.AddRazorPages()
.AddRazorRuntimeCompilation();

// Add services to the container.

builder.Services.Configure<MongoDbSettings>( builder.Configuration.GetSection("MongoDb"));
builder.Services.AddRazorPages()
.AddRazorRuntimeCompilation();

// Add services to the container.

builder.Services.Configure<MongoDbSettings>( builder.Configuration.GetSection("MongoDb"));
yes Aah, this was the issue. This was the correct line var mongoDbSettings = configuration.GetSection("MongoDb").Get<MongoDbSettings>(); Can you explain what you mean by this? I am an absolute noob
shivam51
shivam51OP2y ago
You mean like this?
shivam51
shivam51OP2y ago
"No service for type 'Play.Catalog.Service.Entities.MongoDbSettings' has been registered. I am configuring the MongoDbSettings here. but I get this error 😦 Works like charm! Thanks @Retax! Can you refer some material for DI and stuff related to this issue? would appreciate it.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server