身体参数'宽度'。 GET操作不能拥有一个机构?

时间:2012-04-04 14:04:07

标签: c# wcf web-services rest

我试图从wcf休息服务中获取图像,如下所示:

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

我收到此错误:

  

合同'IReceiveData'中的'GetImage'操作使用GET,但也有   body参数'width'。 GET操作不能有一个正文。要么做   参数'width'是UriTemplate参数,或从中切换   WebGetAttribute到WebInvokeAttribute。

我不确定如何为图像设置webinvoke / UriTemplate方法,或者如何获取图像并将其返回。在这个例子中,有人可以发布正确的方式来显示图像。

修改

如果我尝试以下答案并在导航到UriTemplate = "picture?w={width}&h={height}"时使用http://www.localhost.com:8000/Service/picture?width=50&height=40作为我的UriTemplate,我会在代码中收到错误:

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: 参数无效。

2 个答案:

答案 0 :(得分:12)

在属性中,您需要告诉运行时您期望宽度和高度作为URL参数。

目前,运行时假定您在没有参数的情况下调用URL,但是被调用的方法需要参数,因此运行时实际上不知道如何找到要传递给{{1的方法的值的值}和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; } .... } 等网址。

答案 1 :(得分:10)

UriTemplate = "picture/"

应该像

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

这告诉WCF如何从URL获取width和height参数。