Hi,
I'm trying to implement dependency injection in my project, but when executing it, the following error appears on the line where I register my AddInfraEstructure service:
System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' to type 'System.IServiceProvider'.'
How do I do this type of conversion?
Code:
public static class DependencyInjection
{
public static IServiceProvider AddInfraEstructure(this IServiceCollection services,
IConfiguration configuration)
{
services.AddDbContext
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
services.AddScoped
services.AddScoped
return services;
}
}
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Project.Infra.IoC;
using Project.MVC.MappingConfig;
namespace Project.MVC
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddInfraEstructure(Configuration);
services.AddAutoMapperConfiguration();
services.AddControllersWithViews();
services.AddRazorPages();
}
}
}
Sachin SinghPosted Aug 9, 2022, 4:50 AM
Kelly SoaresPosted Aug 21, 2022, 4:37 PM
Kelly SoaresPosted Aug 13, 2022, 7:16 PM
Kelly SoaresPosted Aug 9, 2022, 12:14 AM
Vishal YelvePosted Aug 8, 2022, 5:51 AM