Click here to Skip to main content
15,893,722 members
Articles / Programming Languages / XML

NullTransport for WCF

Rate me:
Please Sign up or sign in to vote.
4.91/5 (48 votes)
1 Oct 200712 min read 189.8K   1.7K   121  
This article describes design, implementation and the usage of the custom in-process transport for Microsoft Windows Communication Foundation (WCF) model.
//*****************************************************************************
//    Description.....Null Transport 
//                                
//    Author..........Roman Kiss, rkiss@pathcom.com
//    Copyright © 2007 ATZ Consulting Inc.  (see included license.rtf file)     
//                        
//    Date Created:    07/07/07
//
//    Date        Modified By     Description
//-----------------------------------------------------------------------------
//    07/07/07    Roman Kiss     Initial Revision
//*****************************************************************************
//  
#region Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
#endregion


namespace RKiss.NullChannelLib
{
    public class NullInputQueueChannelBase<T> : ChannelBase where T : class
    {
        #region Private members
        EndpointAddress localAddress;
        InputQueue<T> messageQueue;
        #endregion

        #region Constructors
        public NullInputQueueChannelBase(ChannelListenerBase factory, EndpointAddress localAddress)
            : base(factory)
        {
            this.localAddress = localAddress;
            this.messageQueue = new InputQueue<T>();
        }
        #endregion

        #region LocalAddress
        public EndpointAddress LocalAddress
        {
            get { return this.localAddress; }
        }
        #endregion

        #region Queuing
        public int PendingMessageCount
        {
            get { return this.messageQueue.PendingCount; }
        }
        public int NumberOfReaders
        {
            get { return this.messageQueue.NumberOfReaders; }
        }

        public void Dispatch(T request)
        {
            base.ThrowIfDisposedOrNotOpen();
            this.messageQueue.EnqueueAndDispatch(request);
        }

        public IAsyncResult BeginDequeue(TimeSpan timeout, AsyncCallback callback, object state)
        {
            //Console.WriteLine("BeginDequeue");
            return  (base.State == CommunicationState.Opened) ?
                this.messageQueue.BeginDequeue(timeout, callback, state) :
                new CompletedAsyncResult(callback, state);
        }
        public T EndDequeue(IAsyncResult result)
        {
            base.ThrowIfDisposedOrNotOpen();
            return this.messageQueue.EndDequeue(result);
        }
        public T Dequeue(TimeSpan timeout)
        {
            // base.ThrowIfDisposedOrNotOpen();
            return this.messageQueue.Dequeue(timeout);
        }

        public bool TryDequeue(IAsyncResult result, out T message)
        {
            message = null;
            TypedAsyncResult<T> completedResult = result as TypedAsyncResult<T>;
            if (completedResult != null)
            {
                message = TypedAsyncResult<T>.End(result);
            }
            else if (result.CompletedSynchronously == false)
            {
                InputQueue<T>.AsyncQueueReader completedResult2 = result as InputQueue<T>.AsyncQueueReader;
                InputQueue<T>.AsyncQueueReader.End(result, out message);
            }
            return result.IsCompleted;
        }
        #endregion

        #region Abort
        protected override void OnAbort()
        {
            this.messageQueue.Close();
        }
        #endregion

        #region Open
        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        {
            this.OnOpen(timeout);
            return new CompletedAsyncResult(callback, state);
        }
        protected override void OnOpen(TimeSpan timeout)
        {
            this.messageQueue.Open();
        }
        protected override void OnEndOpen(IAsyncResult result)
        {
            CompletedAsyncResult.End(result);
        }
        #endregion

        #region Close
        protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        {
            this.OnClose(timeout);
            return new CompletedAsyncResult(callback, state);
        }
        protected override void OnClose(TimeSpan timeout)
        {
            this.messageQueue.Close();
        }
        protected override void OnEndClose(IAsyncResult result)
        {
            CompletedAsyncResult.End(result);
        }
        #endregion      

        #region GetProperty
        public override P GetProperty<P>()
        {
            if (typeof(P) == typeof(FaultConverter))
            {
                return FaultConverter.GetDefaultFaultConverter(MessageVersion.Soap12WSAddressing10) as P;
            }
            return base.GetProperty<P>();
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions