Click here to Skip to main content
15,893,663 members
Articles / Programming Languages / C#

WS-Enumeration for WCF/WF

Rate me:
Please Sign up or sign in to vote.
4.86/5 (5 votes)
4 Feb 2011CPOL5 min read 25.9K   220   10  
In this article, I describe the design and implementation of a WS-Enumeration for WCF.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Transactions;

//
using WSEnumeration;
using WSEnumeration.Adapters;
using WSEnumeration.Descriptors;
using dc = WSEnumeration.DataContract;
using f = WSEnumeration.Faults;
using fs = WSEnumeration.Descriptors.FileSystem;

namespace TestApp
{
    public class Presenter
    {
        #region data
        IWSEnumeration _enumerator;
        DirListView _view;

        IList<fs.FileInfoDescriptor> _lst;
        IList<DataDisplayObject> _data;

        string _xpath = default(string);
        #endregion

        #region ctor
        public Presenter(IWSEnumeration theEnumerator, DirListView theView)
        {
            Init(theEnumerator, theView);
        }
        #endregion


        #region private
        void Init(IWSEnumeration theEnumerator, DirListView theView)
        {
            _enumerator = theEnumerator;
            _view = theView;

            _data = GetDirs();
            _view.SetPresenter(this);
            _view.Init();

            LLUpdateDesired();
        }
        IList<DataDisplayObject> GetDirs()
        {
            IList<DataDisplayObject> lst = new List<DataDisplayObject>();

            using (TransactionScope txscope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromSeconds(35)))
            {
                try
                {
                    var ctx = _enumerator.Enumerate(new EnumerateRequest() { Enumerate = new dc.Enumerate() { Expires = "1M" } });
                    var res = _enumerator.Pull(new PullRequest(new dc.Pull() { EnumerationContext = ctx.Response.EnumerationContext, MaxElements = 100 }));

                    //ItemsDescriptors should implement IEnumerable
                    ItemDescriptorCollection<fs.FileInfoDescriptor> dscrs = res.Response.Items as ItemDescriptorCollection<fs.FileInfoDescriptor>;

                    if (dscrs != null)
                        foreach (var item in dscrs)
                        {
                            DataDisplayObject data = new DataDisplayObject(item);
                            lst.Add(data);
                        }
                }
                catch (FaultException ex)
                {
                    return null;
                }
                txscope.Complete();
            }

            return lst;
        }
        #endregion

        #region low level
        void LLUpdateDesired()
        {
            _view.ResetData(_xpath, _data);
        }
        #endregion

        #region public
        public void DrillDownDesired(int iRow) 
        {
            IList<DataDisplayObject> lst = new List<DataDisplayObject>();

            if (_data[iRow].Inner.Type == WSEnumeration.Descriptors.FileSystem.ItemType.File)
                return;

            using (TransactionScope txscope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromSeconds(35)))
            {
                try
                {
                    dc.Enumerate enumRequestBody = new dc.Enumerate();
                    enumRequestBody.Expires = "1M";
                    enumRequestBody.Dialect = "XPath";
                    _xpath = enumRequestBody.Filter = _data[iRow].Inner.Root;

                    var ctx = _enumerator.Enumerate(new EnumerateRequest(enumRequestBody));

                    dc.Pull pullRequestBody = new dc.Pull();
                    pullRequestBody.MaxElements = 100;
                    pullRequestBody.EnumerationContext = ctx.Response.EnumerationContext;

                    var res = _enumerator.Pull(new PullRequest(pullRequestBody));

                    ItemDescriptorCollection<fs.FileInfoDescriptor> dscrs = res.Response.Items as ItemDescriptorCollection<fs.FileInfoDescriptor>;

                    if (dscrs != null)
                    {
                        foreach (var item in dscrs)
                        {
                            DataDisplayObject data = new DataDisplayObject(item);
                            lst.Add(data);
                        }
                    }

                }
                catch (FaultException ex)
                {

                }
                finally
                {
                    txscope.Complete();
                }
            }

            //_lst = GetDirs(key);

           // LLUpdateDesired();

            _data = lst;
            LLUpdateDesired();
        }
        public void UpDesired() 
        {

            if (String.IsNullOrEmpty(_xpath))
                return;

            IList<DataDisplayObject> lst = new List<DataDisplayObject>();

            using (TransactionScope txscope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromSeconds(35)))
            {
                try
                {
                    dc.Enumerate enumRequestBody = new dc.Enumerate();
                    enumRequestBody.Expires = "1M";
                    enumRequestBody.Dialect = "XPath";

                    string[] parts = _xpath.Split('/');
                    string filter = String.Empty;

                    for (int i = 0; i < parts.Length; i++)
                    {
                        if (i == parts.Length - 1)
                            continue;

                        filter = filter + "/" + parts[i];
                    }
                    _xpath = enumRequestBody.Filter = filter.Trim('/');

                    var ctx = _enumerator.Enumerate(new EnumerateRequest(enumRequestBody));

                    dc.Pull pullRequestBody = new dc.Pull();
                    pullRequestBody.MaxElements = 100;
                    pullRequestBody.EnumerationContext = ctx.Response.EnumerationContext;

                    var res = _enumerator.Pull(new PullRequest(pullRequestBody));

                    ItemDescriptorCollection<fs.FileInfoDescriptor> dscrs = res.Response.Items as ItemDescriptorCollection<fs.FileInfoDescriptor>;

                    if (dscrs != null)
                    {
                        foreach (var item in dscrs)
                        {
                            DataDisplayObject data = new DataDisplayObject(item);
                            lst.Add(data);
                        }
                    }

                }
                catch (FaultException ex)
                {

                }
                finally
                {
                    txscope.Complete();
                }
            }

            _data = lst;
            LLUpdateDesired();
        }
        #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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
I have Master degree in Particle Physics. During my last several years I work as software developer.

Primary Interests
- c#, c++, php, java.
- scientific programming

Comments and Discussions