RE: Returning data contained in Ienumerable<type> as type[]?



Peter,

Thanx for the response.

I have a stored procedure which I execute via a DataReader. The method that
reads the data from the stored procedure returns structs with the "yield
return" method as a generic IEnumerable for example:

public IEnumerable<CustomerStruct> GetCustomers()
{
IdataReader Mydata = DataLayer.Execute("ProcedureName");
while (Mydata.Read())
{
yield return new CustomerStruct(MyData.GetString(0).......);
}
}

I call this method from within a web service. The problem is that a web
service cannot expose IEnumerable<CustomerStruct>, so I have to use a
CustomerStruct[].

In order to get the results from the GetCustomers method returned, I first
have to get all the CustomerStruct structs in a single collection, which
takes up to much memory and makes things slow.

I would prefer a way to start building up the resulting soap message that
goes to the client while I am iterating through the method's resulting
IEnumerable.

If this can be done, it means that I will always have only one
CustomerStruct and one datarow in memory at a given time, which will be much
faster than having to collect all CustomerStruct's first.

I am currently doing this as follows, but it is to slow:

List<CustomerStruct> Result = new List<CustomerStruct>(GetCustomers());
return Result.ToArray();

I hope this makes more sense now.

"Peter Ritchie [C# MVP]" wrote:

No, IEnumerable only provides a means to enumerate, not convert (hence the
"Enumerable"). IEnumerable is merely an interface to an implementation which
probably has something you can use to convert to an array. Where are you
getting this IEnumerable interface instance? Are you implementing this
collection yourself or are you retrieving it from an 3rd party method?

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


"FrancoisViljoen" wrote:

Hi there,

Does anybody know how to return the results of an IEnumerable<type> as an
array of the same type i.e type[] in a web service call without first having
to collect all elements in the IEnumerable<type> and storing it in memory
first?

This question is really about optimizing large collections of data returned
by web services. If it is possible to loop through the IEnumerable<type>
with a "foreach" and somehow start formatting the soap response already,
there should be a fair amount of memory and time saved. We are currently
running into timeouts and outofmemoryExceptions because building up a
collection of types in a list<type> cannot handle the load.

I tried to do this using the Context.Response and the formatting the SOAP
message myself, but the result of the call on the client returns a null
value. I suspect that this is because it expects a compressed SOAP message.
I also tried to compress the response using GZipStream, but to no avail.

Any help on this would be greatly appreciated.


.



Relevant Pages

  • Content-based routing and content modification
    ... I have a web service with SoapExtensions that read and modify the ... content of the SOAP message both in the request and in the response. ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • Web Service returning wrong content type of text/plain
    ... I have a .net WSE 3.0 Web Service acting as a client calling a j2EE ... Client found response content type ... REST OF SOAP MESSAGE HERE--. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Sign Web Service response
    ... I need to sign the response of my Web Service. ... How can I take the certificate to add it to the soap message? ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • RE: Help consuming third part web services
    ... // The web.config defines the target URL for the web service. ... string xResp = x.AuthenticateUser; ... report the response to the caller. ... I don't think I have a Client Proxy such as is created with WSDL.EXE ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Securing files stored in virtual directory
    ... multiple attachments. ... local file to an attachment, and the web service would receive ... the key as the response from the save step. ... and that exists still in the database. ...
    (microsoft.public.dotnet.framework.webservices.enhancements)