等号数据“导致”BeginGetRequeststream失败

时间:2013-09-24 18:19:32

标签: c# asynchronous

下面代码中标记的行(//这是ASYNC CALL)。如果我将此值作为查询传递给我的函数,则不会调用异步调用:

<Verb>get</Verb><ResourceList><CatalogQuery><ItemQueryList><ItemIDList><ID>16-=-cedar-bonsai</ID></ItemIDList><AttributesType>all</AttributesType></ItemQueryList></CatalogQuery>

注意xml的这一部分:

<ID>16-=-cedar-bonsai</ID>

然而,如果我将等号切换到其他任何东西

,它就可以工作
<Verb>get</Verb><ResourceList><CatalogQuery><ItemQueryList><ItemIDList><ID>16-here-cedar-bonsai</ID></ItemIDList><AttributesType>all</AttributesType></ItemQueryList></CatalogQuery>

真正让我感到困惑的是,查询的内容应该绝对不会影响被调用的回调。正确?

这是代码

//Get get a item attributes Async
        public XElement makeAsyncCall(string query,bool create = false,bool resent = false)
        {

            ManualResetEvent mre = null;
            //The results will be stored here;
            StringBuilder xresults = new StringBuilder() ;

            sem.WaitOne();
            try
            {
                query = query.Replace("&", "&amp;");
                query = query.Replace("#39;", "apos;");

                //If this is not a query that is being sent again append additional info
                if (!resent)
                {
                    query = HEADER_TEXT + query + CLOSER_TEXT;
                }

                //Concatonate a declaration, header information, the query to be performed, and the closing XML tag. Then convert to a Byte array
                Byte[] byteData = UTF8Encoding.UTF8.GetBytes(
                            new XDocument(
                                new XDeclaration("1.0", "utf-8", null),
                                XElement.Parse(query)
                            ).ToString()
                );

                string paddr = POST_ADDRESS_QUERY;
                if (create != false)
                {
                    paddr = POST_ADDRESS_CREATION;
                }

                //Setup the post connection 
                HttpWebRequest httpWReq = WebRequest.Create(paddr) as HttpWebRequest;
                httpWReq.Method = "POST";
                httpWReq.ContentType = "application/x-www-form-urlencoded";
                httpWReq.Proxy = null;
                httpWReq.ContentLength = byteData.Length;
                httpWReq.KeepAlive = true;


                using (mre = new ManualResetEvent(false))
                {


                     //THIS IS THE ASYNC CALL
                     httpWReq.BeginGetRequestStream(new AsyncCallback(getRequestStreamAsync), Tuple.Create<HttpWebRequest, byte[], bool, StringBuilder, ManualResetEvent>(httpWReq, byteData, create, xresults, mre));
                    mre.WaitOne(10000);
                    mre.Close();

                    //Try again if not exited already.
                    if (xresults.Length == 0)
                    {
                        httpWReq.Abort();
                        sem.Release();
                        return makeAsyncCall(query, create, true);
                    }

                    sem.Release();
                    return XElement.Parse(xresults.ToString(), LoadOptions.PreserveWhitespace);

                }
            }
            catch(Exception ex)
            {
                mre.Close();
                sem.Release();
                throw (ex);
            }
        }

1 个答案:

答案 0 :(得分:0)

我发现了造成这个问题的原因。回调正在执行,但它没有导致断点停止。今天早上我再次尝试了代码,并且正在抓住断点。我不确定这是视觉工作室中的错误还是未定义的行为?也许其他人在这方面对视觉工作室有更多了解。

请求通过罚款和接收响应但稍后在我的代码中生成我自己的异常,捕获它们,抛出它们,然后重试命令。所以它陷入了无限循环。