wcf return more than one instance of a class to client
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 开发者_Go百科being thrown is now -
Value of type 'System.Collections.Generic.List(Of ArdbegWeb.ArdbegMember)' cannot be converted to 'ArdbegWeb.ArdbegMember'.
You can always create a service that returns a List<T>
given you define a concrete type for <T>
:
[ServiceContract]
public interface IYourService
{
[OperationContract]
public List<ArdbegMember> GetMembers();
}
Assuming you have a data contract class:
[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`)
}
In your service implementation, you could then write:
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;
}
}
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 of ArdbegMember
objects.
Some good WCF resources:
- The WCF Developer Center
- Beginner's Guide to Windows Communication Foundation
- Resources and Community
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.
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 read Do 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!
精彩评论