title: How can I do a loop foreach in asynchronous methode with signalR in .NET Core?`
body: <p>In my case I want to send the message to all sessions not only user (user can have many sessions), I used foreach to send message to all connections belong to friends with its others sessions,
  there is a problem in <code>await</code> asynchronous and a message:</p>
  <blockquote>
  <p>Failed writing message. Aborting connection.    System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOption</p>
  </blockquote>
  <pre><code>// Test if the user is offline :
  // Find My opened sessions and target opened sessions
  if (userInfoReciever != null)
  {
      var targetSessions = _context.Connections.Where(C =&gt; (C.Username == targetUserModel.UserName) &amp;&amp; (C.Connected == true)).ToList();
  
      if (targetSessions != null)
      { 
          foreach (var targetSeesion in targetSessions)
          {
              await Clients.Client(targetSeesion.ConnectionID).SendAsync(&quot;SendDM&quot;, messageModel, userInfoSender);
              await Clients.Client(targetSeesion.ConnectionID).SendAsync(&quot;SendDN&quot;, notificationModel, userInfoSender);
          }
      }
  }
  
  await Clients.Caller.SendAsync(&quot;MyMessage&quot;, messageModel, userInfoSender);
  
  if (userInfoSender != null)
  {
      var mySessions = _context.Connections.Where(C =&gt; ((C.ConnectionID != Context.ConnectionId) &amp;&amp; (C.Username == Context.User.Identity.Name) &amp;&amp; (C.Connected == true))).ToList();
  
      if (mySessions != null) 
      {
          foreach(var mySession in mySessions)
          {
              await Clients.Client(mySession.ConnectionID).SendAsync(&quot;MyMessage&quot;, messageModel, userInfoSender);
          }
      }
  }
  
  // Send my message to the the others sessions :
  
  // Build the message
  var _message = _mapper.Map&lt;MessageViewModel,Message&gt;(messageModel);
  var _notification = _mapper.Map&lt;NotificationViewModel, Notification&gt;(notificationModel);
  await _context.Messages.AddAsync(_message);
  await _context.Notifications.AddAsync(_notification);
  await _context.SaveChangesAsync();
  </code></pre>