2

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

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

1 回答 1

0

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

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

于 2013-09-25T16:35:15.487 回答