body参数'width'.GET操作不能拥有一个机构?

G G*_* Gr 6 c# rest wcf web-services

我试图从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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的主机应用程序:

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();
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

合同'IReceiveData'中的'GetImage'操作使用GET,但也有body参数'width'.GET操作不能有一个正文.将参数'width'设置为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;
        }
Run Code Online (Sandbox Code Playgroud)

哪些状态ArguementException was unhandled by user code:参数无效.

Tho*_*mar 12

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

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

这看起来像

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

    ....
}
Run Code Online (Sandbox Code Playgroud)

你需要像这样调用URL http://test.tld/picture/320/200.


Bar*_*arg 10

UriTemplate = "picture/"
Run Code Online (Sandbox Code Playgroud)

应该是这样的

UriTemplate = "picture?w={width}&h={height}"
Run Code Online (Sandbox Code Playgroud)

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