2012-02-17 34 views
2

我与WebGet具有以下操作合同,定义如下。用于用户定义参数的WCF REST 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“有一个名为类型Service1.WCF.UserConfig.UserConfigData的“_userConfigData”查询变量”,但类型‘Service1.WCF.UserConfig.UserConfigData’是不被转换‘QueryStringConverter’ 。 UriTemplate查询值的变量必须具有可由'QueryStringConverter'转换的类型。

回答

2

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

+0

谢谢,我用POST试了一下。但是,当我尝试从浏览器访问方法如下所示,我得到方法未找到。有任何想法吗? http:// localhost:1706/WCF/UserConfig/UserConfigService.svc/UpdateUserDetails?configdata = test&configresult = test3 @&clientip = localhost – codematrix 2012-02-22 00:29:42

+0

看到我的编辑..希望它可以帮助 – fiberOptics 2012-02-22 01:10:12

0

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

+0

谢谢,我如何从字符串转换为用户定义的对象?你能给我一个样品吗? – codematrix 2012-02-17 22:12:43

+0

我现在没有样品。但是一旦你在你的实现中获得参数比你可以创建任何你需要的对象。它有道理吗?一个HTTP URL不知道你的对象是什么。 – DarthVader 2012-02-17 22:17:18

0

Here's a link on implementing a custom QueryStringConverter,它会做你希望它是什么。 也请注意(在该文章中提到),将(可能)像UserConfigDataConfigResult这样的(可能)复杂对象作为POST数据传递,而不是在URL中可能会更好。考虑到你的方法被称为“UpdateUserDetails”,本着REST的精神,最好使用POST(WebInvoke)而不是GET(WebGet)。