Return Custom Collection and Web Services

W

Whoever

Here's what I have

A custom collection:
public class aUser {
public string UserName { get {...} set {...}
}

public class UserList : System.Collection.CollectionBase {
public void Add(aUser user) {...}
public aUser this[int Index] {...}
}

A web service:

[WebMethod]
[return: XmlElement("UserList", typeof(UserList))]
public UserList getUserList(string search) {
UserList ul = new UserList();
return ul;
}

On the client side, when adding a reference to the webservice, it can only
see aUser object, but not UserList object. So I have to do

localhost.PhoneSvc svc = new WebApplication1.localhost.PhoneSvc();
localhost.aUser[] a = svc.getContact("test");

Is there a way to expose the collection object, so that you can do something
like this?

UserList ul = svc.getUserList("tester");

I know there are several articles out there that create wrapper class so
that it can be bounded to datagrid, etc. But none of them seems to be able
to expose the collection object directly.

Any ideas? Thanks.
 
K

Kevin Spencer

Try this:

[WebMethod]
public UserList getUserList(string search) {
UserList ul = new UserList();
return ul;
}

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
A

Angelos Karantzalis

We've used an array as the return type for our web services, and that worked
ok. You can always implement an AddRange(User[] users) method in your
collection to re-create the collection from the array ...

Angel
O:]
 
W

Whoever

Thanks for the help. You mean without the [return: XmlEelement...]? I
tried that but it's the same.

It seems whenever you return a class that inherits
System.Collections.CollectionBase, directly or indirectly, it ends up as an
array of the object it contains, never the collection object itself.

Am I missing something?

Kevin Spencer said:
Try this:

[WebMethod]
public UserList getUserList(string search) {
UserList ul = new UserList();
return ul;
}

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

Whoever said:
Here's what I have

A custom collection:
public class aUser {
public string UserName { get {...} set {...}
}

public class UserList : System.Collection.CollectionBase {
public void Add(aUser user) {...}
public aUser this[int Index] {...}
}

A web service:

[WebMethod]
[return: XmlElement("UserList", typeof(UserList))]
public UserList getUserList(string search) {
UserList ul = new UserList();
return ul;
}

On the client side, when adding a reference to the webservice, it can only
see aUser object, but not UserList object. So I have to do

localhost.PhoneSvc svc = new WebApplication1.localhost.PhoneSvc();
localhost.aUser[] a = svc.getContact("test");

Is there a way to expose the collection object, so that you can do something
like this?

UserList ul = svc.getUserList("tester");

I know there are several articles out there that create wrapper class so
that it can be bounded to datagrid, etc. But none of them seems to be able
to expose the collection object directly.

Any ideas? Thanks.
 
W

Whoever

For a more complicated case, such as:

<Department name='something'>
<Group id='123'>
<User>...</User>
<User>...</User>
</Group>
<Group>
...
</Group>
</Department>
<Department>
...
</Department>

Array is not as convenient. Also, it's also much better if you can:

DepartmentList deptlist = svc.getDepartment()
foreach (Department d in deptlist)
GroupList grplist = svc.getGroup(d);

(this last code is not real and may totally be wrong, since I haven't figure
out how to expose the list in the first place)


Angelos Karantzalis said:
We've used an array as the return type for our web services, and that worked
ok. You can always implement an AddRange(User[] users) method in your
collection to re-create the collection from the array ...

Angel
O:]


Whoever said:
Here's what I have

A custom collection:
public class aUser {
public string UserName { get {...} set {...}
}

public class UserList : System.Collection.CollectionBase {
public void Add(aUser user) {...}
public aUser this[int Index] {...}
}

A web service:

[WebMethod]
[return: XmlElement("UserList", typeof(UserList))]
public UserList getUserList(string search) {
UserList ul = new UserList();
return ul;
}

On the client side, when adding a reference to the webservice, it can only
see aUser object, but not UserList object. So I have to do

localhost.PhoneSvc svc = new WebApplication1.localhost.PhoneSvc();
localhost.aUser[] a = svc.getContact("test");

Is there a way to expose the collection object, so that you can do something
like this?

UserList ul = svc.getUserList("tester");

I know there are several articles out there that create wrapper class so
that it can be bounded to datagrid, etc. But none of them seems to be able
to expose the collection object directly.

Any ideas? Thanks.
 
G

Guest

Hi all (specially Whoever),

I've got your same problem. My WebMethod must return an ArrayList of custom
objects. Nevertheless, when calling the web method from my ASP.NET page, it
returns me an array of objetcs. What can I do? I've been trying to
deserialize objects with XMLInclude and so on, but I haven't got very clear
ideas about these issues of serialization adn deserialization.

I want the client to recognize my custom objects inside the Arraylist
because I must bound the returned ArrayList to a DataGrid and this way it's
no posible. First of all, do Web Services work well with Arraylists?

"Whoever", where are these articles telling about making wrappers in order
to bound DataGrids?

I'm willing your beautiful ideas. Thanks in advance.
 
W

Whoever

http://www.ftponline.com/vsm/2003_06/magazine/columns/aspnet/

Datagrid will bound to Properties not field. You can fix that. But I would
really like to see the list object pass to the client so that you can
declare

UserList ul;

Instead of

aUser[] users;

from the client side. Especially with multile level xml document.

Maybe it's not doable with custom collection?


MIGUEL said:
Hi all (specially Whoever),

I've got your same problem. My WebMethod must return an ArrayList of custom
objects. Nevertheless, when calling the web method from my ASP.NET page, it
returns me an array of objetcs. What can I do? I've been trying to
deserialize objects with XMLInclude and so on, but I haven't got very clear
ideas about these issues of serialization adn deserialization.

I want the client to recognize my custom objects inside the Arraylist
because I must bound the returned ArrayList to a DataGrid and this way it's
no posible. First of all, do Web Services work well with Arraylists?

"Whoever", where are these articles telling about making wrappers in order
to bound DataGrids?

I'm willing your beautiful ideas. Thanks in advance.


Whoever said:
Here's what I have

A custom collection:
public class aUser {
public string UserName { get {...} set {...}
}

public class UserList : System.Collection.CollectionBase {
public void Add(aUser user) {...}
public aUser this[int Index] {...}
}

A web service:

[WebMethod]
[return: XmlElement("UserList", typeof(UserList))]
public UserList getUserList(string search) {
UserList ul = new UserList();
return ul;
}

On the client side, when adding a reference to the webservice, it can only
see aUser object, but not UserList object. So I have to do

localhost.PhoneSvc svc = new WebApplication1.localhost.PhoneSvc();
localhost.aUser[] a = svc.getContact("test");

Is there a way to expose the collection object, so that you can do something
like this?

UserList ul = svc.getUserList("tester");

I know there are several articles out there that create wrapper class so
that it can be bounded to datagrid, etc. But none of them seems to be able
to expose the collection object directly.

Any ideas? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top