用于用户定义参数的 WCF REST WebGet [英] WCF REST WebGet for user defined parameters

查看:26
本文介绍了用于用户定义参数的 WCF REST WebGet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与 WebGet 的操作合同定义如下.

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?

合同UserConfigService"中的UpdateUserDetails"操作具有名为_userConfigData"的 Service1.WCF.UserConfig.UserConfigData 类型的查询变量,但Service1.WCF.UserConfig.UserConfigData"类型不能被QueryStringConverter"转换.UriTemplate 查询值的变量必须具有可由QueryStringConverter"转换的类型.

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

推荐答案

我将假设您使用 Json 对象来请求数据.
应该是这样的:

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)  

而JSON数据好像是这样的:

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

有一个很好的测试Rest服务的应用,可以尝试使用:

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

小提琴手

附加信息

响应结果getting Method not found"
您可能没有正确定义端点或服务地址.你的 webconfig 文件应该有这种配置.

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>  

地址是这样的:

SOAP
localhost:1706/soap/UserConfigService.svc

JSON
localhost:1706/json/UserConfigService.svc  

为了更好的参考,您可以尝试在此处观看:

For better reference you could try to watch here:

如何使用 JSON 格式创建简单的基于 REST 的 WCF 服务

这篇关于用于用户定义参数的 WCF REST WebGet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆