c#无法解析类型服务

时间:2017-09-04 07:16:50

标签: c# api asp.net-core

我一直在努力解决这个错误,无法理解原因:

  

无法解析类型' Microsoft.Extensions.Configuration.IConfigurationRoot'在尝试激活' Zipcode_Service.Models.ZipcodeContext'。

我试着查看连接正在重新配置的所有位置,一切看起来都很好:

ZipcodeContext:

   public ZipcodeContext(IConfigurationRoot config, DbContextOptions options) : base(options)
    {
        _config = config;
    }
    public DbSet<City> Cities { get; set; }
    public DbSet<Zipcode> Zipcodes { get; set; }
    public DbSet<Street> Streets { get; set; }


    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        optionsBuilder.UseSqlServer(_config["ConnectionStrings:ZipcodeContextConnection"]);
    }

ZipcodeController:

public class CityController : Controller
{
    private ILogger<CityController> _logger;
    private IZipcodeRepository _repository;

    public CityController(ILogger<CityController> logger, IZipcodeRepository repository)
    {
        _logger = logger;
        _repository = repository;
    }

    [HttpGet("{cityName}")]
    public IActionResult GetCities(string cityName)
    {
            var results = _repository.GetCitiesByCityName(cityName);
            return Ok(Mapper.Map<IEnumerable<CityViewModel>>(results));
        }

ZipcodeRepository:

public class ZipcodeRepository : IZipcodeRepository
{
    private ZipcodeContext _context;

    public ZipcodeRepository(ZipcodeContext context)
    {
        _context = context;
    }

    public IEnumerable<City> GetCitiesByCityName(string cityName)
    {
       return _context.Cities.Where(t => t.Name.Contains(cityName)).ToList();
    }

IZipcodeInterface:

public interface IZipcodeRepository
{
    IEnumerable<City> GetCitiesByCityName(string cityName);
   // City GetCityNameById(int cityId);
}

我的创业公司:

   public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<ZipcodeContext>();
        services.AddScoped<IZipcodeRepository, ZipcodeRepository>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(_config.GetSection("Logging"));
        loggerFactory.AddDebug();
        app.UseMvc(config =>
        {
            config.MapRoute(
                name: "Default",
                template: "{controller}/{action}/{id?}",
                defaults: new { controller = "App", action = "Index" }
                );
        }

注意:禁用CityController的构造函数并使用空的构造函数,我猜错误来自_repository = repository

2 个答案:

答案 0 :(得分:3)

您可以通过两种方式处理案例中的选项:

只需注册IConfigurationRoot

public void ConfigureServices(IServiceCollection services)
{
   //...
   services.AddSingleton<IConfigurationRoot>(c => Configuration);
   //...
}

推荐的方法是创建特殊的ZipcodeOptions类,并使用Options模式注册它:

public void ConfigureServices(IServiceCollection services)
{
   //...
    services.Configure<ZipcodeOptions>(Configuration.GetSection("ZipcodeOptions"));
   //...
}

答案 1 :(得分:2)

您只需拨打services.AddSingleton<IConfigurationRoot>(_config);中的ConfigureServices