wcf 将一个类的多个实例返回给客户端
问题:我有一个 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'.
The problem: I have a web service up and running ok, the problem I have is that i need to return more than one instance of a class and have no real idea how to do this.
I have a loop set up in the service:
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
And i know that this works fine as it returns the last member listed in the DB. Doing something like Mbr(i).urn
etc... won't work as when I loop through i, it is only ever going to return the first instance, and once the service has returned once it will stop.
Below is the code from the client end calling the service - simple enough, all I am looking for just now is for a message box with each surname to be displayed.
Dim abMem As New ArdbegMember
Dim retMem As ArdbegMember
abMem.Downloaded = "N"
Try
cc.Open()
retMem = cc.MbrReq(abMem)
MesgBox(retMem.Surname)
cc.Close()
What I need to know is how to pass a full recordset back to the client
EDIT
So based on the suggestions below, my code now looks like this -
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
And the error being thrown is now -
Value of type 'System.Collections.Generic.List(Of ArdbegWeb.ArdbegMember)' cannot be converted to 'ArdbegWeb.ArdbegMember'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终可以创建一个返回
List
的服务,前提是您为
定义了具体类型:假设您有一个数据协定类:
在您的服务中实现,你可以这样写:
现在你有了一个 WCF 服务,它有一个名为
GetMembers()
的方法(你可能想在你的实际服务中传递一些搜索条件等)并返回返回ArdbegMember
对象列表。一些不错的 WCF 资源:
对一本书感兴趣?我会推荐 Michele Leroux Bustamante 的学习 WCF。她以一种非常容易理解和平易近人的方式涵盖了所有必要的主题。这将教您编写高质量、有用的 WCF 服务所需的一切知识 - 基础知识、中级主题、安全性、事务控制等等。
You can always create a service that returns a
List<T>
given you define a concrete type for<T>
:Assuming you have a data contract class:
In your service implementation, you could then write:
So now you have a WCF service with a single method called
GetMembers()
(you probably want to pass in some search criteria, etc. - in your real service) and returns back a list ofArdbegMember
objects.Some good WCF resources:
Interested in a book? I would recommend Learning WCF by Michele Leroux Bustamante. She covers all the necessary topics, and in a very understandable and approachable way. This will teach you everything - basics, intermediate topics, security, transaction control and so forth - that you need to know to write high quality, useful WCF services.
感谢您的帮助,马克,解决方案非常明显,我不应该错过它!
Do While i <= localtab.Rows.Count
应该阅读Do While i <= localtab.Rows.Count-1
。当它尝试将 Mbr 返回到客户端时,循环本身重复太多次,导致出现问题!Thanks for your help marc, the solution was blindingly obvious and i shouldn't have missed it!
Do While i <= localtab.Rows.Count
should have readDo While i <= localtab.Rows.Count-1
. The loop was repeating on itself one too many times causing issues when it was trying to return Mbr to the client side!