2012-03-29 10 views
0

私は新しいWCFであり、豊かなサービスです。 WCFサービスでオブジェクトを渡す方法を学習しています。WCFサービスでオブジェクトを渡す

私は貼り付けコードとweb.configファイルを持っています。私はなぜこのエラーが表示されているのかわかりません。

私はあなたがクエリパラメータとして任意の複雑なデータ型を渡すことはできませんだと思う。..

Operation 'saveDataGet' in contract 'IRestServiceImpl' has a query variable named ' param1' of type 'GainSoft.TaskManager.Service.InputData', but type 'GainSoft.TaskManager.Service.InputData' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'. 
Description: An unhandled exception occurred during the execution of the current web request. 
Please review the stack trace for more information about the error and where it 

originated in the code. 


Exception Details: System.InvalidOperationException: Operation 'saveDataGet' in 
contract 'IRestServiceImpl' has a query variable named 'param1' of type 
'GainSoft.TaskManager.Service.InputData', but type 
'GainSoft.TaskManager.Service.InputData' is not convertible by  
'QueryStringConverter'. Variables for UriTemplate query values must have types that 
can be converted by 'QueryStringConverter'. 

public class RestServiceImpl : IRestServiceImpl 
{ 
    public string saveDataGet(InputData param1) 
    { 
     return "Via GET: " + param1.FirstName + " " + param1.LastName; 
    } 
    public string saveDataPost(InputData param1) 
    { 
     return "Via POST: " + param1.FirstName + " " + param1.LastName; 
    } 



    public class MyQueryStringConverter : QueryStringConverter 
    { 
     public override bool CanConvert(Type type) 
     { 
      return (type == typeof(InputData)) || base.CanConvert(type); 
     } 
     public override object ConvertStringToValue(string parameter, Type parameterType) 
     { 
      if (parameterType == typeof(InputData)) 
      { 
       string[] parts = parameter.Split(','); 
       return new InputData { FirstName = parts[0], LastName = parts[1] }; 
      } 
      else 
      { 
       return base.ConvertStringToValue(parameter, parameterType); 
      } 
     } 
    } 
    public class MyWebHttpBehavior : WebHttpBehavior 
    { 
     protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription) 
     { 
      return new MyQueryStringConverter(); 
     } 
    } 

public interface IRestServiceImpl 
{ 

    [OperationContract] 
    [WebGet(UriTemplate = "/InsertData?param1={param1}")] 

    string saveDataGet(InputData param1); 

    [OperationContract] 
    [WebInvoke(UriTemplate = "/InsertData")] 
    string saveDataPost(InputData param1); 
} 

<behaviors> 
    <serviceBehaviors> 
    <behavior name="ServiceBehaviour" > 
     <!-- Add the following element to your service behavior configuration. --> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false"/> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="MyWebHttpBehavior"> 
     <webHttp/> 

    </behavior> 
    </endpointBehaviors> 
</behaviors> 
+0

http://stackoverflow.com/questions/3738201/how-can-i-implement-a-custom-querystringconverter-for-restful-wcf –

答えて

0

を助けてください。要件に応じてPOSTやPUTなどの別の動詞を使用してください。 InputDataをjson stringまたはsmthにシリアル化することもできます。これをこの方法で渡します。

+0

コード例から、ブログや投稿へのリンクも表示されます役に立った – Gainster

+0

確かに、ここに行く、これが役立つことを願って [リンク](http://stackoverflow.com/questions/1709781/wcf-rest-parameters-involving-complex-types) – Helikaon

関連する問題