其中操作的请求已提交后不能执行? [英] Which operation cannot be performed after the request has been submitted?

查看:1472
本文介绍了其中操作的请求已提交后不能执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地在手持设备(压实框架/ Windows CE的)和现代之间传递数据(而不是现代在Windows 8中,但在21世纪)的服务器应用程序(网页API)。然而,数据的(成功的)通过后,客户端失败,出现错误消息的不能请求提交后执行此操作

I'm successfully passing data between a handheld device (Compacted Framework/Windows CE) and a modern (not "modern" as in Windows 8, but as in 21st Century) server app (Web API). However, after the (successful) passing of data, the client fails with the err msg, "This operation cannot be performed after the request has been submitted"

该操作是它抱怨?这里是我的客户code:

Which operation is it complaining about? Here's my client code:

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

这可能是code以下内容:

It may be that the code following:

MessageBox.Show("Made it past the call to CreateRequestNoCredentials()");

...是不必要的,但我显然没有得到,即使有,因为我从来没有看到该消息;我只看到前面提到的错误,然后的错误发生意外错误Bla.exe选择相当,然后重新启动该程序,或了解更多信息,选择详情。

具体内容是:错误Bla.exe的ObjectDisposedException在System.Threading.WaitHandle.CheckResultInternal(布尔R)在... 的(等)。

Details are: "Error Bla.exe ObjectDisposedException at System.Threading.WaitHandle.CheckResultInternal(Boolean r) at...(etc.)"

然后,应用程序Bla.exe遇到了一个严重的错误,必须关闭。

那么什么是跳闸了?

我不明白的leadthumb回答这个问题。

I don't understand the leadthumb response to this question.

所有关于它的另外一个奇怪的是,即使我得到遇到了一个严重的错误,它并没有真正似乎是手持应用程序崩溃的严重,其他时间,在这种情况下,我不得不经常热启动的设备和/或复制.exe文件在旧的新版本之前,重新固定在摇篮里的设备。但是,这是不是跟这个严重的错误发生。

An additional odd thing about it all is that, even though I get "encountered a serious error" it doesn't really seem to be as serious as other times the handheld app crashes, in which case I have to often warmboot the device and/or reseat the device in its cradle prior to copying a new version of the .exe over the old. But that is not happening with this "serious" error.

我简化了code,使其不再返回HttpWebResponse,但我仍然得到同样的结果。 YET,我希望发生的正常工作:将数据发送到服务器上的应用程序,它通过基于传递的值插入新记录成功地再现从数据通过一个XML文件,然后更新数据库

I simplified the code so that it no longer returns a HttpWebResponse, but I still get the same result. YET, what I want to happen works fine: the data is sent to the server app, and it successfully recreates an XML file from the data passed, and then updates a database by inserting a new record based on values passed.

下面是新的,简化code:

Here's the new, streamlined code:

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

如果我赶上并忽略了的异常是这样的:

UPDATE 2

If I catch and ignore the exception this way:

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。选择退出并重新启动该程序,或了解更多信息,选择详情。

...I can run the method without incident; however, when I then close the app, I get the "an unexpected error has occurred in Bla.exe. Select Quit and then restart this program, or select Details for more information."

建议的更换code给我,无法隐式转换类型'字节[]'到'长'

The suggested replacement code gives me, "Cannot implicitly convert type 'byte[]' to 'long'"

将其更改为这样的:

request.ContentLength = arrData;

...给我的无法将类型字节[]'到'长'

因此​​,它也不能IM-或显式转换为长... ...然后... ... ???

So it can't be either im- or explicitly converted to long...then...???

好吧,arrData.Length工作。

Okay, arrData.Length works.

推荐答案

您应该在调试器下运行code,看哪一行它失败的。

You should run your code under the debugger and watch which line it fails on.

GetRequestStream 不应该被调用一次以上。你应该考虑改变你的code这样:

GetRequestStream should not be called more than once. You should consider changing your code to this:

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

这篇关于其中操作的请求已提交后不能执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆