2011-06-24 7 views
1

を置く/投稿私はこのようになりますWebサービスを持っている:私はC#のHttpClientを経由してWebサービスを打つときC#WCFのWeb APIの問題

RouteTable.Routes.MapServiceRoute<SomeStuffService>("1.0/SomeStuff", new HttpHostConfiguration()); 

[WebInvoke(UriTemplate = "/{userName}/?key={key}&machineName={machineName}", Method = "PUT")] 
public HttpResponseMessage<SomeStuffPutResponse> PutSomeStuff(string userName, string key, string machineName, string theTextToPut) 
{ 
    // do stuff 
} 

私global.asxは次のようになりますそれとも500を投げていて、私の方法にも達していない。私は、ログの束を追加し、次のエラー取得しています:

The service operation 'PutSomeStuff' expected a value assignable to type 'String' for input parameter 'requestMessage' but received a value of type 'HttpRequestMessage`1'.

がUPDATE:私はtheTextToPut変数にカスタムオブジェクトを作成する場合は、正常に動作します。それは文字列のようなプリミティブ型の場合に問題を起こしているだけです。

答えて

0

urlに文字列theTextToPutがあります。

+1

uriではなくリクエストボディにその文字列を指定する方法を指定します。 –

2

ソリューション1.

あなたはHttpRequestMessageにtheTextToPutパラメータを変更して、メッセージの内容を読むことができます。

[WebInvoke(UriTemplate = "/{userName}/?key={key}&machineName={machineName}", Method = "PUT")] 
public HttpResponseMessage<SomeStuffPutResponse> PutSomeStuff(string userName, string key, string machineName, HttpRequestMessage request) 
{ 
    string theTextToPut = request.Content.ReadAsString(); 
} 

ソリューション2.

あなたは本当にあなたが「theTextToPut」という名前のすべての文字列パラメータを扱う操作ハンドラを作成することができます文字列としてパラメータを取得したい場合。

public class TextToPutOperationHandler : HttpOperationHandler<HttpRequestMessage, string> 
    { 
     public TextToPutOperationHandler() 
      : this("theTextToPut") 
     { } 

     private TextToPutOperationHandler(string outputParameterName) 
      : base(outputParameterName) 
     { } 

     public override string OnHandle(HttpRequestMessage input) 
     { 
      return input.Content.ReadAsString(); 
     } 
    } 

次のように次にあなたがGlobal.asaxの中で、あなたのサービスを設定:

RouteTable.Routes.MapServiceRoute<SomeStuffService>("1.0/SomeStuff", 
       new HttpHostConfiguration().AddRequestHandlers(x => x.Add(new TextToPutOperationHandler()))); 
0

を@ axel22としては、おそらく、アプリケーションがURIにtheTextToPut結合される、と言います。 this article statesのように、単純な型はデフォルトでURIにバインドされます。

FromBody attributeを使用すると、アプリケーションで要求の本文にtheTextToPutをバインドすることができます。