2012-02-17 31 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)。