c# - 为什么我对 Runspace.Open() 的调用没有返回?

标签 c# .net-2.0 powershell-2.0 threadpool

我正在尝试将我为通过远程 Powershell 以编程方式与 Exchange 2010 交互而编写的一些新代码集成到现有的 WinForms 应用程序中。我的代码在一个独立的测试应用程序中工作,但是当我在我的应用程序的上下文中运行代码时,对 Runspace.Open() 的调用会阻塞很长时间——远远超过 我在 WSManConnectionInfo 实例中指定的 OpenTimeout 值为 60 秒。对我来说,这表明我们的应用程序代码中有某些东西造成了问题,但我无法缩小这些潜在原因的范围。该应用程序是多线程的,同时使用了 BackgroundThreadWorkerThreadPool;事实上,我的代码通过应用程序中的 ThreadPool 运行。但我已经尝试在我的测试工具中对此进行模拟,当从 ThreadPool 调用回调时,代码也能正常工作。

这是代码(删除了错误处理并在附近放置了常量定义):

const string EXCHANGE_PS_URI_FORMAT = "http://{0}/PowerShell/";
string uriString = string.Format(EXCHANGE_PS_URI_FORMAT, HostName);
Uri connectionUri = new Uri(uriString);

PSCredential creds = new PSCredential(username, securePwd);

const string EXCHANGE_PS_SCHEMA_URL = 
    "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
WSManConnectionInfo connectionInfo = 
    new WSManConnectionInfo(connectionUri, EXCHANGE_PS_SCHEMA_URL, creds);

const int DEFAULT_OPEN_TIMEOUT = 1 * 60 * 1000; // 1 minute
connectionInfo.OpenTimeout = DEFAULT_OPEN_TIMEOUT;
const int DEFAULT_OPERATION_TIMEOUT = 4 * 60 * 1000; // 4 minutes
connectionInfo.OperationTimeout = DEFAULT_OPERATION_TIMEOUT;

using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo))
{
    // BUGBUG: WHY IS THIS FAILING TO RETURN?
    rs.Open(); // <-- HANGS HERE

    ICollection<PSObject> newReqResults = null;
    PipelineReader<object> newReqErrors = null;
    try
    {
        using (Pipeline pipeline = rs.CreatePipeline())
        {
            // cmd instance is already instantiated with cmdlet info, params, etc.
            pipeline.Commands.Add(cmd);

            //Invoke the command and return the results and errors
            newReqResults = pipeline.Invoke();
            newReqErrors = pipeline.Error;
        }
    }

    // Code to parse results and/or errors...

代码在 Runspace.Open() 上挂起时的调用堆栈似乎表明内部 .NET 代码卡在等待调用上,但我不知道如何继续。正如我之前所说,这段代码在我的测试应用程序中运行良好,即使被称为 ThreadPool 回调,所以我想知道我们的主应用程序代码中有什么可能导致这种情况(同步上下文或线程标识或其他东西? ) 任何帮助将不胜感激。如果我忘记包含一些相关信息,请告诉我,我很乐意包含它。谢谢!

[In a sleep, wait, or join] 
mscorlib.dll!System.Threading.WaitHandle.WaitOne(long timeout, bool exitContext) + 0x2f bytes   
mscorlib.dll!System.Threading.WaitHandle.WaitOne(int millisecondsTimeout, bool exitContext) + 0x25 bytes    
mscorlib.dll!System.Threading.WaitHandle.WaitOne() + 0xd bytes  
System.Management.Automation.dll!System.Management.Automation.Runspaces.AsyncResult.EndInvoke() + 0x14 bytes    
System.Management.Automation.dll!System.Management.Automation.Runspaces.Internal.RunspacePoolInternal.EndOpen(System.IAsyncResult asyncResult) + 0xb2 bytes 
System.Management.Automation.dll!System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.Open() + 0x1a bytes 
System.Management.Automation.dll!System.Management.Automation.Runspaces.RunspacePool.Open() + 0x48 bytes    
System.Management.Automation.dll!System.Management.Automation.RemoteRunspace.Open() + 0x73 bytes    

最佳答案

有没有可能当你在线程池中运行程序时,线程被阻塞了?也许您在这里真正想要的是调用 OpenAsync而不是 Open

关于c# - 为什么我对 Runspace.Open() 的调用没有返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3771361/

相关文章:

c# - 如何验证您的应用程序是否全天运行?

c# - 存储库中添加和更新方法的特定实体对象

c# - 从 .Net 2.0 标准 ASMX Web 服务客户端调用 .Net 3.5 WCF 服务

.net - Clickonce:setup.exe 在浏览器中打开 .application 文件。普通的?

powershell - 使Powershell脚本仅获取为该过滤器定义的前10000个文件

c# - 如何在 Serenity 中创建动态菜单?

c# - DevExpress RepositoryItemComboBox 无法显示组合框列表数据

.net - EventLog.WriteEntry 和 EventLog.WriteEvent 方法之间的区别

powershell:如何单击 "submit type"输入

powershell - CruiseControl.Net 不能很好地与 PowerShell 配合使用