【问题标题】:WCF REST WebGet for user defined parameters用于用户定义参数的 WCF REST WebGet
【发布时间】:2012-02-17 20:04:14
【问题描述】:

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

[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”转换的类型。

【问题讨论】:

    标签: wcf


    【解决方案1】:

    我假设您使用 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

    其他信息

    响应结果“getting Method not found
    您可能没有正确定义端点或服务地址。你的 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

    【讨论】:

    【解决方案2】:

    你必须使用字符串,你不能使用对象作为查询字符串参数。它不会将您的查询字符串转换为对象。这些变量应该被定义为字符串。

    【讨论】:

    • 谢谢,如何从字符串转换为用户定义的对象?可以给我一个样品吗?
    • 我现在没有样品。但是一旦你在你的实现中获得了参数,你就可以在那里创建你需要的任何对象。有道理吗? HTTP URL 不知道您的对象是什么。
    【解决方案3】:

    Here's a link on implementing a custom QueryStringConverter,它会做你想做的事。 另请注意(在那篇文章中提到)最好将(可能)复杂的对象(如 UserConfigDataConfigResult 作为 POST 数据而不是在 URL 中传递)。考虑到您的方法称为“UpdateUserDetails”,本着 REST 的精神,最好使用 POST (WebInvoke) 而不是 GET (WebGet)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 2021-03-25
      • 2016-05-24
      • 1970-01-01
      相关资源
      最近更新 更多