6

我试图从 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,但也有主体参数“width”。GET 操作不能有主体。将参数“宽度”设为 UriTemplate 参数,或从 WebGetAttribute 切换到 WebInvokeAttribute。

我不确定您如何为图像设置 webinvoke/UriTemplate 方法或如何获取图像并返回它。有人可以在此示例中发布显示图像的正确方法吗?

编辑

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

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

4

2 回答 2

13

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

目前,运行时假定您正在调用不带参数的 URL,但被调用的方法需要参数,因此运行时真的不知道如何找到要传递给您的方法的值 forwidthheight

这看起来像

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

    ....
}

您将需要调用 URL,例如http://test.tld/picture/320/200.

于 2012-04-04T14:09:51.957 回答
10
UriTemplate = "picture/"

应该是这样的

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

这告诉 WCF 如何从 URL 中获取宽度和高度参数。

于 2012-04-04T14:11:27.053 回答