Search code examples
wpflistwcftype-conversion

Function conversion of argument did not succeed when calling WCF service


I have a WCF service with this function :

namespace PortfolioSerivce
{
    [ServiceContract]
    public interface IService1
    {
         [OperationContract]
         bool CreateOrders(List<OrderDTO> ListOrder);
    }
}

and here its code

    public bool CreateOrders(List<OrderDTO> ListOrder)
    {
        return true;
    }

The class OrderDTO is defined as below :

[DataContract]
public class OrderDTO
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]        
    public char OrderType { get; set; }

    [DataMember]
    public float OrderedQty { get; set; }

    [DataMember]
    public float PreUNitCost { get; set; }

    [DataMember]
    public float PostUnitCost { get; set; }

    [DataMember]
    public float MarketPrice { get; set; }

    [DataMember]
    public float ClientId { get; set; }

    [DataMember]
    public float SecurityId { get; set; }
}

In WPF, I have a function in which I want to pass a list to the WCF function:

    internal bool RemoteCreateOrders(List<Position> securityMarketDTO)
    {
        List<OrderDTO> ListOrders = new List<OrderDTO>();
        Tools.MyRemoteService.CreateOrders((List<OrderDTO>)ListOrders);
        return true;
    }

When I compile I got those errors :

The best overloaded method match for 'PortfolioView.PortfolioService.Service1Client.CreateOrders(PortfolioView.PortfolioService.OrderDTO[])' has some invalid arguments

Cannot convert from 'System.Collections.Generic.List' to 'PortfolioView.PortfolioService.OrderDTO[]'

What is the problem here, as I send a list of OrderDTO from my app and in WCF I declared as parameter a list of OrderDTO. What conversion should I use?

Thanks in advance.


Solution

  • From you exception , you Service1Client's method CreateOrders needs a variable of type OrderDTO[], while you pass List.

    Please pass the variable of right type , use ListOrders.ToArray to convert list to array.

    Or if you want to pass List, please let vs generate List for collection when you add service reference.

    enter image description here

    When adding service reference using vs, you could click advanced at the bottom, and then your could configure Collection type.