Using the CCR with ASynchronous WCF Service

痴心易碎 提交于 2019-12-03 09:03:10

OK i finally got somewhere with all of this!

First up: You need a custom AsyncResult Class

class AsyncResult : IAsyncResult , IDisposable
    {
        object _state;
        ManualResetEvent _waitHandle = new ManualResetEvent(false);
        bool _isCompleted;

        #region IAsyncResult Members
        public object AsyncState
        {
            get { return _state; }
        }

        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get { return _waitHandle; }
        }

        public bool CompletedSynchronously
        {
            get { return false; }
        }

        public bool IsCompleted
        {
            get { return _isCompleted; }
        }
        #endregion

        Exception _exception;
        internal Exception Exception
        {
            get { return _exception; }
        }

        Accounts.Account _result;
        internal Accounts.Account Result
        {
            get { return _result; }
        }

        internal AsyncResult(PortSet<Accounts.Account, Exception> port, DispatcherQueue queue, AsyncCallback callback, object state)
        {
            _state = state;

            Arbiter.Activate(queue,
                Arbiter.Choice(port,
                    r =>
                    {
                        _result = r;
                        Complete(callback);
                    },
                    e =>
                    {
                        _exception = e;
                        Complete(callback);
                    }
                )
            );
        }

        private void Complete(AsyncCallback callback)
        {
            _isCompleted = true;
            _waitHandle.Set();

            if (callback != null)
            {
                ThreadPool.QueueUserWorkItem(s => callback(this));
            }
        }

        private bool disposedValue = false;

        public void Dispose()
        {
            if (!this.disposedValue)
            {
                _waitHandle.Close();
                _waitHandle = null;
                _state = null;
            }
            this.disposedValue = true;
        }
    }

Ok, then we need to tie this in with the Async WCF Method calls:

public class Service : IService
    {
        private Dispatcher dispatcher;
        private DispatcherQueue dq;

        public Service() 
        {
             dispatcher = new Dispatcher();
             dq = new DispatcherQueue("CCR DispatcherQueue", dispatcher);
        }

        public IAsyncResult BeginGetAccount(int id, AsyncCallback callback, object state)
        {
            PortSet<Accounts.Account, Exception> port = new PortSet<Accounts.Account, Exception>();
            Accounts.Manager manager = new Accounts.Manager();
            manager.GetAccountData(dq, port, id);

            AsyncResult result = new AsyncResult(port, dq, callback, state);
            return result;
        }

        public string EndGetAccount(IAsyncResult result)
        {
            {
                var AccountName = string.Empty;
                if ((result != null))
                {
                    using (Common.AsyncResult asyncResult = result as Common.AsyncResult)
                    {

                        if (asyncResult == null)
                        {
                            throw new NullReferenceException("IAsynchResult Parameter is Null");
                        }

                        asyncResult.AsyncWaitHandle.WaitOne();
                        if (asyncResult.Result != null) 
                        { 
                            AccountName = asyncResult.Result.Name; 
                        }
                    }
                }
                return AccountName;
            }
        }
    }

Then you just need the IEnumerator method to post the answer to the port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!