我与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.UserConfig.UserConfigData“”的名为“”_userConfigData“”的查询变量,但类型“”Service1.WCF.UserConfig.UserConfigData“”不能由“”QueryStringConverter“”转换。“”UriTemplate查询值的变量必须具有可由“QueryStringConverter”转换的类型。
发布于 2012-02-21 17:15:15
我将假设您使用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
附加信息
响应结果“ not 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
发布于 2012-02-18 04:06:31
你必须使用字符串,你不能使用对象作为查询字符串参数。它不会将你的查询字符串转换成一个对象。这些变量应该定义为字符串。
发布于 2012-02-21 17:38:32
Here's a link on implementing a custom QueryStringConverter,它会做你想做的事情。还注意到(在那篇文章中提到),将(可能)像UserConfigData或ConfigResult这样的复杂对象作为POST数据传递可能更好,而不是在URL中传递。考虑到您的方法名为"UpdateUserDetails",本着REST的精神,最好使用POST (WebInvoke)而不是GET (WebGet)。
https://stackoverflow.com/questions/9334643
复制相似问题