C# 提交请求后,哪些操作无法执行?

C# 提交请求后,哪些操作无法执行?,c#,asp.net-web-api,httpwebrequest,httpwebresponse,compact-framework2.0,C#,Asp.net Web Api,Httpwebrequest,Httpwebresponse,Compact Framework2.0,我成功地在手持设备(Compressed Framework/Windows CE)和现代(不像Windows 8中的“现代”,而是21世纪的)服务器应用程序(Web API)之间传递数据。但是,(成功)传递数据后,客户端失败,错误消息为“在提交请求后无法执行此操作” 它在抱怨哪种操作?这是我的客户代码: public static HttpWebResponse SendXMLFile(string xmlFilepath, string uri) { StringBuilder s

我成功地在手持设备(Compressed Framework/Windows CE)和现代(不像Windows 8中的“现代”,而是21世纪的)服务器应用程序(Web API)之间传递数据。但是,(成功)传递数据后,客户端失败,错误消息为“在提交请求后无法执行此操作”

它在抱怨哪种操作?这是我的客户代码:

public static HttpWebResponse SendXMLFile(string xmlFilepath, string uri) 
{
    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = new StreamReader(xmlFilepath))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.AppendLine(line);
        }
    }

    string strData = @sb.ToString(); 
    strData = strData.Replace("\"", "'"); 
    string body = String.Format("\"{0}\"", strData);
    HttpWebRequest request = CreateRequestNoCredentials(uri, HttpMethods.POST, body, "application/json"); 

    MessageBox.Show("Made it past the call to CreateRequestNoCredentials()");
    try
    {
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            MessageBox.Show(string.Format("ReadResponse val = {0}", RESTfulMethods.ReadResponse(response)));
            return response;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(string.Format("Msg = {0}; StackTrace = {1}", ex.Message, ex.StackTrace));
        request.Abort();
        return null;
    }
}

public static HttpWebRequest CreateRequestNoCredentials(string uri, HttpMethods method, string data, string contentType)
{
    //test:
    MessageBox.Show(string.Format("In CreateRequestNoCredentials(); data passed in = {0}", data));

    WebRequest request = HttpWebRequest.Create(uri);
    request.Method = Enum.ToObject(typeof(HttpMethods), method).ToString();
    request.ContentType = contentType;
    ((HttpWebRequest)request).Accept = contentType;
    ((HttpWebRequest)request).KeepAlive = false;
    ((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;

    if (method != HttpMethods.GET && method != HttpMethods.DELETE)
    {
        Encoding encoding = Encoding.UTF8;
        request.ContentLength = encoding.GetByteCount(data);
        request.GetRequestStream().Write(
          encoding.GetBytes(data), 0, (int)request.ContentLength);
        request.GetRequestStream().Close();
    }
    else
    {
        // If we're doing a GET or DELETE don't bother with this 
        request.ContentLength = 0;
    }
    // Finally, return the newly created request to the caller. 
    return request as HttpWebRequest;
}
可能是以下代码:

MessageBox.Show("Made it past the call to CreateRequestNoCredentials()");
…是不必要的,但我显然甚至没有达到目的,因为我从来没有看到这个信息;我只看到前面提到的错误,然后“错误在Bla.exe中发生意外错误,请选择Quite,然后重新启动此程序,或者选择Details以获取更多信息。”

详细信息是:“System.Threading.WaitHandle.CheckResultInternal(布尔r)处的错误Bla.exe ObjectDisposedException位于…(等)”

然后,“应用程序Bla.exe遇到严重错误,必须关闭。”

那么这次旅行是怎么回事

更新 我不明白领导对这个问题的回答

另外一件奇怪的事情是,即使我遇到了“严重错误”,但它似乎并不像其他时候掌上应用程序崩溃时那样严重,在这种情况下,我必须经常对设备进行预热和/或在旧设备上复制.exe的新版本之前,将设备重新安装在支架上。但这一“严重”错误并没有发生

我简化了代码,使其不再返回HttpWebResponse,但仍然得到相同的结果。然而,我希望发生的事情很好:数据被发送到服务器应用程序,它成功地从传递的数据重新创建了一个XML文件,然后根据传递的值插入一个新记录来更新数据库

以下是新的简化代码:

public static void SendXMLFile(string xmlFilepath, string uri, int timeout) // timeout should be 500
{
    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = new StreamReader(xmlFilepath))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.AppendLine(line);
        }
    }

    string strData = @sb.ToString();
    strData = strData.Replace("\"", "'");
    string body = String.Format("\"{0}\"", strData);
    CreateRequestNoCredentials(uri, HttpMethods.POST, body, "application/json"); 
}
更新2 如果我以这种方式捕获并忽略异常:

catch (Exception ex)
{
    if (ex.Message.Contains("This operation cannot be performed after the request has been submitted"))
    {
        // just make like Johnny Bench
    }
}
…我可以毫无意外地运行该方法;但是,当我关闭应用程序时,我会收到“Bla.exe中出现意外错误。请选择退出,然后重新启动此程序,或选择详细信息以了解详细信息。”

更新3 建议的替换代码告诉我,“不能隐式地将类型“byte[]”转换为“long”

将其更改为:

request.ContentLength = arrData;
…告诉我,“无法将类型“byte[]”转换为“long”

所以它不能被即时或显式转换为长…然后

更新4
好的,arrData.Length有效。

您应该在调试器下运行代码,并观察它在哪一行失败

GetRequestStream
不应被多次调用。您应该考虑将代码更改为:

byte[] arrData = Encoding.UTF8.GetBytes(data);
request.ContentLength = arrData.Length;
using (Stream oS = request.GetRequestStream()) 
{
     oS.Write(arrData, 0, arrData.Length);
}

一定有人发现我曾经有一件“没有迪斯科”的t恤;或者可能是“不打高尔夫”的t恤……我无法单步执行此代码——它在手持设备上运行,没有可用的模拟器;如果需要,可以在Windows 8.1上的.NET 4.5中运行。请参阅更新3…没关系,arrData。长度有效。