Exemplo n.º 1
0
        private Collection <MultiPartMessage> Execute(MultiPartMessage msg, int componentIndex)
        {
            if (msg == null)
            {
                throw new ArgumentNullException("msg");
            }

            PipelineComponent component = instanceManagerCollection[componentIndex].GetInstance(msg);

            try
            {
                if (component.Supports(msg))
                {
                    if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Verbose) == SourceLevels.Verbose)
                    {
                        MessageEngine.Instance.Tracing.TraceEvent(TraceEventType.Verbose, 0, "Invoking pipeline component: \"{0}\"...", component.GetType().Name);
                    }

                    Collection <MultiPartMessage> componentResultCollection = component.Invoke(msg);

                    if (componentResultCollection != null)
                    {
                        foreach (MultiPartMessage resultMsg in componentResultCollection)
                        {
                            CopyMetadata(msg, resultMsg);
                        }

                        componentIndex++;

                        if (componentIndex < instanceManagerCollection.Count)
                        {
                            Collection <MultiPartMessage> pipelineResultCollection = new Collection <MultiPartMessage>();

                            foreach (MultiPartMessage componentResultMsg in componentResultCollection)
                            {
                                Collection <MultiPartMessage> executeResultCollection = Execute(componentResultMsg, componentIndex);

                                if (executeResultCollection != null)
                                {
                                    foreach (MultiPartMessage pipelineResultMsg in executeResultCollection)
                                    {
                                        pipelineResultCollection.Add(pipelineResultMsg);
                                    }
                                }
                            }

                            if (pipelineResultCollection.Count > 0)
                            {
                                return(pipelineResultCollection);
                            }
                            else
                            {
                                return(null);
                            }
                        }

                        return(componentResultCollection);
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                if (ExceptionHelper.IsCritical(ex))
                {
                    throw;
                }
                else
                {
                    try
                    {
                        if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Error) == SourceLevels.Error)
                        {
                            MessageEngine.Instance.Tracing.TraceData(TraceEventType.Error, 0, ex);
                        }

                        if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Error) == SourceLevels.Error)
                        {
                            MessageEngine.Instance.Tracing.TraceData(TraceEventType.Error, 0, string.Format("Message content:\n{0}", msg.ToXmlString()));
                        }
                    }
                    catch (Exception logEx)
                    {
                        if (ExceptionHelper.IsCritical(logEx))
                        {
                            throw;
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        internal PipelineComponent GetInstance(MultiPartMessage msg)
        {
            PipelineComponent component   = null;
            object            instanceKey = null;

            if (persistenceMode == PersistenceMode.Adapter)
            {
                if (pipelineType == PipelineType.Receive)
                {
                    instanceKey = msg.Metadata.Read("ReceiveAdapterId");
                }
                else
                {
                    instanceKey = msg.Metadata.Read("SendAdapterId");
                }
            }
            else if (persistenceMode == PersistenceMode.EndPoint)
            {
                if (pipelineType == PipelineType.Receive)
                {
                    instanceKey = msg.Metadata.Read("ReceiveUri");
                }
                else
                {
                    if (msg.Metadata.Contains("SendUri"))
                    {
                        instanceKey = msg.Metadata.Read("SendUri");
                    }
                    else
                    {
                        throw new PipelineException("Cannot create Component with EndPoint persistency when no EndPoint exists");
                    }
                }
            }

            syncLock.AcquireReaderLock(MessageEngine.Instance.LockMillisecondsTimeout);

            try
            {
                if (instanceDictionary.ContainsKey(instanceKey))
                {
                    component = instanceDictionary[instanceKey];
                }
            }
            finally
            {
                syncLock.ReleaseReaderLock();
            }

            if (component == null)
            {
                syncLock.AcquireWriterLock(MessageEngine.Instance.LockMillisecondsTimeout);

                try
                {
                    if (instanceDictionary.ContainsKey(instanceKey))
                    {
                        if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Verbose) == SourceLevels.Verbose)
                        {
                            MessageEngine.Instance.Tracing.TraceEvent(TraceEventType.Verbose, 0, "{0}: Instance already created by another thread.", componentType.Name);
                        }

                        component = instanceDictionary[instanceKey];
                    }
                    else
                    {
                        component = (PipelineComponent)componentConstructor.Invoke(new object[] { configuration });

                        instanceDictionary.Add(instanceKey, component);

                        if (persistenceMode == PersistenceMode.EndPoint)
                        {
                            if (endPointEventHandler == null)
                            {
                                endPointEventHandler = new EventHandler <AdapterEndPointEventArgs>(EndPointDestroyedEventHandler);

                                AdapterBase adapter = MessageEngine.Instance.AdapterProxy.ResolveUriToAdapter((Uri)instanceKey);
                                adapter.EndPointDestroyed += endPointEventHandler;
                            }
                        }

                        if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Verbose) == SourceLevels.Verbose)
                        {
                            MessageEngine.Instance.Tracing.TraceEvent(TraceEventType.Verbose, 0, "{0}: Instance created for: \"{1}\".", componentType.Name, instanceKey);
                        }
                    }
                }
                finally
                {
                    syncLock.ReleaseWriterLock();
                }
            }

            return(component);
        }