简体   繁体   English

为什么我在此异步操作中仅在 LiveServer 上收到 NullReferenceException?

[英]Why am I getting a NullReferenceException only on the LiveServer in this Async operation?

This is an intermitted issue, when I tried to post this live.这是一个间歇性的问题,当我试图发布这个 live 时。

protected void searchasync(string search, string usproengineer_id)
{
    Task a = Task.Run(() => searchuasync(search, usproengineer_id));
    Task b = Task.Run(() => searchcasync(search, usproengineer_id));
    Task c = Task.Run(() => searchpasync(search, usproengineer_id));
    Task.WaitAll(a, b, c);
}

Then each subsequent search section has:然后每个后续搜索部分都有:

protected async void searchuasync(string search, string usproengineer_id)
{
    Task<DataTable> taskAU = Task.Run(() => searchUinjection(search, usproengineer_id));
    Task<DataTable> taskBU = Task.Run(() => searchUexpinjection(search, usproengineer_id));
    Task<DataTable> taskCU = Task.Run(() => searchUcncinjection(search, usproengineer_id));
    Task<DataTable> taskDU = Task.Run(() => searchUcncmetalinjection(search, usproengineer_id));
    Task<DataTable> taskEU = Task.Run(() => searchUurethaneinjection(search, usproengineer_id));
    Task.WaitAll(taskAU, taskBU, taskCU, taskDU, taskEU); //Wait till these parts are done
    ....

So this code has no problem on my local in the Debugger running.所以这段代码在我本地的Debugger中运行是没有问题的。 Or on the Test-Server which is "Almost" the same as the live-server.或者在与实时服务器“几乎”相同的测试服务器上。 That is, I have no problem running the threaded searches.也就是说,我运行线程搜索没有问题。 But I did not anticipate receiving this error multiple times that I can't explain:但是我没想到会多次收到我无法解释的错误:

Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.

StackTrace:    at USAProEngHome.<searchcasync>d__1b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_1(Object state)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

This does seem to be related to this StackOverFlow But not quite the same.这似乎确实与这个StackOverFlow有关,但不太一样。 I have the webconfig targetframework set to 4.6 and no legacy set.我将 webconfig targetframework 设置为 4.6 并且没有遗留设置。 It is possible I have something misconfigured in my webconfig but don't know what it is.可能是我的 webconfig 中配置错误但不知道是什么。

So here is my own answer to the question and what seemed to fix it (maybe someone could still point out ways to improve the design speed ...): Notice above I had '...' in Searchuasync (as I didn't show the rest of the code).所以这是我自己对这个问题的回答以及似乎可以解决它的方法(也许有人仍然可以指出提高设计速度的方法......):注意上面我在 Searchuasync 中有'......'(因为我没有显示其余代码)。 Now, I had moved that code to its parent 'Searchasync'.现在,我将该代码移至其父“Searchasync”。
So now the SearchUasync looks like the following: (returning a Datatable.. Each of the 3 functions look like this, and they each have subsequent tasks that run asynchronously)所以现在 SearchUasync 看起来像下面这样:(返回一个 Datatable.. 3 个函数中的每一个看起来像这样,它们每个都有异步运行的后续任务)

 protected async Task<DataTable> searchuasync(string search, string usproengineer_id)
{
    Task<DataTable> taskAU = Task.Run(() => searchUinjection(search, usproengineer_id));
    Task<DataTable> taskBU = Task.Run(() => searchUexpinjection(search, usproengineer_id));
    Task<DataTable> taskCU = Task.Run(() => searchUcncinjection(search, usproengineer_id));
    Task<DataTable> taskDU = Task.Run(() => searchUcncmetalinjection(search, usproengineer_id));
    Task<DataTable> taskEU = Task.Run(() => searchUurethaneinjection(search, usproengineer_id));
    Task.WaitAll(taskAU, taskBU, taskCU, taskDU, taskEU);
    //Unconfirmed query...
    DataTable dt6 = await taskAU;
    DataTable dt7 = await taskBU;
    DataTable dt8 = await taskCU;
    DataTable dt9 = await taskDU;
    DataTable dt10 = await taskEU;
    dt6.Merge(dt7, true, MissingSchemaAction.Ignore);
    dt6.Merge(dt8, true, MissingSchemaAction.Ignore);
    dt6.Merge(dt9, true, MissingSchemaAction.Ignore);
    dt6.Merge(dt10, true, MissingSchemaAction.Ignore);

    return dt6;
}

Then in the parent looks like this: (notice I've commented out the original task.waitall command, and I'm just using an 'await' for each of the 3 parts. Thinking the 'await c' is the most expensive. By the time it gets to 'await a' or 'b' they should be done. But maybe not in every case.然后在父级中看起来像这样:(注意我已经注释掉了原始的 task.waitall 命令,我只是对 3 个部分中的每一个都使用了一个“await”。认为“await c”是最昂贵的。当它到达“等待 a”或“b”时,它们应该完成了。但也许不是在所有情况下。

protected async void searchasync(string search, string usproengineer_id)
{
    Task<DataTable> c = Task.Run(() => searchpasync(search, usproengineer_id));
    Task<DataTable> b = Task.Run(() => searchcasync(search, usproengineer_id));
    Task<DataTable> a = Task.Run(() => searchuasync(search, usproengineer_id));
    //Task.WaitAll(a, b, c);

    DataTable dt1 = await c;
    ((GridView)ContentPlaceHolder1.FindControl("GridView_MoldProjects")).DataSource = dt1;
    ContentPlaceHolder1.FindControl("GridView_MoldProjects").DataBind();
    if (dt1.Rows.Count != 0)
    {
        ((HtmlInputHidden)ContentPlaceHolder1.FindControl("hidden_4")).Value = "1";
    }
    else
    {
        ((HtmlInputHidden)ContentPlaceHolder1.FindControl("hidden_4")).Value = "0";
    }

    DataTable dt11 = await b;
    ((GridView)ContentPlaceHolder1.FindControl("GridView_ConfirmedQuotes")).DataSource = dt11;
    ContentPlaceHolder1.FindControl("GridView_ConfirmedQuotes").DataBind();
    if (dt11.Rows.Count != 0)
    {
        ((HtmlInputHidden)ContentPlaceHolder1.FindControl("hidden_2")).Value = "1";
    }
    else
    {
        ((HtmlInputHidden)ContentPlaceHolder1.FindControl("hidden_2")).Value = "0";
    }
    //end confirm3ed

    DataTable dt6 = await a;
    ((GridView)ContentPlaceHolder1.FindControl("GridView_UnconfirmedQuotes")).DataSource = dt6;
    ContentPlaceHolder1.FindControl("GridView_UnconfirmedQuotes").DataBind();
    if (dt6.Rows.Count != 0)
    {
        ((HtmlInputHidden)ContentPlaceHolder1.FindControl("hidden_1")).Value = "1";
    }
    else
    {
        ((HtmlInputHidden)ContentPlaceHolder1.FindControl("hidden_1")).Value = "0";
    }
    //End Unconfirmed

}

I've seen no evidence of the 'nullreference' compiler generated Async error.我没有看到“nullreference”编译器生成异步错误的证据。 It's been live-tested this way for one-day.它已经以这种方式进行了一天的现场测试。 With numerous hits.有无数的点击率。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到NullReferenceException? - Why am i getting NullReferenceException? 为什么我会收到此 NullReferenceException? - Why am I getting this NullReferenceException? 无法理解为什么我得到NullReferenceException - Cannot understand why I am getting a NullReferenceException 为什么我在DataSet上得到NullReferenceException? - Why am I getting a NullReferenceException on a DataSet? 在Xamarin.Forms中订阅事件时,为什么只能在iOS上收到NullReferenceException? - Why am I getting a NullReferenceException when subscribing to event in Xamarin.Forms, but only on iOS? 为什么在尝试使用仅具有字符串属性的非常简单的类型构造 XmlSerializer 时出现 NullReferenceException? - Why am I getting a NullReferenceException when attempting to construct an XmlSerializer using a very simple Type with only string properties? 为什么我在System.Data.Entity.dll(实体框架)中收到NullReferenceException? - Why am I getting a NullReferenceException in System.Data.Entity.dll (Entity Framework)? 尝试为属性数组设置值时为什么会出现NullReferenceException? - Why am I getting NullReferenceException when tried to set a value to property array? 为什么在使用IsChecked时使用此foreach循环获取System.NullReferenceException - Why I am getting a System.NullReferenceException with this foreach loop when using IsChecked 当我的变量在“清醒”方法中实例化时,为什么会出现NullReferenceException? - Why am I getting a NullReferenceException when my variable is instantiated in the “awake” method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM