Search code examples
c#restwcfservicecontractoperationcontract

Use parameter "params string[]" in WCF Rest endpoint


I would like to define an OperationContract, which I can pass any number of string parameters. The values should be interpreted as an array of string. Is there any possibility to use this type of parameter in an OperationContract and define this in the UriTemplate?

[ServiceContract]
public interface IRestService {
    [OperationContract]
    [WebGet(UriTemplate = "operations/{values}")]
    void Operations(params string[] values);
}

Solution

  • You should not do this on a GET operation. GET operations support parameters on the path or the query string only, neither of which are suitable for complex types such as collections.

    A collection should be passed as a body parameter using a POST operation.

    [OperationContract]
    [WebInvoke(Method = "POST", 
               RequestFormat = WebMessageFormat.Json, // or xml
               UriTemplate = "operations/xAllTheStrings")]
    void Operations(string[] values);