The parameter "width" of the body. GET operations cannot have a body?

I am trying to get an image from wcf support as follows:

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

In my host application:

 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:

The GetImage operation in the IReceiveData contract uses a GET, but also has a body width parameter. GET operations cannot have a body. Either make the parameter 'width' the parameter UriTemplate or switch from WebGetAttribute to WebInvokeAttribute.

I'm not sure how you set the webinvoke / UriTemplate method for the image or how you get the image and return it. Can anyone post the correct way to display the image in this example.

EDIT

If I try to answer below and use UriTemplate = "picture?w={width}&h={height}" as my UriTemplate when navigating http://www.localhost.com:8000/Service/picture?width=50&height=40 I get 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; } 

What states ArguementException was unhandled by user code: The parameter is not valid.

+6
source share
2 answers

In the attributes you need to tell runtime that you expect the width and height as the URL parameter.

Currently, the runtime assumes that you are calling the URL without parameters, but the called method expects parameters, so the runtime does not know how to find the values ​​for your method for width and height .

It might look like

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

and you will need to call a url like http://test.tld/picture/320/200 .

+11
source
 UriTemplate = "picture/" 

It should be something like

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

This tells WCF how to get the width and height parameters from the URL.

+9
source

Source: https://habr.com/ru/post/912395/


All Articles