Return complex data type from a web method

J

John

Hi,

I am implementing web services and try to return a collection of records,
say

1,"John","Doe",1/1/1990
2,"Jane","Doe",2/1/1990
3,"Joe","Smith",3/1/1990
.......

In my c# project I have a class that implements ICollection to provide these
data.

When I try to make web method to return the class, I got :
The return type of .... is not CLS-compliant.

What is the best way to return a collection of records from a web method?

TIA
 
G

Guest

Create a "Record" class with fields corresponding to the CSV data you posted,
and mark it as serializable. Your webservice will be happy to return an array
of these.
Peter
 
S

ssamuel

Your question tells us what your data looks like, but nothing about
what the return type of your function is. Can you post the signature of
the web service-visible method, as well as an outline of any
non-framework types that may be used in that signature?

As for the "best way" to do it, that's probably to create a shell
return type struct or class and dump that. The web service framework
will automatically serialize it as XML:

public struct myReturnType
{
public int ID;
public string FirstName;
public string LastName;
public DateTime Date;
}

If your method has the following signature:

[WebMethod]public myReturnType[] GetRecords(...) { ... }

you'll get back something vaguely like:

<GetRecords>
<myReturnType>
<ID>1</ID>
<FirstName>John</FirstName>
...
</myReturnType>
<myReturnType>
<ID>2</ID>
...
</myReturnType>
...
</GetRecords>

That's about the best way to use web services.


Stephan
 

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

Top