Image Handling using REST starter kithttp://forums.asp.net/t/1471505.aspx/1?Image+Handling+using+REST+starter+kitTue, 13 Oct 2009 18:52:52 -040014715053409459http://forums.asp.net/p/1471505/3409459.aspx/1?Image+Handling+using+REST+starter+kitImage Handling using REST starter kit <p>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.&nbsp;</p> <p>Am i right? if so can anyone explain or list a simple sample for me which clearly show the usage.</p> <p>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.</p> <p><br> </p> 2009-09-16T23:22:33-04:003413400http://forums.asp.net/p/1471505/3413400.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p>&nbsp;Yeah, you want to use streaming.&nbsp; There's a great blog posting <a href="http://www.west-wind.com/WebLog/posts/310747.aspx" target="_blank"> here</a> about WCF Rest over ASMX services, but in there is a good example of streaming an image in a service operation.&nbsp; (Just search the page for the text, &quot;image&quot; and you should see it.)</p> <p>&nbsp;</p> <p>[In case the above link is broken, <a href="http://www.west-wind.com/WebLog/posts/310747.aspx"> http://www.west-wind.com/WebLog/posts/310747.aspx</a>]</p> 2009-09-18T16:58:20-04:003450998http://forums.asp.net/p/1471505/3450998.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p>So i tried this.</p> <p></p> &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;public Stream GetImage()&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FileStream stream = File.OpenRead(@&quot;c:\BK2.jpg&quot;);&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WebOperationContext.Current.OutgoingResponse.ContentType = &quot;image/jpeg&quot;;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return stream as Stream;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&lt;/div&gt; &lt;div&gt;&lt;/div&gt; <p></p> <p>&nbsp;public Stream GetImage()</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>&nbsp;FileStream stream = File.OpenRead(@&quot;c:\BK2.jpg&quot;);</b></p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WebOperationContext.Current.OutgoingResponse.ContentType = &quot;image/jpeg&quot;;</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return stream as Stream;</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</p> &lt;div&gt;<br> &lt;/div&gt;&lt;div&gt;But I end up with a system.stackoverflowexception in the bold line. &nbsp;I am unable to figure out what could be wrong?&lt;/div&gt;&lt;div&gt;Do you think You can help me with that.?&lt;/div&gt; <p></p> <p></p> 2009-10-09T22:08:52-04:003451152http://forums.asp.net/p/1471505/3451152.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p>I see nothing wrong with your code.&nbsp;If you could share&nbsp;the stack trace and&nbsp;more of the code, perhaps we can be of more help... you're probably calling yourself in a recursive loop.</p> 2009-10-10T02:52:50-04:003454152http://forums.asp.net/p/1471505/3454152.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p>Hi,</p> <p></p> &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;public class Sample&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp;{&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;public Stream image&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;get{&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;image = GetImage();&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return image;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;set&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&lt;/div&gt; <p>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.</p> <p></p> <p>public class Sample</p> <p>&nbsp;&nbsp; {</p> <p>&nbsp;public Stream image</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;get{</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;image = GetImage();</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return image;</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;set</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public Stream GetImage()</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FileStream stream = File.OpenRead(@&quot;c:\BK2.jpg&quot;);</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WebOperationContext.Current.OutgoingResponse.ContentType = &quot;image/jpeg&quot;;</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return stream as Stream;</p> <p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</p> &lt;div&gt;}&lt;/div&gt;&lt;div&gt;<br> &lt;/div&gt;&lt;div&gt;the onget () function looks like this.&lt;/div&gt;&lt;div&gt;&lt;div&gt;&nbsp;&nbsp; protected override Sample OnGetItem()&lt;/div&gt;&lt;div&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{&lt;/div&gt;&lt;div&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return this.item;&lt;/div&gt;&lt;div&gt;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&lt;/div&gt;&lt;/div&gt; <p></p> <p><br> </p> <p>Any help will be appreciated.</p> <p>Thanks</p> <p></p> 2009-10-12T16:06:25-04:003454272http://forums.asp.net/p/1471505/3454272.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p style="padding-left:60px">I tried inserting the image&nbsp;retreive code in the get itself.&nbsp;&nbsp;and now i get a http 307 error in the Internet browser.</p> <p style="padding-left:60px">&nbsp;</p> <p style="padding-left:60px">&nbsp;</p> 2009-10-12T17:19:52-04:003454376http://forums.asp.net/p/1471505/3454376.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p>I think your problem is in the GET accessor:</p> <pre class="prettyprint">get { image = GetImage(); return image; }</pre> <P><BR>Because "image" is the name of your public property, you are calling both the SET and GET accessors within your GET accessor.&nbsp; Therefore, you have an infinite loop.&nbsp; Replace your GET accessor with: </P><pre class="prettyprint">get { return GetImage(); }</pre> <p><br> &nbsp;</p> 2009-10-12T18:17:06-04:003454439http://forums.asp.net/p/1471505/3454439.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p>hi, I tried that &nbsp;and this created the same type of error as my last post suggest when i replace the&nbsp;</p> <ol start="1" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:1px!important; margin-left:45px!important; list-style-type:decimal; list-style-position:initial; background-color:#ffffff; color:#5c5c5c; padding:0px; border:initial none initial"> <li style="font-size:11px; padding-top:0px!important; padding-right:3px!important; padding-bottom:0px!important; padding-left:10px!important; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:solid; border-width:initial; border-color:initial; list-style-type:decimal-leading-zero; list-style-position:outside!important; border-left-width:3px; border-left-color:#6ce26c; background-color:#ffffff; color:inherit; line-height:14px; margin:0px!important"> <span style="color:black; background-color:inherit; padding:0px; margin:0px; border:initial none initial"><span style="color:#006699; background-color:inherit; font-weight:bold; padding:0px; margin:0px; border:initial none initial">get</span><span style="color:black; background-color:inherit; padding:0px; margin:0px; border:initial none initial">&nbsp;&nbsp;&nbsp;</span></span> </li><li style="font-size:11px; padding-top:0px!important; padding-right:3px!important; padding-bottom:0px!important; padding-left:10px!important; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:solid; border-width:initial; border-color:initial; list-style-type:decimal-leading-zero; list-style-position:outside!important; border-left-width:3px; border-left-color:#6ce26c; background-color:#f8f8f8; color:#5c5c5c; line-height:14px; margin:0px!important"> <span style="color:black; background-color:inherit; padding:0px; margin:0px; border:initial none initial">{&nbsp;&nbsp;&nbsp;</span> </li><li style="font-size:11px; padding-top:0px!important; padding-right:3px!important; padding-bottom:0px!important; padding-left:10px!important; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:solid; border-width:initial; border-color:initial; list-style-type:decimal-leading-zero; list-style-position:outside!important; border-left-width:3px; border-left-color:#6ce26c; background-color:#ffffff; color:inherit; line-height:14px; margin:0px!important"> <span style="color:black; background-color:inherit; padding:0px; margin:0px; border:initial none initial">&nbsp;&nbsp;&nbsp;&nbsp;image&nbsp;=&nbsp;GetImage();&nbsp;&nbsp;</span> </li><li style="font-size:11px; padding-top:0px!important; padding-right:3px!important; padding-bottom:0px!important; padding-left:10px!important; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:solid; border-width:initial; border-color:initial; list-style-type:decimal-leading-zero; list-style-position:outside!important; border-left-width:3px; border-left-color:#6ce26c; background-color:#f8f8f8; color:#5c5c5c; line-height:14px; margin:0px!important"> <span style="color:black; background-color:inherit; padding:0px; margin:0px; border:initial none initial">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006699; background-color:inherit; font-weight:bold; padding:0px; margin:0px; border:initial none initial">return</span><span style="color:black; background-color:inherit; padding:0px; margin:0px; border:initial none initial">&nbsp;image;&nbsp;&nbsp;</span></span> </li><li style="font-size:11px; padding-top:0px!important; padding-right:3px!important; padding-bottom:0px!important; padding-left:10px!important; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:solid; border-width:initial; border-color:initial; list-style-type:decimal-leading-zero; list-style-position:outside!important; border-left-width:3px; border-left-color:#6ce26c; background-color:#ffffff; color:inherit; line-height:14px; margin:0px!important"> <span style="color:black; background-color:inherit; padding:0px; margin:0px; border:initial none initial">}&nbsp;</span></li></ol> <p><span class="Apple-style-span" style="line-height:14px; font-size:small"><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="line-height:normal">with&nbsp;</span></font></span></p> <p><span class="Apple-style-span" style="line-height:14px; font-size:small"><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="line-height:normal"></span></font></span></p> <p><font class="Apple-style-span" size="3"></font></p> <p><font class="Apple-style-span" size="3"><pre class="prettyprint">get { FileStream stream = File.OpenRead(@&quot;c:\BK2.jpg&quot;); WebOperationContext.Current.OutgoingResponse.ContentType = &quot;image/jpeg&quot;; return stream as Stream; }</pre></font></p><p></p><p><br></p><p><span class="Apple-style-span" style="font-size: small; " mce_fixed="1"><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="" mce_fixed="1">&nbsp;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</span></font></span></p><p><span class="Apple-style-span" style="font-size: small; " mce_fixed="1"><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="" mce_fixed="1">Http 307 as the result &nbsp;which i found out as a redirection error. </span></font></span></p><p><span class="Apple-style-span" style="font-size: small; " mce_fixed="1"><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="" mce_fixed="1">I am lost. I thought my code was a simple one to merely display an image from the hard disk on the webpage.&nbsp;</span></font></span></p><p><span class="Apple-style-span" style="font-size: small; " mce_fixed="1">Any Help would be sincerely appreciated.</span></p><p><br></p><p><span class="Apple-style-span" style="font-size: x-small; " mce_fixed="1"><br></span></p><p><span class="Apple-style-span" style="font-size: small; " mce_fixed="1"><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="" mce_fixed="1">&nbsp;<br>&nbsp;</span></font></span></p><p></p><p><font class="Apple-style-span" size="3"></font></p><p><font class="Apple-style-span" size="3"></font></p><p><font class="Apple-style-span" size="3"><pre class="prettyprint"> </pre><br> <br> </font></p> <p></p> <p></p> <p></p> <p><br> </p> <ol start="1" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:1px!important; margin-left:45px!important; list-style-type:decimal; list-style-position:initial; background-color:#ffffff; color:#5c5c5c; padding:0px; border:initial none initial"> </ol> 2009-10-12T18:59:59-04:003454725http://forums.asp.net/p/1471505/3454725.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p><br> </p> <p>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&nbsp;</p> <p>convert </p> <p>http://localhost:1883/Service.svc </p> <p>to </p> <p>http://localhost:1883/Service.svc/</p> <p>. 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.</p> <p>Is there a way to resolve this? </p> <p>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. &nbsp;There was some mention of </p> <pre class="prettyprint">&gt; Microsoft.ServiceModel.Web.DLL!Microsoft.ServiceModel.Web.WebErrorHandler.HandleError(System.Exception error = {&quot;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.&quot;}) Line 29 C#</pre> <p><br> Any advise or suggestions are always welcome!</p> <p></p> 2009-10-12T23:11:01-04:003456167http://forums.asp.net/p/1471505/3456167.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p>protected override Sample OnGetItem() <br> {<br> &nbsp;&nbsp; return this.item;<br> }</p> <p>Your return type is Sample--but if you are returning an image, your return type needs to be &quot;stream&quot;.&nbsp; Don't use the WCF REST starter kit templates for this.&nbsp; The templates are for returning structured data: Xml and Json.&nbsp; You want to return unstructured&nbsp;data--raw bytes.&nbsp; Just create a plain WCF REST service with:</p> <pre class="prettyprint">[OperationContract] [WebGet(UriTemplate =&quot;&lt;Your UriTemplate Here&gt;&quot;)] public Stream GetImage() { FileStream stream = File.OpenRead(@&quot;c:\BK2.jpg&quot;); WebOperationContext.Current.OutgoingResponse.ContentType = &quot;image/jpeg&quot;; return stream; }</pre> <p><br> <br> &nbsp;</p> 2009-10-13T16:18:39-04:003456278http://forums.asp.net/p/1471505/3456278.aspx/1?Re+Image+Handling+using+REST+starter+kitRe: Image Handling using REST starter kit <p>Thanks a Ton Guys!</p> <p><br> Finally was able to solve the problem. Now I can clearly see the image. &nbsp;As Randalt Suggested i tried the raw programming model instead of the Starter Kit and that gave me good amount of control over the Data Contract.</p> <p>Special thanks to Randalt.&nbsp;</p> 2009-10-13T18:52:52-04:00