static void RegisterRootReceiveContext(Activity activity, Guid workflowId)
        {
            if (activity == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("activity");
            }

            Activity contextActivity = activity.ContextActivity;
            if (contextActivity == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new InvalidOperationException(SR2.GetString(SR2.Error_ContextOwnerActivityMissing)));
            }

            Activity owner = contextActivity.RootActivity;
            if (owner == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new InvalidOperationException(SR2.GetString(SR2.Error_ContextOwnerActivityMissing)));
            }

            ReceiveContextCollection collection =
                owner.GetValue(ReceiveContextCollection.ReceiveContextCollectionProperty) as ReceiveContextCollection;
            if (collection == null)
            {
                collection = new ReceiveContextCollection();
                owner.SetValue(ReceiveContextCollection.ReceiveContextCollectionProperty, collection);
            }

            if (!collection.Contains(ContextToken.RootContextName))
            {
                collection.Add(new ReceiveContext(ContextToken.RootContextName, workflowId, true));
            }
        }
        static void RegisterReceiveContext(ReceiveActivity activity,
            Guid workflowId,
            string contextName,
            string ownerActivityName)
        {
            if (activity == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("activity");
            }
            if (string.IsNullOrEmpty(contextName))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("contextName",
                    SR2.GetString(SR2.Error_ArgumentValueNullOrEmptyString));
            }

            Activity contextActivity = activity.ContextActivity;
            Activity owner = null;

            if (contextActivity == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new InvalidOperationException(SR2.GetString(SR2.Error_ContextOwnerActivityMissing)));
            }

            if (string.IsNullOrEmpty(ownerActivityName))
            {
                owner = contextActivity.RootActivity;
            }
            else
            {
                while (contextActivity != null)
                {
                    owner = contextActivity.GetActivityByName(ownerActivityName, true);
                    if (owner != null)
                    {
                        break;
                    }

                    contextActivity = contextActivity.Parent;
                    if (contextActivity != null)
                    {
                        contextActivity = contextActivity.ContextActivity;
                    }
                }
            }

            if (owner == null)
            {
                owner = Helpers.ParseActivityForBind(activity, ownerActivityName);
            }

            if (owner == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new InvalidOperationException(SR2.GetString(SR2.Error_ContextOwnerActivityMissing)));
            }

            ReceiveContextCollection collection =
                owner.GetValue(ReceiveContextCollection.ReceiveContextCollectionProperty) as ReceiveContextCollection;
            if (collection == null)
            {
                collection = new ReceiveContextCollection();
                owner.SetValue(ReceiveContextCollection.ReceiveContextCollectionProperty, collection);
            }

            if (!collection.Contains(contextName))
            {
                collection.Add(new ReceiveContext(contextName, workflowId, false));
            }
        }