1
votes

Issue

Scenario:

  • SignalR version 1.0.0 RC1 Final
  • DotNet Core 2.0
  • Tipical: User - Posts - Comments models.

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?


Some unnecesary code details

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);

    // ...
}
1
Referential loop? - aaron
@aaron, has sense. But, how to avoid it? How to trace this error? - dani herrera
Entities are usually mapped to DTOs before sending to a client. - aaron
@aaron, It has sense, also DTO name looks cool. I will accept this approach as a solution but wait please. If someone post how to trace websockets errors, then the answer may be can match better with the first part of the question: How can I diagnose what is happens? Someone knows what is the reason? Any case, thanks and good shot. - dani herrera
You can enable developer exception page and/or configure logging. - aaron

1 Answers

1
votes

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