尝试激活'BuySell_20190423.Controllers.StockController'时无法解析类型为'System.String'的服务

时间:2019-05-05 14:05:26

标签: asp.net-core

我对此后端做了很多补充。现在,从值控制器复制到股票控制器的基本HTTPPOST在chome窗口中给了我这个错误:

InvalidOperationException: Unable to resolve service for type 
system.String' while attempting to activate 
BuySell_20190423.Controllers.StockController'.

这是我的库存控制员职位:

        [HttpPost]
    public void Post([FromBody]string value)
    {

    }

在邮递员中,我使用此正文得到500个内部错误

{"UserName":"johndoe"}

这是我的一些创业公司。

namespace BuySell_20190423
{
public class Startup
{


    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public static IConfiguration Configuration { get; private set; }

    public void ConfigureServices(IServiceCollection services)
    {

    services.AddCors(o => o.AddPolicy("CorsPolicy", corsBuilder =>
    {
       corsBuilder.AllowAnyOrigin()
                  .AllowAnyMethod()
                  .AllowAnyHeader()
                  .AllowCredentials();
    }));

    services.AddDbContext<Helpers.DataContext>(x => x.UseInMemoryDatabase("TestDb"));
    services.AddDbContext<Models.StockContext>(opt => opt.UseInMemoryDatabase("item"));

    services.AddDbContext<AppDbContext>(x => x.UseInMemoryDatabase("AppDb"));
    // Auto Mapper Configurations
    var mappingConfig = new MapperConfiguration(mc =>
    {
      mc.AddProfile(new MappingProfile());
    });

    IMapper mapper = mappingConfig.CreateMapper();
    services.AddSingleton(mapper);
    //services.AddScoped<Post>(_ => new MyService("value here"));
        // configure strongly typed settings objects
        var appSettingsSection = Configuration.GetSection("AppSettings");
    services.Configure<AppSettings>(appSettingsSection);


    // configure DI for application services
    services.AddScoped<IUserService, UserService>();
        // configure DI for string services
        services.AddHttpClient();


        app.UseCors("CorsPolicy");
        app.UseHttpsRedirection();

        app.UseMvc();
    }
}

这是股票控制器的构造函数:

        public StockController(String context, IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
        _context = context;
    }

1 个答案:

答案 0 :(得分:1)

您必须删除string context参数,依赖项注入系统无法知道应该是什么。