TaskFriendlyHttpContextAccessor issues

Issue #10 new
Sergey Zubatkin created an issue

Hello Piotr! I am curious about fixing TaskFriendlyHttpContextAccessor issues. I do not know exact scenario which you fixed but I have another one. if I try to get CurrentContext in OnDisconnected hub method I get null. May be you need to change (_scope.Tag as string) == "work" to "shell" in TaskFriendlyHttpContextAccessor or make up another way of getting httpcontext in Task. We have the same problems in all hub method if transport is webSockets because in that case SignalR uses Tasks.

Comments (3)

  1. Piotr Szmyd

    Hi Sergey! Thanks for issue report. Websockets are kinda problematic because they do not follow Orchard unit-of-work pattern, ie. there is no request but a separate listener that runs in the background.

    I have an idea how to fix this but it's a bit complicated. Anyways - will figure it out and let you know.

  2. Sergey Zubatkin reporter

    Thank you for quick response! Until this problem is resolved we will use next wrapper in All hub methods, I think it is safe:

        public static void ExecuteWithEnsuredWorkContext(this IWorkContextAccessor wca, Action<WorkContext> action)
        {
            var wc = wca.GetContext();
            if (wc != null)//work context already exists
            {
                action(wc);
            }
            else//create new work context
            {
                using (var wcScope = wca.CreateWorkContextScope())
                {
                    var transactionManager = wcScope.Resolve<ITransactionManager>();
                    transactionManager.RequireNew();
                    try
                    {
                        action(wcScope.WorkContext);
                    }
                    catch (Exception)
                    {
                        transactionManager.Cancel();
                        throw;
                    }
                }
            }
        }
    
  3. Log in to comment