车身参数“宽度”。 GET操作不能有身体吗? [英] body parameter 'width'. GET operations cannot have a body?

查看:220
本文介绍了车身参数“宽度”。 GET操作不能有身体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着从一个WCF休息服务中获得的图像,像这样:

Im trying to get an image from a wcf rest service like so:

[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;
    }
}

在我的主机应用程序:

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();



我得到这个错误:

I get this error:

运行合同IReceiveData使用GET,但也有
体参数'宽'的getImage。 GET操作不能有一个机构。无论是做
参数'宽度'一个UriTemplate参数或交换机从
WebGetAttribute到WebInvokeAttribute。

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.

林不知道您如何设置webinvoke / UriTemplate方法图像或你如何得到的图像并将其返回。有人可以张贴在这个例子来显示图像的正确方法。

Im not sure how you set the webinvoke/UriTemplate method for an image or how you get an image and return it. Can some one post the correct way to display an image in this example.

修改

如果我尝试下面的答案,并使用 UriTemplate =图片W = {}宽度和放大器; H = {height}的<导航到时/ code>作为我UriTemplate http://www.localhost.com:8000 ?/服务/画面宽度= 50安培;高度= 40 我在我的代码收到一个错误:

If I try the below answer and use UriTemplate = "picture?w={width}&h={height}" as my UriTemplate when navigating to http://www.localhost.com:8000/Service/picture?width=50&height=40 I recieve an error in my code:

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是由用户代码未处理: 参数是无效的。

推荐答案

在属性,你需要告诉你期待的宽度和高度为URL参数的运行时间。

In the attributes, you need to tell the runtime that you're expecting the width and height as an URL parameter.

目前,运行时假定你调用一个不带参数的URL,但被调用的方法需要的参数,所以运行时真的不知道如何找到价值传递给你的方法宽度高度

At the moment, the runtime assumes that you're calling the URL without parameters, but the method being called expects parameters, so the runtime really doesn't know how to find the values to pass to your method for width and 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

这篇关于车身参数“宽度”。 GET操作不能有身体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆