我在WCF服務中的對象內傳遞列表時遇到問題。我有這個類:無法傳輸列表:底層連接已關閉
[DataContract]
public class Field
{
/*Necessary Information*/
[DataMember]
public int ID { get; set; }
[DataMember]
public String Picture { get; set; }
[DataMember]
public double TypicalAge { get; set; }
[DataMember]
public List<Report> ReportHistory { get; set; }
}
[DataContract]
public class Report
{
[DataMember]
public long ID { get; set; }
[DataMember]
public ReportTypes Type { get; set; }
[DataMember]
public int FieldID { get; set; }
[DataMember]
public String Head { get; set; }
[DataMember]
public String Body { get; set; }
[DataMember]
public DateTime TimeStamp { get; set; }
}
當報告列表爲空或1大小,一切正常。 當列表大小比1大,我得到這個錯誤:
The underlying connection was closed: The connection was closed unexpectedly..
在服務器中的代碼:
public List<Field> GetFieldsByIDs(List<int> listOfIDs)
{
var result = new List<Field>();
using (var context = new FieldBookEntities())
{
var fields = context.FieldEntities.Where(x => listOfIDs.Contains(x.ID)).Select(FieldEntityToField).ToList();
for (int i = 0; i < listOfIDs.Count; i++)
{
var field = fields.FirstOrDefault(x => x.ID == listOfIDs.ElementAt(i));
if (field != null)
result.Add(field);
}
}
return result;
}
在客戶端的代碼:
client = new FieldServiceClient();
var fieldsByIDs = client.GetFieldsByIDs(new int[] { 1, 2 });
界面:
public interface IFieldService
{
[OperationContract]
bool CheckConnection();
[OperationContract]
List<Field> GetFieldsByIDs(List<int> listOfIDs); //max 30 fields
}
我該怎麼辦?
顯示錯誤代碼。 – Ric
編輯我的問題 – BestArmy
爲什麼你傳遞一個'int []'而不是'List' –
Ric