IngTun2018
IngTun2018

Reputation: 329

How to do service WCF REST with a parameter Array

This is my class :

[DataContract]
public class UserIdParams 
{
    [DataMember]
    public int UserId { get; set; }
    [DataMember]
    public List<int> ListUserId { get; set; }
}

it contains both a list of integer and an integer.

My goal is create the service WCF REST : GetUserByID to get list of users according to ids

  [OperationContract]
  [WebGet]
  List<User> GetUserByID (UserIdParams userIdParams);

but as we know , we can't pass Array or complex types as input parameter in wcf rest ,

(and when I test it like that, I have this error:

enter image description here

In other hand , it worked fine for WCF SOAP.

So any idea how to resolve my problem to get all users with WCF REST and the parameter is an array ? thanks,

Upvotes: 2

Views: 956

Answers (1)

IngTun2018
IngTun2018

Reputation: 329

Thanks a lot for dnxit, he offered me a solution by always working with GET, * My old class:

public class UserIdParams : CommonParams
 {
[DataMember]
public int UserId { get; set; }
[DataMember]
public List<int> ListUserId { get; set; }     
}
  • and the old service :

    [OperationContract]        
    [WebInvoke(Method = "Get", UriTemplate = "GetUserByID")]    
    List<User> GetUserByID(UserIdParams userIdParams);
    

Now for fix this bug and work execute WCF REST with a parameter Array:

  • the modified class:

    public class UserIdParams : CommonParams
    {
    [DataMember]
    public int UserId { get; set; }
    [DataMember]     
    public string DelimitedUserIds { get; set; }
    }
    

    the modified service:

     [OperationContract]        
     [WebGet(UriTemplate = "GetUserByID?DelimitedUserIds={DelimitedUserIds}")]
     List<User> GetUserByID(string DelimitedUserIds);
    

And the most important thing is to add : (exemple)

string DelimitedUserIds = "9,3,12,43,2"
List<int> UserIds = DelimitedUserIds .Split(',').Select(int.Parse).ToList();

Upvotes: 1

Related Questions