ActionMessageFilterTable.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Dispatcher / ActionMessageFilterTable.cs / 1 / ActionMessageFilterTable.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{ 
    using System;
    using System.ServiceModel.Channels; 
    using System.Collections; 
    using System.Collections.Generic;
    using System.Collections.ObjectModel; 
    using System.ServiceModel.Diagnostics;
    using System.Runtime.Serialization;

    [DataContract] 
    internal class ActionMessageFilterTable : IMessageFilterTable
    { 
        Dictionary filters; 
        Dictionary> actions;
        List always; 

        public ActionMessageFilterTable()
        {
            Init(); 
        }
 
        void Init() 
        {
            this.filters = new Dictionary(); 
            this.actions = new Dictionary>();
            this.always = new List();
        }
 
        public TFilterData this[MessageFilter filter]
        { 
            get 
            {
                return this.filters[filter]; 
            }
            set
            {
                if(this.filters.ContainsKey(filter)) 
                {
                    this.filters[filter] = value; 
                } 
                else
                { 
                    this.Add(filter, value);
                }
            }
        } 

        public int Count 
        { 
            get
            { 
                return this.filters.Count;
            }
        }
 
        [DataMember(IsRequired = true)]
        Entry[] Entries 
        { 
            get
            { 
                Entry[] entries = new Entry[Count];
                int i = 0;
                foreach (KeyValuePair item in filters)
                    entries[i++] = new Entry(item.Key, item.Value); 

                return entries; 
            } 
            set
            { 
                Init();

                for(int i = 0; i < value.Length; ++i)
                    Add(value[i].filter, value[i].data); 
            }
        } 
 
        public bool IsReadOnly
        { 
            get
            {
                return false;
            } 
        }
 
        public ICollection Keys 
        {
            get 
            {
                return this.filters.Keys;
            }
        } 

        public ICollection Values 
        { 
            get
            { 
                return this.filters.Values;
            }
        }
 
        public void Add(ActionMessageFilter filter, TFilterData data)
        { 
            if(filter == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("filter"); 
            }

            this.filters.Add(filter, data);
            List filters; 
#pragma warning suppress 56506 // [....], Actions will never be null
            if(filter.Actions.Count == 0) 
            { 
                this.always.Add(filter);
            } 
            else
            {
                for(int i = 0; i < filter.Actions.Count; ++i)
                { 
                    if(!this.actions.TryGetValue(filter.Actions[i], out filters))
                    { 
                        filters = new List(); 
                        this.actions.Add(filter.Actions[i], filters);
                    } 
                    filters.Add(filter);
                }
            }
        } 

        public void Add(MessageFilter filter, TFilterData data) 
        { 
            if(filter == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("filter");
            }

            Add((ActionMessageFilter)filter, data); 
        }
 
        public void Add(KeyValuePair item) 
        {
            Add(item.Key, item.Value); 
        }

        public void Clear()
        { 
            this.filters.Clear();
            this.actions.Clear(); 
            this.always.Clear(); 
        }
 
        public bool Contains(KeyValuePair item)
        {
            return ((ICollection>)this.filters).Contains(item);
        } 

        public bool ContainsKey(MessageFilter filter) 
        { 
            return this.filters.ContainsKey(filter);
        } 

        public void CopyTo(KeyValuePair[] array, int arrayIndex)
        {
            ((ICollection>)this.filters).CopyTo(array, arrayIndex); 
        }
 
        IEnumerator IEnumerable.GetEnumerator() 
        {
            return this.GetEnumerator(); 
        }

        public IEnumerator> GetEnumerator()
        { 
            return ((ICollection>)this.filters).GetEnumerator();
        } 
 
        MessageFilter InnerMatch(Message message)
        { 
            string act = message.Headers.Action;
            if(act == null)
            {
                act = string.Empty; 
            }
 
            List filters; 
            if(this.actions.TryGetValue(act, out filters))
            { 
                if(this.always.Count + filters.Count > 1)
                {
                    List tmp = new List(filters);
                    tmp.AddRange(this.always); 
                    Collection matches = new Collection(tmp);
                    throw TraceUtility.ThrowHelperError(new MultipleFilterMatchesException(SR.GetString(SR.FilterMultipleMatches), null, matches), message); 
                } 
                return filters[0];
            } 

            if(this.always.Count > 1)
            {
                Collection matches = new Collection(new List(this.always)); 
                throw TraceUtility.ThrowHelperError(new MultipleFilterMatchesException(SR.GetString(SR.FilterMultipleMatches), null, matches), message);
            } 
            else if(this.always.Count == 1) 
            {
                return this.always[0]; 
            }

            return null;
        } 

        void InnerMatch(Message message, ICollection results) 
        { 
            for(int i = 0; i < this.always.Count; ++i)
            { 
                results.Add(this.always[i]);
            }

            string act = message.Headers.Action; 
            if(act == null)
            { 
                act = string.Empty; 
            }
 
            List filters;
            if(this.actions.TryGetValue(act, out filters))
            {
                for(int i = 0; i < filters.Count; ++i) 
                {
                    results.Add(filters[i]); 
                } 
            }
        } 

        void InnerMatchData(Message message, ICollection results)
        {
            for(int i = 0; i < this.always.Count; ++i) 
            {
                results.Add(this.filters[this.always[i]]); 
            } 

            string act = message.Headers.Action; 
            if(act == null)
            {
                act = string.Empty;
            } 

            List filters; 
            if(this.actions.TryGetValue(act, out filters)) 
            {
                for(int i = 0; i < filters.Count; ++i) 
                {
                    results.Add(this.filters[filters[i]]);
                }
            } 
        }
 
        public bool GetMatchingValue(Message message, out TFilterData data) 
        {
            if (message == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
            }
 
            MessageFilter f = InnerMatch(message);
            if(f == null) 
            { 
                data = default(TFilterData);
                return false; 
            }

            data = this.filters[f];
            return true; 
        }
 
        public bool GetMatchingValue(MessageBuffer messageBuffer, out TFilterData data) 
        {
            if (messageBuffer == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageBuffer");
            }
 
            MessageFilter f = null;
            Message msg = messageBuffer.CreateMessage(); 
            try 
            {
                f = InnerMatch(msg); 
            }
            finally
            {
                msg.Close(); 
            }
 
            if(f == null) 
            {
                data = default(TFilterData); 
                return false;
            }

            data = this.filters[f]; 
            return true;
        } 
 
        public bool GetMatchingFilter(Message message, out MessageFilter filter)
        { 
            if (message == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
            } 

            filter = InnerMatch(message); 
            return filter != null; 
        }
 
        public bool GetMatchingFilter(MessageBuffer messageBuffer, out MessageFilter filter)
        {
            if (messageBuffer == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageBuffer");
            } 
 
            Message msg = messageBuffer.CreateMessage();
            try 
            {
                filter = InnerMatch(msg);
                return filter != null;
            } 
            finally
            { 
                msg.Close(); 
            }
        } 

        public bool GetMatchingFilters(Message message, ICollection results)
        {
            if (message == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message"); 
            } 

            if (results == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("results");
            }
 
            int count = results.Count;
            InnerMatch(message, results); 
            return count != results.Count; 
        }
 
        public bool GetMatchingFilters(MessageBuffer messageBuffer, ICollection results)
        {
            if (messageBuffer == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageBuffer");
            } 
 
            if (results == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("results");
            }

            Message msg = messageBuffer.CreateMessage(); 
            try
            { 
                int count = results.Count; 
                InnerMatch(msg, results);
                return count != results.Count; 
            }
            finally
            {
                msg.Close(); 
            }
        } 
 
        public bool GetMatchingValues(Message message, ICollection results)
        { 
            if (message == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
            } 

            if (results == null) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("results");
            } 

            int count = results.Count;
            InnerMatchData(message, results);
            return count != results.Count; 
        }
 
        public bool GetMatchingValues(MessageBuffer messageBuffer, ICollection results) 
        {
            if (messageBuffer == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageBuffer");
            }
 
            if (results == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("results"); 
            }
 
            Message msg = messageBuffer.CreateMessage();
            try
            {
                int count = results.Count; 
                InnerMatchData(msg, results);
                return count != results.Count; 
            } 
            finally
            { 
                msg.Close();
            }
        }
 
        public bool Remove(ActionMessageFilter filter)
        { 
            if(filter == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("filter"); 
            }

            if(this.filters.Remove(filter))
            { 
#pragma warning suppress 56506 // [....], ActionMessageFilter.Actions can never be null
                if(filter.Actions.Count == 0) 
                { 
                    this.always.Remove(filter);
                } 
                else
                {
                    List filters;
                    for(int i = 0; i < filter.Actions.Count; ++i) 
                    {
#pragma warning suppress 56506 // [....], PreSharp generates a false error here 
                        filters = this.actions[filter.Actions[i]]; 
#pragma warning suppress 56506 // [....], filters can never be null
                        if(filters.Count == 1) 
                        {
                            this.actions.Remove(filter.Actions[i]);
                        }
                        else 
                        {
                            filters.Remove(filter); 
                        } 
                    }
                } 
                return true;
            }
            return false;
        } 

        public bool Remove(MessageFilter filter) 
        { 
            if(filter == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("filter");
            }

            ActionMessageFilter aFilter = filter as ActionMessageFilter; 
            if(aFilter != null)
            { 
                return Remove(aFilter); 
            }
            return false; 
        }

        public bool Remove(KeyValuePair item)
        { 
            if(((ICollection>)this.filters).Contains(item))
            { 
                return Remove(item.Key); 
            }
            return false; 
        }

        public bool TryGetValue(MessageFilter filter, out TFilterData data)
        { 
            return this.filters.TryGetValue(filter, out data);
        } 
 
        [DataContract]
        class Entry 
        {
            [DataMember(IsRequired = true)]
            internal MessageFilter filter;
 
            [DataMember(IsRequired = true)]
            internal TFilterData data; 
 
            internal Entry(MessageFilter f, TFilterData d)
            { 
                filter = f;
                data = d;
            }
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK