views:

343

answers:

6

Hey folks

The problem i'm having is that i've created a web service, and a windows form (separate solutions). I added the web service to the form app using a service reference, I can pass an 'int' or a 'string' to the web service no problem, but i cannot pass an array of int's or List<int>

The web service code is as follows:

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Services;

namespace CalculateService
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]

    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public int CalcluateSum(List<int> listInt)
        {
            int sum = listInt.Sum();
            return sum;
        }
    }
}

and the client code is:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalculateForm
{
    public partial class FormCalculate : Form
    {
        List<int> intArray = new List<int>();

        public FormCalculate()
        {
            InitializeComponent();
        }

        private void btnAddNum_Click(object sender, EventArgs e)
        {
            intArray.Add(Convert.ToInt32(txtAddNum.Text));
            txtAddNum.Clear();
            listBoxAddNum.Items.Clear();
            for (int i = 0; i < intArray.Count; i++)
            {
                listBoxAddNum.Items.Add(intArray[i]);
            }
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            CalculateForm.CalculateService.Service1SoapClient client = new CalculateForm.CalculateService.Service1SoapClient();
            int result = client.CalcluateSum(intArray);
            txtResult.Text = Convert.ToString(result);
        }
    }
}

The error I am getting is:

Argument '1': cannot convert from 'System.Collections.Generic.List' to 'CalculateForm.CalculateService.ArrayOfInt'

I am sure it's something simple and I'll feel daft when someone points it out :)

Cheers Carl

+4  A: 

WSDL cannot handle Lists, so it uses an Array instead, doing the conversion before it passes it into your Service Method. Just call ToArray() on your List before calling the method.

pdr
A: 

Try this:

 client.CalcluateSum(intArray.ToArray()) 
Robert
A: 

Cheers guys but if i try:

int result = client.CalcluateSum(intArray.ToArray());

or

int[] array = intArray.ToArray();
int result = client.CalcluateSum(array);

The error message just changes to:

Argument '1': cannot convert from 'int[]' to 'CalculateForm.CalculateService.ArrayOfInt'

P.S. I figured I'd reply here rather than to each comment individually as you all suggested the same fix.

Carl
Did you also change your methods parameter from List<int> to int[]?
Carra
How did you generate your Service1SoapClient code?
pdr
Change "public int CalcluateSum(List<int> listInt)" on the server to "public int CalcluateSum(int[] listInt)" If you need to convert it to a List<int>, pass the array in the constructor. But treating it as an array or IEnumerable<int> should suffice for most uses.
Jesse C. Slicer
@Carra - yup i changed the web service parameter to int[]@pdr - do you mean the code generated by visual studio when i published the web service to IIS?
Carl
@Carl, yes, that's what I meant. I see you've answered the question: you used a Web Reference. Judging by the wording of your assignment, I think you're fine to write a service reference. That's just invoking WCF on the client side; the wording says nothing about the client.
pdr
A: 

This question looks similar to this one. I'd say you'd need to write a quick static function that takes in a List and performs a type conversion over to CalculateForm.CalculateService.ArrayOfInt. Your web service has generated this type, and it would seem it expects nothing less. I'm unfamiliar with this behavior because I pass Lists across web services regularly and my web services have always managed to perform the conversion .ToArray() for me.

Joel Etherton
I tried adding the web service to my client app as a 'Web Service' rather than a 'Service Reference' and this allowed me to pass the List just fine, problem being is that it's for coursework and I believe that it has to be added as a 'Service Reference'I'll try the suggestion from the other question and see how i get onThanks
Carl
A: 

I finally got it working!! :)

I managed to get it to pass the List<int> rather than using the ToArray() method.

Here's the client code:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CalculateForm
{
    public partial class FormCalculate : Form
    {
        List<int> listInt = new List<int>();

        public FormCalculate()
        {
            InitializeComponent();
        }

        private void btnAddNum_Click(object sender, EventArgs e)
        {
            listInt.Add(Convert.ToInt32(txtAddNum.Text));
            txtAddNum.Clear();
            listBoxAddNum.Items.Clear();

            for (int i = 0; i < listInt.Count; i++)
            {
                listBoxAddNum.Items.Add(listInt[i]);
            }
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            CalculateService.Service1SoapClient client = new CalculateService.Service1SoapClient();

            CalculateService.ArrayOfInt arrayOfInt = new CalculateService.ArrayOfInt();

            arrayOfInt.AddRange(listInt);

            int result = client.CalcluateSum(arrayOfInt);

            txtResult.Text = Convert.ToString(result);
        }
    }
}

and the web service code:

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Services;

namespace CalculateService
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]

    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public int CalcluateSum(List<int> listInt)
        {
            int sum = listInt.Sum();
            return sum;
        }
    }
}

so basically all I had to do was create a new instance of the CalculateService.ArrayOfInt in the client, and add the range of data from the List<int> listInt then pass arrayOfInt to the web service.

Cheers everyone for your help, no doubt I'll be back soon :)

Carl
A: 

I did it using WCE3.0 extension.When you generate the reference.cs, the system put int[] ionstead of List. You have to replace int[] by List in Reference.cs file. Regards.

Guillermo Regairaz