onkar kulkarni
onkar kulkarni

Reputation: 31

Passing List<T> as parameter to WCF service operation

I have a WCF operation contract which looks like this:

public void SavePersons(List<Person> list, bool IsSelected)
{
}

I am passing it a strongly typed list of Person objects (List<Person>) in my client. However, I am getting a bad request 400 message when calling the service. What am I doing wrong?

Upvotes: 0

Views: 4435

Answers (2)

Garima Sharma
Garima Sharma

Reputation: 101

I was facing a similar problem in passing a List<Health> of class Health type as a parameter to a wcf service method. I created a data contract in wcf service as below:

[DataContract]
public class Health
{
  ...
}

Defined a method in wcf service class such as:

public string GetData(List<Health> healthValues)

In my client application, while configuring/updating the service, I followed these steps:

  • Add/Update URL
  • Under Data Type (in Advanced), selected option, Collection type: System.Collection.Generic.List

And finally, I created a list and added the code in client as follows:

List<WcfService.Health> listHealth = new List<WcfService.Health>();
WcfService.Health h = new WcfService.Health();
.
.
listHealth.Add(h);
WcfService.Service1Client s = new WcfService.Service1Client();
string str = s.GetData(listHealth);

This solved my purpose and I was able to send the data as a list through wcf service.

Upvotes: 0

Paul Fleming
Paul Fleming

Reputation: 24526

May I suggest you create you create a contract to encapsulate the parameters like so:

public void SavePersons(PersonCollectionContract Request)
{
    ...
}

[DataContract]
public class PersonCollectionContract
{
    [DataContract]
    public List<Person> People { get; set; }

    [DataContract]
    public bool IsSelected { get; set; }
}

[DataContract]
public class Person
{
    ...
}

Upvotes: 2

Related Questions