简体   繁体   中英

Unable to pass a Dictionary as a parameter to WCF REST service in GET type of method

I have a requirement where most of WCF REST apis are of type GET and would receive Dictionary as a param. But when it is put as below, it throws error.

[WebGet(UriTemplate = "/GetVersion?Param1={serverDetails}")]
public Version GetVersion(Dictionary<string, string> keyValue)
{

}

It gives below error:

Operation 'GetVersion' in contract 'IDeploy' has a query variable named 'keyValue' of type 'System.Collections.Generic.Dictionary'2[System.String,System.String]', but type 'System.Collections.Generic.Dictionary'2[System.String,System.String]' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.

Any idea how to resolve this? it would be hard to replace Dictionary param type as there are lots of such methods in service.

For supporting such complex parameters, The Get request cannot accomplish it. We need Post/Put/Delete request to complete this task since we can set up these complex parameters in the request body.

[OperationContract]
        [WebInvoke(RequestFormat =WebMessageFormat.Json,BodyStyle =WebMessageBodyStyle.Bare)]
        string GetData(Dictionary<string,string> value);

Below is the JSON content of the sample request.

[{
    "Key":"Key1",
    "Value":"World"
},
{
    "Key":"Key2",
    "Value":"Hello"
}]

Below is the Xml content of the sample request when set to WebMessageFormat.Xml

<ArrayOfKeyValueOfstringstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <KeyValueOfstringstring>
    <Key>Key1</Key>
    <Value>Hello</Value>
  </KeyValueOfstringstring>
  <KeyValueOfstringstring>
    <Key>Key2</Key>
    <Value>World</Value>
  </KeyValueOfstringstring>
</ArrayOfKeyValueOfstringstring>

Feel free to let me know if the problem still exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM