简体   繁体   English

对于一个控制器,新的线程创建默默地失败了,而对于另一个控制器却没有? C#ASP.NET(IIS 8.5)Web API 2

[英]New thread creation silently fails for one controller and not the other? C# ASP.NET (IIS 8.5) Web API 2

I have two controllers set up in a Web API 2 project. 我在Web API 2项目中设置了两个控制器。 The first controller starts n processing sessions by calling the other controller. 第一个控制器通过调用另一个控制器来开始n个处理会话。

First controller in psuedo-code: 伪代码中的第一个控制器:

[HttpPost]
public bool Post(...)
{
 ...
 StartProcessingSessions(howMany)
}
void StartProcessingSessions(...)
{
 for (i=1 to HowMany)
  Task.Factory.StartNew(() => StartProcessingSession(...);
}
void StartProcessingSession(...)
{
 [post async to 2nd controller]
}

This code works just fine and the 2nd controller is called. 该代码可以正常工作,并调用第二个控制器。 The 2nd controller attempts to create a thread the exact same way to do some potentially long running work (1-10 minutes) but that thread fails to start, silently. 第二个控制器尝试以完全相同的方式创建线程来执行一些可能需要长时间运行的工作(1-10分钟),但是该线程无法静默启动。 After trawling the internet, I've read conflicting information about the support for creating threads within IIS. 拖曳了互联网之后,我阅读了有关在IIS中创建线程的支持的冲突信息。 Most say that after 4.5.2 it should work no problem, but others say it has always worked. 大多数人说,在4.5.2之后,它应该没有问题,但是其他人则说它一直都在起作用。 For corporate reasons, I can't go beyond 4.5.1 for the time being. 由于公司原因,暂时不能超过4.5.1。

Best of all, the code works correctly in debug mode and only fails when deployed to the web server. 最重要的是,该代码在调试模式下可以正常工作,并且只有在部署到Web服务器时才会失败。

I have tried replacing the processing with a simple db log write, but it does not execute. 我尝试用简单的数据库日志写入替换处理,但是无法执行。 Other methods of creating a thread fail silently as well. 创建线程的其他方法也会无声地失败。 If I remove the thread creation in the 2nd controller, everything works correctly. 如果我在第二个控制器中删除线程创建,则一切正常。

Next, I plan to try simplifications of the controllers to see if I can pinpoint what exactly is the criteria that blocks the thread. 接下来,我计划尝试简化控制器,以查看是否可以精确地确定阻塞线程的条件。

Any insights? 有什么见解吗? Thanks 谢谢

Edit: here's a simple controller that can stand in as "Controller 2" that illustrates the problem. 编辑:这是一个简单的控制器,可以用作说明问题的“控制器2”。 The first log is written and the second is not. 写入第一个日志,不写入第二个日志。

    [HttpPost]
    public bool Post()
    {
        _logRepository.InsertError("Modeling Debugging", "TestController hit");
        Task.Factory.StartNew(() => _logRepository.InsertError("Modeling Debugging", "TestController Thread"));
        return true;
    }

It appears this is just a "safety" limitation of ASP.NET, possibly intended to prevent the developer from potentially shooting self in foot via request splitting ballooning out of control. 看来,这只是ASP.NET的“安全”限制,它可能旨在防止开发人员通过将请求拆分气球超出控制范围而潜在射击。 I haven't found any information that confirms this but it is the only plausible explanation I have at this point. 我还没有发现任何可以证实这一点的信息,但这是我目前仅有的唯一合理的解释。

If you can use .NET 4.5.2+ it may be a moot point since you have the BackgroundWorker which might not be subject to this restriction. 如果您可以使用.NET 4.5.2+,那么这可能是一个有争议的问题,因为您拥有的BackgroundWorker可能不受此限制。 I'm not blocked in my use case above since I still was able to get the threads I wanted though I had run them directly as the controller thread. 在上面的用例中,我没有被阻止,因为尽管我已经直接将它们作为控制器线程运行,但我仍然能够获得所需的线程。 Not what I wanted, but it works to give me full CPU usage. 不是我想要的,但是它可以使我充分利用CPU。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM