0

我正在尝试从 silverlight 4 将图像发布到 wcf 休息服务,但是当我从请求中获取响应时出现异常,异常是

System.Net.WebException:远程服务器返回错误:NotFound。---> System.Net.WebException:远程服务器返回错误:NotFound。在 System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.BrowserHttpWebRequest.<>c_ DisplayClassa.b _9(Object sendState) 在 System.Net.Browser.AsyncHelper.<>c_ DisplayClass4.b _1 (Object sendState) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) 在 Admin。 Popups.AddCategory.OnImageGetResponseCompleted(IAsyncResult ar)}

当我尝试从android访问它时,我在服务上的所有其他功能都可以正常工作,即使是这个功能,我已经看过并尝试了silverlight跨域策略,但我认为它不适用,我的代码如下

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);
                        });
            }
        }

wcf 函数是

[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);

和功能实现是

 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;
        }
4

1 回答 1

1

我已经解决了 WCF 默认 maxReceivedMessageSize 为 65K 的问题,我试图上传超过 65k 的文件,聪明的我 :) 所以我将 maxReceivedMessageSize="2000000" 设置为 2MB 现在它适用于最大 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>
于 2012-11-07T09:44:46.203 回答