0
votes

i am trying to post an image to wcf rest service from silverlight 4 but i am getting an exception when i am taking resonse from the request, the exception is

System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c_DisplayClassa.b_9(Object sendState) at System.Net.Browser.AsyncHelper.<>c_DisplayClass4.b_1(Object sendState) --- End of inner exception stack trace --- at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at Admin.Popups.AddCategory.OnImageGetResponseCompleted(IAsyncResult ar)}

all of my other functions on the service is working fine even this one when i try to access it from android, i already had a look and tried the silverlight cross domain policy but i dont think its applicable, my code is below

private void UpLoadImage()
        {
            string URL = Utilities.GetServiceUrl() + "AddCategoryImage?CategoryId=2&ContentType=image&apikey=" + Utilities.GetApiKey();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "text/plain";
            request.ContentLength = SelectedFile.Length;

            request.BeginGetRequestStream(OnImageGetRequestStreamCompleted, request);
        }

        private void OnImageGetRequestStreamCompleted(IAsyncResult ar)
        {
            try
            {
                if (SelectedFile != null)
                {
                    HttpWebRequest request = (HttpWebRequest)ar.AsyncState;

                    using (Stream postStream = request.EndGetRequestStream(ar))
                    {
                        using (Stream source = SelectedFile.OpenRead())
                        {
                            byte[] buffer = new byte[2048];
                            int bytesRead;
                            while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                postStream.Write(buffer, 0, bytesRead);
                            }
                        }

                   }

                    request.BeginGetResponse(new AsyncCallback(OnImageGetResponseCompleted), request);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void OnImageGetResponseCompleted(IAsyncResult ar)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    string result = sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                this.Dispatcher.BeginInvoke(delegate()
                        {
                            MessageBox.Show(ex.Message);
                        });
            }
        }

the wcf function is

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "AddCategoryImage?CategoryId={CategoryId}&ContentType={ContentType}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        bool AddCategoryImage(int CategoryId, string ContentType, Stream Image);

and function implementation is

 public static bool AddCategoryImage(int CategoryId, string ContentType,Stream Image)
        {
            SqlConnection sqlCon = new SqlConnection(Params.GetConnectionString());
            SqlCommand sqlCmd = new SqlCommand("UPDATE Categories SET Cat_Img = @Cat_Img,Cat_Img_Type = @Cat_Img_Type WHERE Cat_ID = @Cat_ID", sqlCon);
            sqlCmd.Parameters.Add("@Cat_Img", SqlDbType.Image).Value = Utility.ConvertStreamToByteArray(Image);
            sqlCmd.Parameters.Add("@Cat_Img_Type", SqlDbType.NVarChar, 32).Value = ContentType;
            sqlCmd.Parameters.Add("@Cat_ID", SqlDbType.Int).Value = CategoryId;

            try
            {
                sqlCon.Open();

                return sqlCmd.ExecuteNonQuery() > 0 ? true : false;
            }
            catch (Exception ex)
            {
                Error.CreateError(ex.Message, "Category.cs", "AddCategoryImg(int,string,Stream)");
            }
            finally
            {
                sqlCon.Close();
            }

            return false;
        }
1

1 Answers

1
votes

i have solved the issue WCF defualt maxReceivedMessageSize is 65K and i was trying to upload file more than 65k, clever me :) so i set the maxReceivedMessageSize="2000000" to 2MB now it works for files up to 2MB

<binding name ="webBinding" sendTimeout="00:10:00" receiveTimeout="00:10:00" openTimeout="00:10:00"  closeTimeout="00:10:00" maxReceivedMessageSize="2000000">
    <readerQuotas maxDepth="32" maxStringContentLength="100000000" maxArrayLength="100000000" maxBytesPerRead="100000000" maxNameTableCharCount="100000000" />
</binding>