Scenario:
Issue:
When I send a model through websocket fails silently (no message is send to clients). It works fine if I set to null navigation property:
_context.Posts.Add(model);
_context.SaveChanges();
model.User.Posts = null; // <- removing this line fails silently
_hub.Clients.All.SendAsync("AddAsync", model);
How can I diagnose what is happens? Someone knows what is the reason?
User.cs
public Guid Id { get; set; }
// ...
public virtual List<Post> Posts { get; set; } = new List<Post>();
Post.cs
public Guid Id { get; set; }
// ...
public virtual User User { get; set; }
PostsController.cs
private IHubContext<PostsHub> _hub;
private DatabaseContext _context;
public PostsController(IHubContext<PostsHub> hub)
{
_hub = hub;
}
// ...
[HttpPost]
public async Task<ActionResult> PostAsync([FromBody] Post model)
{
// ...
_context.Posts.Add(model);
_context.SaveChanges();
model.User.Posts = null;
_hub.Clients.All.SendAsync("AddAsync", model);
// ...
}
With json convert Serialize the model to json and deserialize it again as Post object just before sending it. If there is an error it should popup there, because if there is a dependency loop json convert should throw exception