ASP.NET Core: A second operation started on this context before a previous operation completed












0















I'm receiving the following error intermittently on one of my SignalR methods:




Error: An unexpected error occurred invoking 'AcceptQueuedUser' on the
server. InvalidOperationException: A second operation started on this
context before a previous operation completed. Any instance members
are not guaranteed to be thread safe.




Anytime I run into this error, the first thing I check is to make sure all my async method invocations are awaited. However, I've gone through my entire code base looking for async methods that are missing an await keyword. I've used regex to try and track it down as well as ReSharper's "Async method invocation without await expression" inspection on the entire solution. No dice.



I have a feeling this problem is related to SignalR, as I've never had this much trouble on normal API Endpoints/Controllers. I'm injecting my scoped DbContext into my SignalR hub as I would on any normal Controller:



 public class ChatQueueHub<TUser> : Hub<IChatQueueHubClient>
where TUser : class, IChatRoomUser
{
private readonly IChatQueueRepository _chatQueueRepository;
/// <summary>
/// Default constructor
/// </summary>
public ChatQueueHub(IChatQueueRepository chatQueueRepository)
{
_chatQueueRepository = chatQueueRepository
}

public async Task<ApiResponse<string>> AcceptQueuedUser(int userQueueId)
{
// ...
await _chatQueueRepository.RemoveQueuedUserAsync(userQueueUser);
// ...
return new ApiResponse<string>(newChatroom.Id, BaseApiCode.Success);
}
}


Here I'm injecting IChatQueueRepository which injects DbContext into itself and performs all context operations.



I have a feeling this is due to some weird threading issue on SignalR, but I can't see anything abnormal in my code. As far as I know SignalR instantiates a new Hub for every RPC sent over the wire.



What are some other things I can look for that would cause A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe. aside from non-awaited async invocations?



Edit:



I see now that the ASP.NET Core Dependancy injection documentation says the following:




Entity Framework contexts should be added to the service
container using the scoped lifetime. This is handled automatically
with a call to the AddDbContext method when registering the database
context. Services that use the database context should also use the
scoped lifetime.




SignalR Hubs are Transient, so perhaps this is causing the problem. What is the correct way to access a DBContext from a SignalR Hub?










share|improve this question





























    0















    I'm receiving the following error intermittently on one of my SignalR methods:




    Error: An unexpected error occurred invoking 'AcceptQueuedUser' on the
    server. InvalidOperationException: A second operation started on this
    context before a previous operation completed. Any instance members
    are not guaranteed to be thread safe.




    Anytime I run into this error, the first thing I check is to make sure all my async method invocations are awaited. However, I've gone through my entire code base looking for async methods that are missing an await keyword. I've used regex to try and track it down as well as ReSharper's "Async method invocation without await expression" inspection on the entire solution. No dice.



    I have a feeling this problem is related to SignalR, as I've never had this much trouble on normal API Endpoints/Controllers. I'm injecting my scoped DbContext into my SignalR hub as I would on any normal Controller:



     public class ChatQueueHub<TUser> : Hub<IChatQueueHubClient>
    where TUser : class, IChatRoomUser
    {
    private readonly IChatQueueRepository _chatQueueRepository;
    /// <summary>
    /// Default constructor
    /// </summary>
    public ChatQueueHub(IChatQueueRepository chatQueueRepository)
    {
    _chatQueueRepository = chatQueueRepository
    }

    public async Task<ApiResponse<string>> AcceptQueuedUser(int userQueueId)
    {
    // ...
    await _chatQueueRepository.RemoveQueuedUserAsync(userQueueUser);
    // ...
    return new ApiResponse<string>(newChatroom.Id, BaseApiCode.Success);
    }
    }


    Here I'm injecting IChatQueueRepository which injects DbContext into itself and performs all context operations.



    I have a feeling this is due to some weird threading issue on SignalR, but I can't see anything abnormal in my code. As far as I know SignalR instantiates a new Hub for every RPC sent over the wire.



    What are some other things I can look for that would cause A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe. aside from non-awaited async invocations?



    Edit:



    I see now that the ASP.NET Core Dependancy injection documentation says the following:




    Entity Framework contexts should be added to the service
    container using the scoped lifetime. This is handled automatically
    with a call to the AddDbContext method when registering the database
    context. Services that use the database context should also use the
    scoped lifetime.




    SignalR Hubs are Transient, so perhaps this is causing the problem. What is the correct way to access a DBContext from a SignalR Hub?










    share|improve this question



























      0












      0








      0








      I'm receiving the following error intermittently on one of my SignalR methods:




      Error: An unexpected error occurred invoking 'AcceptQueuedUser' on the
      server. InvalidOperationException: A second operation started on this
      context before a previous operation completed. Any instance members
      are not guaranteed to be thread safe.




      Anytime I run into this error, the first thing I check is to make sure all my async method invocations are awaited. However, I've gone through my entire code base looking for async methods that are missing an await keyword. I've used regex to try and track it down as well as ReSharper's "Async method invocation without await expression" inspection on the entire solution. No dice.



      I have a feeling this problem is related to SignalR, as I've never had this much trouble on normal API Endpoints/Controllers. I'm injecting my scoped DbContext into my SignalR hub as I would on any normal Controller:



       public class ChatQueueHub<TUser> : Hub<IChatQueueHubClient>
      where TUser : class, IChatRoomUser
      {
      private readonly IChatQueueRepository _chatQueueRepository;
      /// <summary>
      /// Default constructor
      /// </summary>
      public ChatQueueHub(IChatQueueRepository chatQueueRepository)
      {
      _chatQueueRepository = chatQueueRepository
      }

      public async Task<ApiResponse<string>> AcceptQueuedUser(int userQueueId)
      {
      // ...
      await _chatQueueRepository.RemoveQueuedUserAsync(userQueueUser);
      // ...
      return new ApiResponse<string>(newChatroom.Id, BaseApiCode.Success);
      }
      }


      Here I'm injecting IChatQueueRepository which injects DbContext into itself and performs all context operations.



      I have a feeling this is due to some weird threading issue on SignalR, but I can't see anything abnormal in my code. As far as I know SignalR instantiates a new Hub for every RPC sent over the wire.



      What are some other things I can look for that would cause A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe. aside from non-awaited async invocations?



      Edit:



      I see now that the ASP.NET Core Dependancy injection documentation says the following:




      Entity Framework contexts should be added to the service
      container using the scoped lifetime. This is handled automatically
      with a call to the AddDbContext method when registering the database
      context. Services that use the database context should also use the
      scoped lifetime.




      SignalR Hubs are Transient, so perhaps this is causing the problem. What is the correct way to access a DBContext from a SignalR Hub?










      share|improve this question
















      I'm receiving the following error intermittently on one of my SignalR methods:




      Error: An unexpected error occurred invoking 'AcceptQueuedUser' on the
      server. InvalidOperationException: A second operation started on this
      context before a previous operation completed. Any instance members
      are not guaranteed to be thread safe.




      Anytime I run into this error, the first thing I check is to make sure all my async method invocations are awaited. However, I've gone through my entire code base looking for async methods that are missing an await keyword. I've used regex to try and track it down as well as ReSharper's "Async method invocation without await expression" inspection on the entire solution. No dice.



      I have a feeling this problem is related to SignalR, as I've never had this much trouble on normal API Endpoints/Controllers. I'm injecting my scoped DbContext into my SignalR hub as I would on any normal Controller:



       public class ChatQueueHub<TUser> : Hub<IChatQueueHubClient>
      where TUser : class, IChatRoomUser
      {
      private readonly IChatQueueRepository _chatQueueRepository;
      /// <summary>
      /// Default constructor
      /// </summary>
      public ChatQueueHub(IChatQueueRepository chatQueueRepository)
      {
      _chatQueueRepository = chatQueueRepository
      }

      public async Task<ApiResponse<string>> AcceptQueuedUser(int userQueueId)
      {
      // ...
      await _chatQueueRepository.RemoveQueuedUserAsync(userQueueUser);
      // ...
      return new ApiResponse<string>(newChatroom.Id, BaseApiCode.Success);
      }
      }


      Here I'm injecting IChatQueueRepository which injects DbContext into itself and performs all context operations.



      I have a feeling this is due to some weird threading issue on SignalR, but I can't see anything abnormal in my code. As far as I know SignalR instantiates a new Hub for every RPC sent over the wire.



      What are some other things I can look for that would cause A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe. aside from non-awaited async invocations?



      Edit:



      I see now that the ASP.NET Core Dependancy injection documentation says the following:




      Entity Framework contexts should be added to the service
      container using the scoped lifetime. This is handled automatically
      with a call to the AddDbContext method when registering the database
      context. Services that use the database context should also use the
      scoped lifetime.




      SignalR Hubs are Transient, so perhaps this is causing the problem. What is the correct way to access a DBContext from a SignalR Hub?







      c# entity-framework asynchronous signalr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 17:03







      Brad

















      asked Nov 21 '18 at 16:52









      BradBrad

      3,256103955




      3,256103955
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416963%2fasp-net-core-a-second-operation-started-on-this-context-before-a-previous-opera%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416963%2fasp-net-core-a-second-operation-started-on-this-context-before-a-previous-opera%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()