问题:我有一个Web服务运行正常,我遇到的问题是我需要返回一个以上的类实例,并且不知道如何执行此操作。
我在服务中设置了一个循环:
If localtab.Rows.Count > 0 Then
Do While i <= localtab.Rows.Count
Mbr.Urn = localtab.Rows(i).Item(0) & vbNullString
Mbr.Title = localtab.Rows(i).Item(1) & vbNullString
Mbr.Initials = localtab.Rows(i).Item(2) & vbNullString
Mbr.Surname = localtab.Rows(i).Item(3) & vbNullString
Mbr.Address1 = localtab.Rows(i).Item(4) & vbNullString
Mbr.Address2 = localtab.Rows(i).Item(5) & vbNullString
Mbr.Address3 = localtab.Rows(i).Item(6) & vbNullString
Mbr.Town = localtab.Rows(i).Item(7) & vbNullString
Mbr.County = localtab.Rows(i).Item(8) & vbNullString
Mbr.Country = localtab.Rows(i).Item(9) & vbNullString
Mbr.Postcode = localtab.Rows(i).Item(10) & vbNullString
Mbr.msg = "Success"
i = i + 1
Loop
Else
Mbr.msg = "Fail - no record found"
End If
我知道这样可以正常工作,因为它会返回数据库中列出的最后一个成员。做Mbr(i).urn
之类的东西......不会像我循环通过i那样工作,它只会返回第一个实例,一旦服务一旦停止就返回。
以下是来自客户端调用服务的代码 - 很简单,我现在正在寻找的是一个带有每个姓氏的消息框。
Dim abMem As New ArdbegMember
Dim retMem As ArdbegMember
abMem.Downloaded = "N"
Try
cc.Open()
retMem = cc.MbrReq(abMem)
MesgBox(retMem.Surname)
cc.Close()
我需要知道的是如何将完整的记录集传递回客户端
修改
基于以下建议,我的代码现在看起来像这样 -
Dim results As List(Of ArdbegMember)
Dim i As Integer = 0
'assign values from the table to the ArdbegMember object
If localtab.Rows.Count > 0 Then
Do While i <= localtab.Rows.Count
Mbr.Urn = localtab.Rows(i).Item(0) & vbNullString
Mbr.Title = localtab.Rows(i).Item(1) & vbNullString
Mbr.Initials = localtab.Rows(i).Item(2) & vbNullString
Mbr.Surname = localtab.Rows(i).Item(3) & vbNullString
Mbr.Address1 = localtab.Rows(i).Item(4) & vbNullString
Mbr.Address2 = localtab.Rows(i).Item(5) & vbNullString
Mbr.Address3 = localtab.Rows(i).Item(6) & vbNullString
Mbr.Town = localtab.Rows(i).Item(7) & vbNullString
Mbr.County = localtab.Rows(i).Item(8) & vbNullString
Mbr.Country = localtab.Rows(i).Item(9) & vbNullString
Mbr.Postcode = localtab.Rows(i).Item(10) & vbNullString
Mbr.msg = "Success"
i = i + 1
results.Add(Mbr)
Loop
Else
Mbr.msg = "Fail - no record found"
End If
'Tidy up
dataAdapter.Dispose()
Cmd.Dispose()
oConn.Close()
'Return the ArdbegMember object
Return results
现在抛出错误 -
Value of type 'System.Collections.Generic.List(Of ArdbegWeb.ArdbegMember)' cannot be converted to 'ArdbegWeb.ArdbegMember'.
答案 0 :(得分:2)
您可以随时创建一个返回List<T>
的服务,前提是您为<T>
定义了具体类型:
[ServiceContract]
public interface IYourService
{
[OperationContract]
public List<ArdbegMember> GetMembers();
}
假设您有数据合同类:
[DataContract]
public class ArdbegMember
{
[DataMember]
public string Title { get; set; }
[DataMember]
public string Initials { get; set; }
[DataMember]
public string Surname { get; set; }
.... (and so forth - define all the properties you need on your `ArdbegMember`)
}
在您的服务实现中,您可以编写:
public class YourService : IYourService
{
public List<ArdbegMember> GetMembers()
{
List<ArdbegMember> results = new List<ArdbegMember>();
// load your `localtab` somehow
foreach(DataRow row in localtab.Rows)
{
ArdbegMember member = new ArdbegMember();
// set the properties, based on the "row"
results.Add(member);
}
return results;
}
}
所以现在你有了一个名为GetMembers()
的单一方法的WCF服务(你可能希望在你的真实服务中传递一些搜索条件等)并返回ArdbegMember
的列表对象。
一些好的WCF资源:
对书有兴趣吗?我会推荐Michele Leroux Bustamante的Learning WCF。她涵盖了所有必要的主题,并且以一种非常容易理解和平易近人的方式。这将教会您编写高质量,有用的WCF服务所需的一切 - 基础知识,中间主题,安全性,事务控制等等。
答案 1 :(得分:0)
感谢你的帮助marc,解决方案非常明显,我不应该错过它!
Do While i <= localtab.Rows.Count
应该已阅读Do While i <= localtab.Rows.Count-1
。当它试图将Mbr返回到客户端时,循环重复自身太多次导致问题!