c# – body参数’width’. GET操作不能拥有一个机构?

前端之家收集整理的这篇文章主要介绍了c# – body参数’width’. GET操作不能拥有一个机构?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从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();

我收到此错误

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方法,或者如何获取图像并将其返回.在这个例子中,有人可以发布正确的方式来显示图像.

编辑

如果我尝试以下答案,并在导航到http://www.localhost.com:8000/Service/picture?width=时使用UriTemplate =“picture?w = {width}& h = {height}”作为我的UriTemplate 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,(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参数.

目前,运行时假定您在没有参数的情况下调用URL,但被调用方法需要参数,因此运行时实际上不知道如何找到要传递给宽度和高度的方法的值.

这看起来像

[WebInvoke(Method = "GET",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.

猜你在找的C#相关文章