2012-11-07 61 views
0

我想從Silverlight 4發佈圖像到wcf休息服務,但我得到一個異常,當我從請求中反應,例外是System.Net.WebException:遠程服務器返回錯誤:NotFound。 ---> silverlight文件發佈到WCF休息服務

系統.Net.WebException:遠程服務器返回錯誤:NotFound。 ---> System.Net.WebException:遠程服務器返回一個錯誤: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(對象sendState)內部異常堆棧跟蹤的 --- ---結束在 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,對象狀態) 在System.Net。 Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult的asyncResult) 在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; 
     } 

回答

1

我已經解決了WCF defualt 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> 
相關問題