Hello Experts,
I am not able to understand that why my below mentioned businessObject is not getting proper IDbConnectionFactory instance.
I have also noted that in my service class which is inherited from Service base class is properly getting the IDbConnectionFactory however when the control is reaching to BusinessObject where I want to orchestrate things and business logic there this IDbConnectionFactory is null
Here is the appHost where IDBConnectionString is added as singleton.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
[assembly: HostingStartup(typeof(Realtor.ConfigureDb))]
namespace Realtor
{
public class ConfigureDb : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context, services) =>
{
services.AddSingleton<IDbConnectionFactory>(new OrmLiteConnectionFactory(
context.Configuration.GetConnectionString("DefaultConnection"), PostgreSqlDialect.Provider));
})
.ConfigureAppHost(appHost =>
{
// Enable built-in Database Admin UI at /admin-ui/database
// appHost.Plugins.Add(new AdminDatabaseFeature());
// This is the piece of code if we need to use multiple databases, such as Multi Tenancy Case
var logDbConnStr = "ConnectionStrings:LogDbConnection";
//var constr = appHost.AppSettings.Get<string>(appSettingsKey);
//var logDbConnString = Convert.ToString(appHost.AppSettings.GetDictionary("ConnectionStrings")["LogDbConnection"]);
var logDbConnString = appHost.AppSettings.GetString(logDbConnStr);
var dbFactory = appHost.Container.Resolve<IDbConnectionFactory>() as OrmLiteConnectionFactory;
if (dbFactory == null) return;
dbFactory.RegisterConnection("LogDbConnection", logDbConnString, PostgreSqlDialect.Provider);
//var db = dbFactory.OpenDbConnectionString(logDbConnString);
});
}
}
Business Object
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.OrmLite;
namespace Realtor.BusinessObjects.Features
{
public class MainCategoryBo
{
public MainCategoryBo() { }
public IDbConnectionFactory? DbConnectionFactory { get; set; }
public bool GetMainCategoires()
{
var result = false;
var mainCategoryData = new Realtor.Data.Dac.MainCategory();
using (var db = DbConnectionFactory.Open())
{
// here DbConnectionFactory is null
}
var mainCategoreis = mainCategoryData.GetMainCategories();
return result;
}
}
}
Kindly advise…