学习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; }
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
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
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.