2

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,

IngTun2018
  • 329
  • 1
  • 10
  • related if not a duplicate: https://stackoverflow.com/questions/5241661/wcf-complex-json-input-error-not-convertible-by-querystringconverter – rene Sep 24 '19 at 09:33
  • thanks ,yes it is related but not duplicate, first I am using Microsoft.Net 4.6 and Visual studio 2017, and also the input parameter is an Array – IngTun2018 Sep 24 '19 at 09:47
  • Complexe type are send by Post/Put by convention. The question is a duplicate because both method are get. Both have only one paramter that is a complex type. The answer is the same either serialize and pass it as a string. Or go for Post and Put. Or changing the webconfig endpointBehavior so it does the serialisation it self. – xdtTransform Sep 24 '19 at 11:43
  • [Passing-a-class-as-parameter-in-restful-wcf-service](https://stackoverflow.com/a/6783286/5519709) you can find a solution. – Selim Yildiz Sep 24 '19 at 19:22
  • I recommend refactoring your method as you use the post method. As mentioned above, the get method cannot serialize parameters by default, so we have to manually add the serialization and deserialization methods to parse the data. https://blogs.msdn.microsoft.com/carlosfigueira/2011/08/08/wcf-extensibility-querystringconverter/ – Abraham Qian Sep 25 '19 at 02:33
  • @AbrahamQian ,thanks for your reply, in fact I see this best explanation of WCF Extensibility, but my question ,I want work with GET becuse i want to get a list of users from a list Ids, So when i modify the GET to POST for send the complex parameters in the body, i have the same result? list of users from list of Ids ? (because as we know POST just for adding and not to get ! ) – IngTun2018 Sep 25 '19 at 07:42

1 Answers1

1

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();
IngTun2018
  • 329
  • 1
  • 10