我正在尝试集成一些我编写的新代码,用于通过Remote Powershell以编程方式与Exchange 2010交互到现有的WinForms应用程序中。我的代码在一个独立的测试应用程序中工作,但是当我在我的应用程序的上下文中运行代码时,对Runspace.Open()
的调用会阻塞很长时间 - 远超过OpenTimeout
值60秒,我在WSManConnectionInfo
实例中指定。对我来说,这表明我们的应用程序代码中有一些东西正在创建问题,但我无法缩小这些潜在原因可能是什么。该应用程序是多线程的,并使用BackgroundThreadWorker
和ThreadPool
;事实上,我的代码通过应用程序中的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