运行项目netcore

时间:2018-07-31 02:45:33

标签: c# asp.net-core .net-core automapper

当我运行netcore项目时,我收到一条消息{“ stateMachine”:{“ <> 1__state”:-1,“ <> t__builder”:{,但我不知道该如何解决。我在命令行中看到错误

  

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware [1]         执行请求时发生未处理的异常。   Newtonsoft.Json.JsonSerializationException:为类型为“ System.Runtime.CompilerServices.AsyncTaskMethodBuilder”的属性“ task”检测到自引用循环。

  

Microsoft.AspNetCore.Server.Kestrel [13]         连接ID“ 0HLFMHMJ7MBQN”,请求ID“ 0HLFMHMJ7MBQN:00000001”:应用程序引发了未处理的异常。   Newtonsoft.Json.JsonSerializationException:为类型为“ System.Runtime.CompilerServices.AsyncTaskMethodBuilder”的属性“ task”检测到自引用循环。

这是文件Startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<AppDbContext>(options =>
               options.UseSqlServer(Configuration.GetConnectionString("AppDbConnection"),
                   b => b.MigrationsAssembly("liyobe.Data")));

        services.AddIdentity<AppUser, AppRole>()
            .AddEntityFrameworkStores<AppDbContext>()
            .AddDefaultTokenProviders();
        // Configure Identity
        services.Configure<IdentityOptions>(options =>
        {
            // Password settings
            options.Password.RequireDigit = true;
            options.Password.RequiredLength = 6;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireLowercase = false;

            // Lockout settings
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
            options.Lockout.MaxFailedAccessAttempts = 10;

            // User settings
            options.User.RequireUniqueEmail = true;
        });

        services.AddAutoMapper();

        // Add application services.
        services.AddScoped<UserManager<AppUser>, UserManager<AppUser>>();
        services.AddScoped<RoleManager<AppRole>, RoleManager<AppRole>>();

        //CreateMapper(services, Configuration);
        //services.AddSingleton(Mapper.Configuration);
        services.AddScoped<IMapper>(sp => new Mapper(sp.GetRequiredService<AutoMapper.IConfigurationProvider>(), sp.GetService));

        services.AddTransient(typeof(IUnitOfWork), typeof(EFUnitOfWork));
        services.AddTransient(typeof(IAsyncRepository<,>), typeof(EFRepository<,>));
        services.AddTransient<IFunctionService, FunctionService>();
        services.AddTransient<DbInitializer>();
        //services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/error");
        }
        //app.UseStaticFiles();
        //app.UseHttpsRedirection();
        app.UseMvc();
    }

这是我的文件ValuesController

[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    IFunctionService _functionService;
    public ValuesController(IFunctionService functionService)
    {
        _functionService = functionService;
    }
    // GET api/values
    [HttpGet]
    public async  Task<IActionResult> Get()
    {
        try
        {
            var data = _functionService.GetAll("");
            return Ok(data);
        }
        catch (Exception ex)
        {
            throw new Exception();
        }
    }

这是FunctionService类中的getAll函数

public async Task<List<FunctionViewModel>> GetAll(string functionId)
    {
        var query = await _functionRepository.ListAllAsync();
        var result = _mapper.Map<List<Function>, List<FunctionViewModel>>(query);
        return result;
    }

这是函数类

public class FunctionViewModel
{
    public string Id { get; set; }

    [Required]
    [StringLength(128)]
    public string Name { set; get; }

    [Required]
    [StringLength(250)]
    public string URL { set; get; }

    [StringLength(128)]
    public string ParentId { set; get; }

    public string IconCss { get; set; }
    public int SortOrder { set; get; }
    public bool Status { set; get; }
}

这是函数类

[Table("Functions")]
public class Function : BaseEntity<string>, ISwitchable, ISortable
{
    public Function()
    {

    }
    public Function(string name, string url, string parentId, string iconCss, int sortOrder)
    {
        this.Name = name;
        this.URL = url;
        this.ParentId = parentId;
        this.IconCss = iconCss;
        this.SortOrder = sortOrder;
    }
    [Required]
    [StringLength(128)]
    public string Name { set; get; }

    [Required]
    [StringLength(250)]
    public string URL { set; get; }


    [StringLength(128)]
    public string ParentId { set; get; }

    public string IconCss { get; set; }
    public int SortOrder { set; get; }
    public bool Status { set; get; }
}

我在FunctionService中返回数据时看到错误发生。但是我不知道该如何解决。

1 个答案:

答案 0 :(得分:0)

 // GET api/values
[HttpGet]
public async  Task<IActionResult> Get()
{
    try
    {
        var data = await _functionService.GetAll("");
        return Ok(data);
    }
    catch (Exception ex)
    {
        throw new Exception();
    }
}