开发者

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

开发者 https://www.devze.com 2022-12-08 22:20 出处:网络
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:

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_obj开发者_JAVA百科File.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());

...or similar.

0

精彩评论

暂无评论...
验证码 换一张
取 消