问题
Few days ago I decided to rewrite old API project from .net core 2.0 to 3.1 but today I faced with an error which I cannot solve. After rewriting "AccountManager" class _signInManager.PasswordSignInAsync() starts to throw error connected with Json object cycle.
Exception:
((System.Text.Json.JsonException)ex).Message: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.
Example of usage:
public class AccountManager : IAccountManager{
public ApplicationUser ActualUser { get; private set; }
private readonly SignInManager<ApplicationUser> _signInManager;
public AccountManager(SignInManager<ApplicationUser> signInManager)
{
_signInManager = signInManager;
}
public async Task<LoginResponseDTO> LoginAsync(LoginRequestDTO input)
{
var result = await _signInManager.PasswordSignInAsync(input.Email, input.Password, false, true);
...
}
}
Startup:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
...
var builder = services.AddIdentityCore<ApplicationUser>(u =>
{
...
})
.AddEntityFrameworkStores<RBDbContext>().AddDefaultTokenProviders()
.AddSignInManager<UserManager<ApplicationUser>>()
.AddUserManager<UserManager<ApplicationUser>>();
builder.AddEntityFrameworkStores<RBDbContext>().AddDefaultTokenProviders();
services.AddScoped<IAccountManager,AccountManager>();
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
I even tried to user and other classses attribute [JsonIgnore], but it didn't worked. Application user class:
public class ApplicationUser : IdentityUser
{
public string Avatar { get; set; }
...
[JsonIgnore]
public virtual ICollection<Order> Orders{ get; set; }
[JsonIgnore]
public virtual ICollection<Participation> Participations{ get; set; }
[JsonIgnore]
public virtual ICollection<UserProduct> UserProducts{ get; set; }
}
UserManager comes from Microsoft.Extensions.Identity.Core, Version=3.1.2.0
回答1:
A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.
In ASP.NET Core 3.0+, it uses System.Text.Json by default for JSON serialization, and some features of Newtonsoft.Json may not work well with System.Text.Json (for example, Newtonsoft.Json doesn't have a maximum depth limit by default), which might cause this issue.
You can try to install the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package to add support for Newtonsoft.Json based formatters and features with following code snippet, and check if it works for you.
services.AddControllersWithViews().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
Besides, you can find differences between Newtonsoft.Json and System.Text.Json from here: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#table-of-differences-between-newtonsoftjson-and-systemtextjson
来源:https://stackoverflow.com/questions/60477141/signinmanagertuser-passwordsigninasync-is-throwing-system-text-json-object-c