Peter
Peter

Reputation: 5332

Cannot Convert from 'List<T>' to 'T[]'

I am trying to pass a list object of type List<UploadQueue> to a WCF SOAP method of the same parameter type and I am getting the error:

Cannot Convert from 'System.Collections.Generic.List' to 'WebAPI.Upload.UploadQueue[]'

I don't understand this because my WCF method's (below) parameter type is List<UploadQueue>:

IService.DoUpload(List<UploadQueue> request)

Here is the code that calls "DoUpload" which returns the above error.

    List<UploadQueue> results = new List<UploadQueue>();
    HttpPostedFile m_objFile  = default(HttpPostedFile);
    int m_objFlag = default(int);
    Guid m_objGuid = Guid.NewGuid();
    DateTime m_objDate = DateTime.Now;

    try
    {
        if (Request.Files.Count > 0)
        {
            for (var j = 0; i <= (Request.Files.Count - 1); j++)
            {
                m_objFile = Request.Files[j];

                if (!(m_objFile == null | string.IsNullOrEmpty(m_objFile.FileName) | m_objFile.ContentLength < 1))
                {
                    results.Add(new UploadQueue(
                        m_objGuid,
                        m_objFlag,
                        m_objFile.ContentLength,
                        m_objFile.FileName,
                        m_objDate)
                    );

                }

            }
        }

    }
    catch (Exception ex)
    {
        //handle error
    }

    retParam = upload.DoUpload(results);

Ideas? Thanks.

Upvotes: 1

Views: 3191

Answers (4)

user16324
user16324

Reputation:

In your client project, you need to right click on the service reference and select "Configure Service Reference". On configuration screen, in the Data Type section, you need to set the collection type to System.Collections.Generic.List instead of System.Array.

Upvotes: 14

RichardOD
RichardOD

Reputation: 29157

The generated client has replaced the List with an Array (The default behaviour). With VS.NET 2008 you have the option of generating this with a List instead- look at the Configure Service Dialog Box. As other have said ToArray will work.

Upvotes: 3

Justin Niessner
Justin Niessner

Reputation: 245399

retParam = upload.DoUpload(results.ToArray());

...or similar.

Upvotes: 1

kemiller2002
kemiller2002

Reputation: 115420

Try doing results.ToArray(). That will probably fix it.

upload.DoUpload(results.ToArray());

The problem is that the soap service says that it wants an array of objects, and not a list. When the proxy class is built from the WSDL, it converts it to the most basic object it can that satisfies the needs of the service which is an array.

Upvotes: 2

Related Questions