I am trying to develop a REST Service using the kit to send and receive images. From wat i have researched and understood ,it seems i can only do so using stream of bytes.
Am i right? if so can anyone explain or list a simple sample for me which clearly show the usage.
If i am not. wat is the best way to handle images... is it as a stream or as a xml serializable or as json ?..I am quite confused . Any help would be really appreciated.
Yeah, you want to use streaming. There's a great blog posting
here about WCF Rest over ASMX services, but in there is a good example of streaming an image in a service operation. (Just search the page for the text, "image" and you should see it.)
<div>
</div><div>But I end up with a system.stackoverflowexception in the bold line. I am unable to figure out what could be wrong?</div><div>Do you think You can help me with that.?</div>
I see nothing wrong with your code. If you could share the stack trace and more of the code, perhaps we can be of more help... you're probably calling yourself in a recursive loop.
I have attached the class definition which also contains the above defined method. This Class is being called in the OnGet() of the WCF rest Starter Kit service.
Because "image" is the name of your public property, you are calling both the SET and GET accessors within your GET accessor. Therefore, you have an infinite loop. Replace your GET accessor with:
hi, I tried that and this created the same type of error as my last post suggest when i replace the
get
{
image = GetImage();
return image;
}
with
get
{
FileStream stream = File.OpenRead(@"c:\BK2.jpg");
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return stream as Stream;
}
I believe wat you suggest and what i have written above create the same situation. I have tried both the codes and now I am not able to see any output in the internet explorer. It says "internet Explorer cannot display the page" and therefore i opened Fiddler and wanted to verify . Fiddler gave me a response
Http 307 as the result which i found out as a redirection error.
I am lost. I thought my code was a simple one to merely display an image from the hard disk on the webpage.
I have figured out the solution to the 307 error... I had to change the address at the get request builder in Fiddler to end with a backslash. Basically
convert
http://localhost:1883/Service.svc
to
http://localhost:1883/Service.svc/
. However this hasn't solved my initial problem. I still don't see any image in the Fiddler GET request. I now get a HTTP 0 status for my Get request.
Is there a way to resolve this?
I am planning to shift to raw programming model of the WCF REST and not use thee starter kit . This will probably provide me more control over the service contract and item types. I tried debuggin and found out that the error was not in reading the image
file. It was read perfectly into the filestream but the function was unable to return the filestream as a stream object. There was some mention of
> Microsoft.ServiceModel.Web.DLL!Microsoft.ServiceModel.Web.WebErrorHandler.HandleError(System.Exception error = {"Type 'System.IO.FileStream' with data contract name 'FileStream:http://schemas.datacontract.org/2004/07/System.IO' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer."}) Line 29
C#
Your return type is Sample--but if you are returning an image, your return type needs to be "stream". Don't use the WCF REST starter kit templates for this. The templates are for returning structured data: Xml and Json. You want to return unstructured data--raw
bytes. Just create a plain WCF REST service with:
snehitn
Member
1 Points
8 Posts
Image Handling using REST starter kit
Sep 16, 2009 11:22 PM|LINK
I am trying to develop a REST Service using the kit to send and receive images. From wat i have researched and understood ,it seems i can only do so using stream of bytes.
Am i right? if so can anyone explain or list a simple sample for me which clearly show the usage.
If i am not. wat is the best way to handle images... is it as a stream or as a xml serializable or as json ?..I am quite confused . Any help would be really appreciated.
Rest Starter Kit Image Handling WCF
James Osborn...
Member
70 Points
30 Posts
Re: Image Handling using REST starter kit
Sep 18, 2009 04:58 PM|LINK
Yeah, you want to use streaming. There's a great blog posting here about WCF Rest over ASMX services, but in there is a good example of streaming an image in a service operation. (Just search the page for the text, "image" and you should see it.)
[In case the above link is broken, http://www.west-wind.com/WebLog/posts/310747.aspx]
snehitn
Member
1 Points
8 Posts
Re: Image Handling using REST starter kit
Oct 09, 2009 10:08 PM|LINK
So i tried this.
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public Stream GetImage()</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> FileStream stream = File.OpenRead(@"c:\BK2.jpg");</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return stream as Stream;</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div></div>public Stream GetImage()
{
FileStream stream = File.OpenRead(@"c:\BK2.jpg");
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return stream as Stream;
}
<div></div><div>But I end up with a system.stackoverflowexception in the bold line. I am unable to figure out what could be wrong?</div><div>Do you think You can help me with that.?</div>
ranamauro
Member
44 Points
12 Posts
Re: Image Handling using REST starter kit
Oct 10, 2009 02:52 AM|LINK
I see nothing wrong with your code. If you could share the stack trace and more of the code, perhaps we can be of more help... you're probably calling yourself in a recursive loop.
snehitn
Member
1 Points
8 Posts
Re: Image Handling using REST starter kit
Oct 12, 2009 04:06 PM|LINK
Hi,
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public class Sample</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public Stream image</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> get{</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> image = GetImage();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return image;</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> set</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div>I have attached the class definition which also contains the above defined method. This Class is being called in the OnGet() of the WCF rest Starter Kit service.
public class Sample
{
public Stream image
{
get{
image = GetImage();
return image;
}
set
{
}
public Stream GetImage()
{
FileStream stream = File.OpenRead(@"c:\BK2.jpg");
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return stream as Stream;
}
<div>}</div><div></div><div>the onget () function looks like this.</div><div><div> protected override Sample OnGetItem()</div><div> {</div><div> return this.item;</div><div> }</div></div>
Any help will be appreciated.
Thanks
snehitn
Member
1 Points
8 Posts
Re: Image Handling using REST starter kit
Oct 12, 2009 05:19 PM|LINK
I tried inserting the image retreive code in the get itself. and now i get a http 307 error in the Internet browser.
randallt
Member
108 Points
34 Posts
Re: Image Handling using REST starter kit
Oct 12, 2009 06:17 PM|LINK
I think your problem is in the GET accessor:
get { image = GetImage(); return image; }Because "image" is the name of your public property, you are calling both the SET and GET accessors within your GET accessor. Therefore, you have an infinite loop. Replace your GET accessor with:
get { return GetImage(); }snehitn
Member
1 Points
8 Posts
Re: Image Handling using REST starter kit
Oct 12, 2009 06:59 PM|LINK
hi, I tried that and this created the same type of error as my last post suggest when i replace the
with
get { FileStream stream = File.OpenRead(@"c:\BK2.jpg"); WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; return stream as Stream; }I believe wat you suggest and what i have written above create the same situation. I have tried both the codes and now I am not able to see any output in the internet explorer. It says "internet Explorer cannot display the page" and therefore i opened Fiddler and wanted to verify . Fiddler gave me a response
Http 307 as the result which i found out as a redirection error.
I am lost. I thought my code was a simple one to merely display an image from the hard disk on the webpage.
Any Help would be sincerely appreciated.
snehitn
Member
1 Points
8 Posts
Re: Image Handling using REST starter kit
Oct 12, 2009 11:11 PM|LINK
I have figured out the solution to the 307 error... I had to change the address at the get request builder in Fiddler to end with a backslash. Basically
convert
http://localhost:1883/Service.svc
to
http://localhost:1883/Service.svc/
. However this hasn't solved my initial problem. I still don't see any image in the Fiddler GET request. I now get a HTTP 0 status for my Get request.
Is there a way to resolve this?
I am planning to shift to raw programming model of the WCF REST and not use thee starter kit . This will probably provide me more control over the service contract and item types. I tried debuggin and found out that the error was not in reading the image file. It was read perfectly into the filestream but the function was unable to return the filestream as a stream object. There was some mention of
> Microsoft.ServiceModel.Web.DLL!Microsoft.ServiceModel.Web.WebErrorHandler.HandleError(System.Exception error = {"Type 'System.IO.FileStream' with data contract name 'FileStream:http://schemas.datacontract.org/2004/07/System.IO' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer."}) Line 29 C#Any advise or suggestions are always welcome!
randallt
Member
108 Points
34 Posts
Re: Image Handling using REST starter kit
Oct 13, 2009 04:18 PM|LINK
protected override Sample OnGetItem()
{
return this.item;
}
Your return type is Sample--but if you are returning an image, your return type needs to be "stream". Don't use the WCF REST starter kit templates for this. The templates are for returning structured data: Xml and Json. You want to return unstructured data--raw bytes. Just create a plain WCF REST service with:
[OperationContract] [WebGet(UriTemplate ="<Your UriTemplate Here>")] public Stream GetImage() { FileStream stream = File.OpenRead(@"c:\BK2.jpg"); WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; return stream; }