Can't pass the <T> list to a web service?

I have the same classes on my server and on my web service. I have the following WebMethod:

[WebMethod]
        public int CreateOrder(List<Purchase> p, string username)
        {
            o.Add(new Order(p,username));

            return o.Count;
        }

However, the following code runs on the server:

protected void CartRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        List<Purchase> l = ((List<Purchase>)Session["Cart"]);

        if (e.CommandName == "Order")
        {
            localhost.ValidateService WS = new localhost.ValidateService();
            WS.CreateOrder(l, Session["username"].ToString());
        }
    }

It gives the following error: Argument '1': cannot convert from 'System.Collections.Generic.List<Purchase>' to 'localhost.Purchase[]'.

How to transfer an object list<Purchase>to a web service?

+3
source share
5 answers

When using such web services, it is List<T>converted to an array ( T[]) by default . Convert your list to an array by executing .ToArray()before passing it to a method.

Another option is to change the web service code generation settings to use lists instead of arrays.

, , , Purchase, , -, Purchase. , ( ). , - Automapper .

+7

svcutil -, collectionType, , . , , , WCF; 100%, ASMX.

, , :

svcutil.exe /collectionType:System.Collections.Generic.List`1 [service url]
+2

, webservice SOAP , XML-. .NET .

, , , , .

+1

List <T> xml, <T> , , xml.

, List <T> , -, no-nos, .

+1

localohost.ValidateService is a proxy class with its own namespaces for classes: then "Order" is not the same as "localhost.Order"

if your calling web service is from a different method in the same web service class, try the following:

tihs.CreateOrder (l, Session ["username"]. ToString ());

0
source

Source: https://habr.com/ru/post/1740899/


All Articles