none
How to do service WCF REST with a parameter Array RRS feed

  • Question

  • 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:

    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 input parameter is an array ?

    thanks,

    mardi 24 septembre 2019 09:33

Réponses

  • 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();

    • Marqué comme réponse Thabetidris mercredi 25 septembre 2019 12:42
    mercredi 25 septembre 2019 12:42