Ok, I am making a Api, trying to use DI.
My Controller:
 [Route("api/[controller]")]
    [ApiController]
    public class TerminalsController : ControllerBase
    {
        private readonly IServiceWrapper _service;
        public TerminalsController(IServiceWrapper service)
        {
            _service = service;
        }
        [HttpPost]
        public async Task<ActionResult> Post([FromBody] Message object)
        {
            try
            {
                Result result = await _service.Terminal.UpsertInfo(ternminalObject);
                if (result.shopId != -1 || result.deviceId != -1 || result.companyId != -1)
                {
                    return Ok(result);
                }
                else
                {
                    return BadRequest("Can not save info from session on database");
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Internal server error");
            }
        }
    }
And the code of my service:
public class TerminalService : ITerminalService
    {
        private readonly IRepositoryWrapper _repository;
        public TerminalService(IRepositoryWrapper repository) 
        {
            _repository = repository;
        }
        public async Task<Result> UpsertInfo(company company)
        {
            try
            {
                var company = await _repository.Company.GetById(int.Parse(company.Id))
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
When my code rise the line
 var company = await _repository.Company.GetById(int.Parse(company.Id))
I get the error
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Here there are my others class:
My factory:
 public class DbClientFactory<T>
    {
        private static Lazy<T> _factoryLazy = new Lazy<T>(
            () => (T)FormatterServices.GetUninitializedObject(typeof(T)),
            LazyThreadSafetyMode.ExecutionAndPublication);
        public static T Instance
        {
            get
            {
                return _factoryLazy.Value;
            }
        }
    }
The factory instace the service and the repositories.
This is my StartUp.cs:
 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            SqlHelper.connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING");
        }
        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.ConfigureCors();
            services.AddMvc();
            services.ConfigureServiceWrapper();
            services.ConfigureRepositoryWrapper();
            services.AddControllers().AddNewtonsoftJson();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCors("CorsPolicy");
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.All
            });
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
The ConfigureRepositoryWrapper and the ConfigureServiceWrapper are in the ServiceExtensions.cs:
 public static class ServiceExtensions
    {
        public static void ConfigureCors(this IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder.AllowAnyOrigin()
                    .AllowAnyMethod());
            });
        }
        public static void ConfigureRepositoryWrapper(this IServiceCollection services)
        {
            services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();
        }
        public static void ConfigureServiceWrapper(this IServiceCollection services)
        {
            services.AddScoped<IServiceWrapper, ServiceWrapper>();
        }
    }
The implement of ServiceWrapper is:
 public class ServiceWrapper : IServiceWrapper
    {
        private ITerminalService _terminal;
        public ITerminalService Terminal {
            get
            {
                if (_terminal == null)
                {
                    _terminal = DbClientFactory<TerminalService>.Instance;
                }
                return _terminal;
            }
        }
    }
And the implement of RepositoryWrapper is:
public class RepositoryWrapper : IRepositoryWrapper
    {
        private IDeviceRepository _device;
        private IShopRepository _shop;
        private ICompanyRepository _company;
        public IDeviceRepository Device
        {
            get
            {
                if (_device == null)
                {
                    _device = DbClientFactory<DeviceRepository>.Instance;
                }
                return _device;
            }
        }
        public IShopRepository Shop
        {
            get
            {
                if (_shop == null)
                {
                    _shop = DbClientFactory<ShopRepository>.Instance;
                }
                return _shop;
            }
        }
        public ICompanyRepository Company {
            get {
                if (_company == null)
                {
                    _company = DbClientFactory<CompanyRepository>.Instance;
                }
                return _company;
            }
        }
    }
I really dont know what is wrong here...
Thank you!
 
     
    