2012-04-04 11 views
6

イムはそうのようなWCFのRESTサービスから画像を取得しようとしている:私のホストアプリケーションでボディーパラメーター 'width'。 GET操作には本体がありませんか?

[ServiceContract] 
public interface IReceiveData 
{ 
    [OperationContract] 
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "picture/")] 
    //this line is wrong though 
    Stream GetImage(int width, int height); 
} 
public class RawDataService : IReceiveData 
{ 
    public Stream GetImage(int width, int height) 
    { 
     // Although this method returns a jpeg, it can be 
     // modified to return any data you want within the stream 
     Bitmap bitmap = new Bitmap(width, height); 
     for (int i = 0; i < bitmap.Width; i++) 
     { 
      for (int j = 0; j < bitmap.Height; j++) 
      { 
       bitmap.SetPixel(i, j, (Math.Abs(i - j) < 2) ? Color.Blue : Color.Yellow); 
      } 
     } 
     MemoryStream ms = new MemoryStream(); 
     bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
     ms.Position = 0; 
     WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; 
     return ms; 
    } 
} 

Operation 'GetImage' in contract 'IReceiveData' uses GET, but also has body parameter 'width'. GET operations cannot have a body. Either make the parameter 'width' a UriTemplate parameter, or switch from WebGetAttribute to WebInvokeAttribute.

イムわからない:

class Program 
    { 
     static void Main(string[] args) 
     { 
      string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
      ServiceHost host = new ServiceHost(typeof(RawDataService), new Uri(baseAddress)); 
      host.AddServiceEndpoint(typeof(IReceiveData), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); 
      host.Open(); // this line 
      Console.WriteLine("Host opened"); 
      Console.ReadLine(); 

私はこのエラーを取得しますイメージのwebinvoke/UriTemplateメソッドを設定する方法、イメージを取得して返す方法この例では、画像を表示する正しい方法を投稿することができます。

EDIT

私は以下の回答を試してみて、http://www.localhost.com:8000/Service/picture?width=50&height=40に移動したときに私のUriTemplateとしてUriTemplate = "picture?w={width}&h={height}"を使用している場合、私は自分のコード内のエラーを受け取る:

public Stream GetImage(int width, int height) 
     { 
      Bitmap bitmap = new Bitmap(width, height); // this line 
      for (int i = 0; i < bitmap.Width; i++) 
      { 
       for (int j = 0; j < bitmap.Height; j++) 
       { 
        bitmap.SetPixel(i, j, (Math.Abs(i - j) < 2) ? Color.Blue : Color.Yellow); 
       } 
      } 
      MemoryStream ms = new MemoryStream(); 
      bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
      ms.Position = 0; 
      WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; 
      return ms; 
     } 

ArguementException was unhandled by user code:パラメータが有効ではないと述べ。

答えて

11

ランタイムでは、URLパラメータとして幅と高さが必要であることをランタイムに伝える必要があります。

現在のところ、ランタイムはパラメータなしでURLを呼び出すと仮定していますが、呼び出されるメソッドはパラメータを必要とするため、ランタイムは実際にはwidthのメソッドに渡す値の検索方法を認識しません。 height

これは

[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "picture/{width}/{height}")] 
Stream GetImage(string width, string height) 
{ 
    int w, h; 
    if (!Int32.TryParse(width, out w)) 
    { 
     // Handle error: use default values 
     w = 640; 
    } 
    if (!Int32.TryParse(height, out h)) 
    { 
     // Handle error use default values 
     h = 480; 
    } 

    .... 
} 

のように見えることができますし、http://test.tld/picture/320/200のようなURLを呼び出す必要があります。

+0

あなたのメソッドから返されるのは、次のとおりです。 '' IReceiveData 'の' GetImage 'オペレーションに' width 'という名前のパス変数があります。この変数は' string '型ではありません。 UriTemplateパスセグメントの変数は 'string'型でなければなりません。 –

+1

あなたは正しいですが、 'int'を' string'に変更するのを忘れました。 URLは、パラメータが 'int'か' string'かどうかの概念を持たないので、すべてが 'string'として渡されます。それに応じてコードの値を検証してキャストする必要があります。とにかく、私の解決策は正解です。これは私のアプリケーションでも正しく実行されています:-D –

+0

私の答えをもう一度更新して、もちろん*行の//ハンドルエラー'エラーを処理するいくつかのコードに置き換える必要があります... –

9
UriTemplate = "picture/" 

UriTemplate = "picture?w={width}&h={height}" 

のようなものであるべきこれはURLから幅と高さのパラメータを取得する方法をWCFに指示します。

+0

これは実行されますが、 'http:// localhost:8000/Service/picture?width = 50&height = 40'に移動しようとすると、この行にエラーが表示されます:' Bitmap bitmap = new Bitmap(width、height); ' **パラメータは無効です** –

+1

これは '{width}'と '{height}'が文字列で、あなたのメソッドが 'int'を期待しているからです。下の私のソリューションを使用すると、URLがBargのものよりもきれいに見え、b)文字列を 'int'にキャストした後に動作します。 –

+0

また、URLとUriTemplateの間に一貫性があることに注意してください。 UriTemplateの上には 'w'と 'h'という2つのパラメータが指定されていますが、URLには 'width'と 'height'というパラメータがあります。正しいURLは、http:// localhost:8000/Service/picture?w = 50&h = 40です。 – Opsimath