快速上手篇之WCF自定義集合
學習WCF時,你可能會遇到WCF自定義集合問題,這里將介紹WCF自定義集合問題的解決方法,在這里拿出來和大家分享一下。對于一個好的分布式系統來講,設計時應當考慮到異構性、開放性、安全性、可擴展性、故障處理、并發性以及透明性等問題。基于SOAP的Web Service可以實現異構環境的互操作性,保證了跨平臺的通信。
#T#利用WSE(Web Service Enhancements)可以為ASMX提供安全性的保證。.NET Remoting具有豐富的擴展功能,可以創建定制的信道、格式化器和代理程序。Enterprise Service(COM+)提供了對事務的支持,其中還包括分布式事務,可實現故障的恢復。MSMQ可以支持異步調用、脫機連接、斷點連接等功能,利用消息隊列支持應用程序之間的消息傳遞。從功能角度來看,WCF整合了ASMX、.Net Remoting、Enterprise Service、WSE以及MSMQ等現有技術的優點,它提供了一種構建安全可靠的分布式面向服務系統的統一的框架模型,使軟件研發人員在開發分布式應用時變得更加輕松。
集合元素類的定義如下:
- public enum FileType
- {
- TXT,DOC,HTML,OTHER
- }
- [DataContract]
- public class Document
- {
- private string m_localPath;
- private string m_fileName;
- private FileType m_fileType;
- [DataMember]
- public string LocalPath
- {
- get { return m_localPath; }
- set { m_localPath = value; }
- }
- [DataMember]
- public string FileName
- {
- get { return m_fileName; }
- set { m_fileName = value; }
- }
- [DataMember]
- public FileType FileType
- {
- get { return m_fileType; }
- set { m_fileType = value; }
- }
- }
WCF自定義集合DocumentList則實現了IList接口:
- //which attribute should be applied here?
- public class DocumentList:IList
- {
- private IList m_list = null;
- public DocumentList()
- {
- m_list = new List();
- }
- #region IList Members
- public int IndexOf(Document item)
- {
- return m_list.IndexOf(item);
- }
- public void Insert(int index, Document item)
- {
- m_list.Insert(index,item);
- }
- public void RemoveAt(int index)
- {
- m_list.RemoveAt(index);
- }
- public Document this[int index]
- {
- get
- {
- return m_list[index];
- }
- set
- {
- m_list[index] = value;
- }
- }
- #endregion
- #region ICollection Members
- public void Add(Document item)
- {
- m_list.Add(item);
- }
- public void Clear()
- {
- m_list.Clear();
- }
- public bool Contains(Document item)
- {
- return m_list.Contains(item);
- }
- public void CopyTo(Document[] array, int arrayIndex)
- {
- m_list.CopyTo(array,arrayIndex);
- }
- public int Count
- {
- get { return m_list.Count; }
- }
- public bool IsReadOnly
- {
- get { return m_list.IsReadOnly; }
- }
- public bool Remove(Document item)
- {
- return m_list.Remove(item);
- }
- #endregion
- #region IEnumerable Members
- public IEnumerator GetEnumerator()
- {
- return m_list.GetEnumerator();
- }
- #endregion
- #region IEnumerable Members
- IEnumerator IEnumerable.GetEnumerator()
- {
- return ((IEnumerable)m_list).GetEnumerator();
- }
- #endregion
- }