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,

6
  • related if not a duplicate: stackoverflow.com/questions/5241661/… Commented Sep 24, 2019 at 9: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 Commented Sep 24, 2019 at 9: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. Commented Sep 24, 2019 at 11:43
  • Passing-a-class-as-parameter-in-restful-wcf-service you can find a solution. Commented Sep 24, 2019 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. blogs.msdn.microsoft.com/carlosfigueira/2011/08/08/… Commented Sep 25, 2019 at 2:33

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.