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(); 我收到此错误:
我不知道你如何为图像设置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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |