Search code examples
wcf

WCF REST WebGet for user defined parameters


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'.


Solution

  • 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