用于用户定义参数的 WCF REST WebGet

标签 wcf

我与 WebGet 的操作契约(Contract)定义如下。

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

当我运行该服务时,出现以下错误。有什么想法可以解决这个问题吗?

合约“UserConfigService”中的操作“UpdateUserDetails”有一个名为“_userConfigData”、类型为 Service1.WCF.UserConfig.UserConfigData”的查询变量,但类型“Service1.WCF.UserConfig.UserConfigData”无法由“QueryStringConverter”转换。 UriTemplate 查询值的变量必须具有可由“QueryStringConverter”转换的类型。

最佳答案

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

[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 数据似乎是这样的:

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

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

Fiddler

其他信息

响应结果“找不到获取方法
您可能没有正确定义端点或服务地址。你的 webconfig 文件应该有这种配置。

<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  

为了更好的引用,您可以尝试在这里观看:

How to create simple REST Based WCF Service with JSON format

关于用于用户定义参数的 WCF REST WebGet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9334643/

相关文章:

wcf - 添加用于 WCF 服务和 Windows Phone 7 应用程序的枚举类型

c# - 有什么轻松的方法可以创建发布 url 格式请求的 WCF REST 客户端?

WCF 读取数据成员名称属性

.net - IEndpointBehavior ApplyClientBehavior 方法在行为添加到端点行为后未执行

c# - 使用证书签署文件

.net - 尝试将 ASP.NET CORE 2 Web API 应用程序连接到 WCF 服务

jquery - 错误: Cannot process the message because the content type 'application/json; charset=utf-8 was not the expected type

c# - 如何使用非托管 C++ 中的自定义绑定(bind)连接到 WCF 服务

c# - 如何从客户端请求中获取 X509Certificate

wcf - 是否可以为 WCF WebGet 方法设置 ContentType?