Quiet Molly
Quiet Molly

Reputation: 102

Cannot instantiate implementation type for service type in .NET Core

My Excel service generates reports.

public class ExcelService : IExcelService
    {
        private readonly List<IReportFactory> _reports;

        public ExcelService(List<IReportFactory> reports)
        {
            _reports = reports;
        }

        public Stream Generate(string reportName, params ReportParameter[] parameters)
        {
            var reportFactory = GetReportFactory(reportName);
            var report = reportFactory.Create(parameters);

            MemoryStream stream = new MemoryStream(report.Generate());
            return stream;
        }

        private IReportFactory GetReportFactory(string reportName)
        {
            var reportFactory = _reports.Find(factory => factory.Name == reportName);
            if (reportFactory != null)
            {
                return reportFactory;
            }
            throw new NotSupportedException($"Report {reportName} is not supported. Report is not registerd.");
        }
    }

 //Service

public interface IExcelService
    {
        Stream Generate(string reportName, params ReportParameter[] parameters);
    }

To achieve that it uses ReportParameters from IReportFactory

public interface IReportFactory
    {
        string Name { get; }
        Report Create(ReportParameter[] parameters);
    }

which takes Name attribute from exact reports:

public class GIDReportFactory : IReportFactory
    {
        public const string Name = nameof(GIDReport);

        public const string DataSourceParam = "DataSource";
        string IReportFactory.Name
        {
            get
            {
                return Name;
            }
        }
    }

To make it work I had to add service in my Startup.cs

        private static void RegisterServices(IServiceCollection services)
        {
            services.AddScoped<IExcelService, ExcelService>();
        }

and now I'm stuck at error:

'Some services are not able to be constructed (Error while validating the service descriptor
'ServiceType: ...Services.IExcelService Lifetime: Scoped ImplementationType: ...Services.ExcelService': Unable to resolve service for type 
 'System.Collections.Generic.List`1
[....Services.ExcelReports.Factories.IReportFactory]' while attempting to activate '...Services.ExcelService'.)'

I know that its connected with my IRreportFactory and fact that it's not included inside of Startup.cs file. But I've got no clue how I could possibly add it there.

While I tried adding it via AddScoped<IReportFactory, GIDReportFactory>(); I'm ending up with error:

'Cannot instantiate implementation type '...Services.ExcelReports.Factories.IReportFactory' for service type '...Services.ExcelReports.Factories.IReportFactory'.'

I've got really no idea what might be the good solution.

Upvotes: 0

Views: 4785

Answers (1)

Charles Han
Charles Han

Reputation: 2010

In your ExcelService.cs, change the constructor as below,

public ExcelService(IEnumerable<IReportFactory> reports)
{
    _reports = reports.ToList();
}

Now, in your DI setup, add the below code,

services.AddScoped<IReportFactory, GIDReportFactory>();
services.AddScoped<IExcelService, ExcelService>();

See the reference.

Upvotes: 2

Related Questions