2

I have below operation contract with WebGet defined as follows.

[OperationContract]
[WebGet(UriTemplate = "UpdateUserDetails/?configdata={_userConfigData}&configresult={_configResult}&clientip={_clientIP}&adminname={AdminName}")]
public void UpdateUserDetails(UserConfigData _userConfigData, ConfigResult _configResult, string _clientIP, string AdminName)

When I run the service, I am getting below error. Any ideas how to fix this issue?

Operation 'UpdateUserDetails' in contract 'UserConfigService' has a query variable named '_userConfigData' of type Service1.WCF.UserConfig.UserConfigData', but type 'Service1.WCF.UserConfig.UserConfigData' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.

3 Answers 3

2

I will assume that you use Json object to request data.
it should be like this:

[OperationContract]
[WebInvoke(UriTemplate = "UpdateUserDetails?_clientIP={_clientIP}&AdminName={AdminName}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public void UpdateUserDetails(UserConfigData _userConfigData, ConfigResult _configResult, string _clientIP, string AdminName)  

And JSON data seems to be like this:

{
    "_userConfigData":{
        "Property1":"value",
        "Property2":"value",
        "Property3":"value"
        ..and so on...
    },
    "_configResult":{
        "Property1":"value",
        "Property2":"value",
        "Property3":"value"
        ..and so on...
    }
}

There is a good application for testing Rest services, you can try to use:

Fiddler

Additional Info

In response to the result "getting Method not found"
You may not have defined the endpoint or the service address properly. Your webconfig file should have this kind of config.

<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<bindings>
  <basicHttpBinding>
    <binding name="soapBinding">
      <security mode="None"></security>
    </binding>
  </basicHttpBinding>
  <webHttpBinding>
    <binding name="webBinding"></binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="jsonBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="defaultServiceBehavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
  <!-- USING SOAP-->
  <service behaviorConfiguration="defaultServiceBehavior" name="MyProject.WCF.UserConfig.UserConfigService">
    <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="soapBinding" contract="MyProject.WCF.UserConfig.IUserConfigService"></endpoint>
  </service>
  <!-- USING JSON-->
  <service behaviorConfiguration="defaultServiceBehavior" name="MyProject.WCF.UserConfig.UserConfigService">
    <endpoint address="json" binding="webHttpBinding" bindingConfiguration="webBinding" behaviorConfiguration="jsonBehavior" contract="MyProject.WCF.UserConfig.IUserConfigService"></endpoint>
  </service>
</services>
</system.serviceModel>  

The address seems like this:

SOAP
localhost:1706/soap/UserConfigService.svc

JSON
localhost:1706/json/UserConfigService.svc  

For better reference you could try to watch here:

How to create simple REST Based WCF Service with JSON format

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I tried this with POST. But when I try to access the method from browser as shown below, I am getting Method not found. Any ideas? localhost:1706/WCF/UserConfig/UserConfigService.svc/…
0

you have to use string, u cant use an object as query string prameters. it wont convert your query string to an object. those variables should be defined as string.

2 Comments

Thanks, how do I convert from string to user defined object? Can you please provide me a sample?
i dont have a sample right now. but once u get the parameter in your implementation than u can create whatever object you need right there. it makes sense no? a HTTP URL doesnt know what your object is.
0

Here's a link on implementing a custom QueryStringConverter, which will do what you want it to. Note also (mentioned in that post) that it might be better to pass a (possibly) complicated object like UserConfigData or ConfigResult as POST data, rather than in the URL. Considering your method is called "UpdateUserDetails", it's probably best to use a POST (WebInvoke) instead of a GET (WebGet) anyway, in the spirit of REST.

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.