GetSynchronizedContextFor() public method

Gets the reference to the synchronized context requested by the method.
public GetSynchronizedContextFor ( MethodInfo methodInfo ) : SynchronizeContextReference
methodInfo System.Reflection.MethodInfo The method.
return SynchronizeContextReference
Esempio n. 1
0
        private bool InvokeInSynchronizationContext(IInvocation invocation)
        {
            if (metaInfo == null)
            {
                return(false);
            }

            SynchronizationContext syncContext     = null;
            SynchronizationContext prevSyncContext = null;
            var methodInfo     = invocation.MethodInvocationTarget;
            var syncContextRef = metaInfo.GetSynchronizedContextFor(methodInfo);

            if (syncContextRef != null)
            {
                syncContext     = syncContextRef.Resolve(kernel, CreationContext.CreateEmpty());
                prevSyncContext = SynchronizationContext.Current;
            }
            else
            {
                syncContext = SynchronizationContext.Current;
            }

            if (syncContext != activeSyncContext)
            {
                try
                {
                    var result = CreateResult(invocation);

                    if (prevSyncContext != null)
                    {
                        SynchronizationContext.SetSynchronizationContext(syncContext);
                    }

                    if (syncContext.GetType() == typeof(SynchronizationContext))
                    {
                        InvokeSynchronously(invocation, result);
                    }
                    else
                    {
                        syncContext.Send(state =>
                        {
                            activeSyncContext = syncContext;
                            try
                            {
                                InvokeSafely(invocation, result);
                            }
                            finally
                            {
                                activeSyncContext = null;
                            }
                        }, null);
                    }
                }
                finally
                {
                    if (prevSyncContext != null)
                    {
                        SynchronizationContext.SetSynchronizationContext(prevSyncContext);
                    }
                }
            }
            else
            {
                InvokeSynchronously(invocation, null);
            }

            return(true);
        }
        private bool InvokeInSynchronizationContext(IInvocation invocation)
        {
            if (metaInfo != null)
            {
                IHandler handler = null;
                SynchronizationContext syncContext     = null;
                SynchronizationContext prevSyncContext = null;
                var methodInfo     = invocation.MethodInvocationTarget;
                var syncContextRef = metaInfo.GetSynchronizedContextFor(methodInfo);

                if (syncContextRef != null)
                {
                    switch (syncContextRef.ReferenceType)
                    {
                    case SynchronizeContextReferenceType.Key:
                        handler = kernel.GetHandler(syncContextRef.ComponentKey);
                        break;

                    case SynchronizeContextReferenceType.Interface:
                        handler = kernel.GetHandler(syncContextRef.ServiceType);
                        break;
                    }

                    if (handler == null)
                    {
                        throw new ApplicationException("The synchronization context could not be resolved.  Did you forget to register it in the container?");
                    }

                    syncContext = handler.Resolve(CreationContext.CreateEmpty()) as SynchronizationContext;

                    if (syncContext == null)
                    {
                        throw new ApplicationException(string.Format("{0} does not implement {1}",
                                                                     syncContextRef, typeof(SynchronizationContext).FullName));
                    }

                    prevSyncContext = SynchronizationContext.Current;
                }
                else
                {
                    syncContext = SynchronizationContext.Current;
                }

                if (syncContext != activeSyncContext)
                {
                    try
                    {
                        var result = CreateResult(invocation);

                        if (prevSyncContext != null)
                        {
                            SynchronizationContext.SetSynchronizationContext(syncContext);
                        }

                        if (syncContext.GetType() == typeof(SynchronizationContext))
                        {
                            InvokeSynchronously(invocation, result);
                        }
                        else
                        {
                            syncContext.Send(state =>
                            {
                                activeSyncContext = syncContext;
                                try
                                {
                                    InvokeSafely(invocation, result);
                                }
                                finally
                                {
                                    activeSyncContext = null;
                                }
                            }, null);
                        }
                    }
                    finally
                    {
                        if (prevSyncContext != null)
                        {
                            SynchronizationContext.SetSynchronizationContext(prevSyncContext);
                        }
                    }
                }
                else
                {
                    InvokeSynchronously(invocation, null);
                }

                return(true);
            }

            return(false);
        }