Use parameter “params string[]” in WCF Rest endpoint

爱⌒轻易说出口 提交于 2019-12-08 01:10:39

问题


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);
}

回答1:


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



回答2:


No, but you can make an (extension) method to your proxy and/or service contract for convenience, that would expose a params string array parameter, and pass it to the real proxy/service contract as a string array.




回答3:


Your Service Contract interface is just that - a formal contract between what the server will accept and what the client needs to adhere to. This contract is serialised in to XML in the form of the wsdl - so any data types which appear in your contract must be expressible in their serialised form in XML.

In your case, the number of the arguments to your service call is not well-defined: it could have 0, 1, 2... etc. One of the tenants of service orientation is that the contracts need to be explicit - which this is not.

The most "idiomatic" approach (within a service orientated context) is the following:

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

As suggested in this answer, if you want to add some syntactic sugar on the client side you could create an extension method which does use the params keyword to make the client-side experience easier to digest.

EDIT:

As pointed out by Tom, the above contract will not work. You would need to either change the operation to a POST (as demonstrated on Tom's answer), or make the argument string of a delimited that you unravel on the server side to produce the array:

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


来源:https://stackoverflow.com/questions/38452787/use-parameter-params-string-in-wcf-rest-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!