简体   繁体   中英

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.

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.

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.

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.

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

...或类似。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM