इम इतना की तरह एक 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;
}
}
मेरी मेजबान आवेदन में:
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.
इम यकीन नहीं:
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 विधि कैसे सेट करते हैं या आप कैसे छवि प्राप्त करते हैं और इसे वापस करते हैं। क्या कोई इस उदाहरण में एक छवि प्रदर्शित करने के लिए सही तरीका पोस्ट कर सकता है।
संपादित
अगर मैं नीचे इस सवाल का जवाब कोशिश करते हैं और मेरी UriTemplate रूप UriTemplate = "picture?w={width}&h={height}"
का उपयोग करते हैं 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:
पैरामीटर मान्य नहीं है ।
आपकी विधि वापस आती है: 'ऑपरेशन' GetImage 'अनुबंध' IReceiveData 'में' चौड़ाई 'नामक एक पथ चर है जिसमें टाइप' स्ट्रिंग 'नहीं है। UriTemplate पथ खंडों के लिए वेरिएबल्स में 'string' टाइप होना चाहिए। –
आप सही हैं, मैं 'int' को 'स्ट्रिंग' में बदलना भूल गया। एक यूआरएल में कोई अवधारणा नहीं है कि पैरामीटर एक 'int' या' स्ट्रिंग 'है, इसलिए सब कुछ' स्ट्रिंग 'के रूप में पास किया गया है और आपको तदनुसार अपने कोड में मानों को सत्यापित और डालने की आवश्यकता है। वैसे भी, मेरा समाधान सही उत्तर है, क्योंकि मेरे पास यह भी मेरे अनुप्रयोगों में से एक में चल रहा है :- डी –
ठीक है, मैं यह स्पष्ट करने के लिए फिर से अपना उत्तर अपडेट करूंगा कि * निश्चित रूप से * रेखा '// हैंडल त्रुटि त्रुटि को संभालने के लिए कुछ कोड द्वारा प्रतिस्थापित किया जाना चाहिए ... –