jQuery success callback called with empty response when WCF method throws an Exception

前端 未结 5 1850
既然无缘
既然无缘 2021-02-05 11:55

I\'m tearing my hair out over this one, so bear with me (it\'s a long post).

Basic Info

  • ASP.NET 3.5 with WCF service in ASP.NET compatibility mode
5条回答
  •  一个人的身影
    2021-02-05 12:02

    I had the same symptoms with a different scenario so this may or may not help.

    Here is a breif summary of what I was doing and our solution:

    I was posting to a REST implementation of a WCF service that we host from a classic ASP page. I found I had to set the input as a stream and read from that, disposing of the stream when done. I beleive it was at this point I was getting the 202 response with text of "success" as you have described. I discovered that by not disposing of the stream I was getting the response I was expecting for error conditions.

    Here is a summary of the final code:

    [WebHelp(Comment="Expects the following parameters in the post data:title ...etc")] 
        public int SaveBook(Stream stream)
        {
            NameValueCollection qString;
            StreamReader sr = null;
            string s;
            try
            {
                /**************************************************************************************
                 * DO NOT CALL DISPOSE ON THE STREAMREADER OR STREAM                                  *
                 * THIS WILL CAUSE THE ERROR HANDLER TO RETURN A PAGE STATUS OF 202 WITH NO CONTENT   *
                 * IF THERE IS AN ERROR                                                               *
    
                 * ***********************************************************************************/
                sr = new StreamReader(stream);
                s = sr.ReadToEnd();
                qString = HttpUtility.ParseQueryString(s);
    
                string title = qString["title"];
    
                //Do what we need
    
                //Then Return something
                int retRecieptNum = UtilitiesController.SubmitClientEntryRequest(entryReq);                
    
                return retRecieptNum;
            }
            catch (Exception ex)
            {
                throw new WebProtocolException(System.Net.HttpStatusCode.Forbidden, ex.Message, this.GetExceptionElement(true, "BookRequest", ex.Message), false, ex);
            }
            finally
            {
    
            }            
        }
    

    Hopefully this is some help to you, maybe try using a stream and see how that goes.

提交回复
热议问题