イムはそうのような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:
パラメータが有効ではないと述べ。
あなたのメソッドから返されるのは、次のとおりです。 '' IReceiveData 'の' GetImage 'オペレーションに' width 'という名前のパス変数があります。この変数は' string '型ではありません。 UriTemplateパスセグメントの変数は 'string'型でなければなりません。 –
あなたは正しいですが、 'int'を' string'に変更するのを忘れました。 URLは、パラメータが 'int'か' string'かどうかの概念を持たないので、すべてが 'string'として渡されます。それに応じてコードの値を検証してキャストする必要があります。とにかく、私の解決策は正解です。これは私のアプリケーションでも正しく実行されています:-D –
私の答えをもう一度更新して、もちろん*行の//ハンドルエラー'エラーを処理するいくつかのコードに置き換える必要があります... –