I have a Windows Forms app that provisions user accounts in Exchange using Powershell and Exchange2007 cmdlets. There is only one form to this application that takes the information for the new user and then runs the Powershell commands. Like a good programmer, I just refactored the code and took all the Exchange and Active Directory calls out and put them in separate classes. In the Windows form, I call the following code in a button Click event:
我有一个Windows窗体应用程序,使用Powershell和Exchange2007 cmdlet在Exchange中配置用户帐户。此应用程序只有一个表单,它为新用户提供信息,然后运行Powershell命令。就像一个优秀的程序员一样,我只是重构了代码并将所有的Exchange和Active Directory调用输出并将它们放在不同的类中。在Windows窗体中,我在按钮Click事件中调用以下代码:
ExchangeMailboxFunctions exFuncs = new ExchangeMailboxFunctions();
exFuncs.CreateNewMailbox(username, userPrincipalName, OU, txtDisplayName.Text, txtFirstName.Text, txtInitials.Text, txtLastName.Text,
txtPassword1.Text, ckbUserChangePassword.Checked, mailboxLocation);
In the class itself, I have the following:
在课堂上,我有以下内容:
RunspaceConfiguration config = RunspaceConfiguration.Create();
PSSnapInException warning;
Runspace thisRunspace;
public ExchangeMailboxFunctions()
{
InitializePowershell();
}
private void InitializePowershell()
{
try
{
thisRunspace = RunspaceFactory.CreateRunspace(config);
config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out warning);
thisRunspace.Open();
}
catch (Exception ex)
{
throw new Exception(string.Format("Could not initialize Powershell: {0}", ex.Message));
}
}
public void CreateNewMailbox(string username, string userPrincipalName, string OU, string DisplayName, string FirstName, string Initials,
string LastName, string Password, bool ChangePasswordNextLogon, string MailboxLocation)
{
try
{
using (Pipeline thisPipeline = thisRunspace.CreatePipeline())
[Set a bunch of pipLine parameters here]
thisPipeline.Invoke();
}
catch (Exception ex)
The thisPipeline.Invoke causes the error and I have no idea what is Disposed. This code worked perfectly fine when it was in the codebehind of the form. I also have some Active Directory methods that I tore out into a separate class library and they seem to work fine.
thisPipeline.Invoke导致错误,我不知道什么是Disposed。当代码在表单的代码隐藏中时,此代码工作得很好。我也有一些Active Directory方法,我把它们分成一个单独的类库,它们似乎工作正常。
What should I do to get this to stop happening? Thanks!
我该怎么办才能让这件事停止发生?谢谢!
2 个解决方案
#1
Make sure your code actually looks like this...
确保您的代码看起来像这样......
using (Pipeline thisPipeline = thisRunspace.CreatePipeline())
{
[Set a bunch of pipLine parameters here]
thisPipeline.Invoke();
}
The brackets being the key, and they're missing in your example.
括号是关键,在你的例子中它们是缺失的。
#2
Sorry guys, the brackets and such were actually ok - I must've left them out of the questions. I missed these lines of code in the create method:
对不起伙计们,括号等等实际上还可以 - 我一定是把它们排除在问题之外。我在create方法中错过了这些代码行:
using (SecureString ss = new SecureString())
{
foreach (char c in Password)
ss.AppendChar(c);
thisPipeline.Commands[0].Parameters.Add("Password", ss);
}
Since the Using disposed before the Invoke got called, there was no ss as it was already Disposed. Should've looked a little deeper before posting this :-(
由于在调用Invoke之前处理了Using,因此没有ss,因为它已经被Disposed了。发布之前应该看得更深一点:-(
#1
Make sure your code actually looks like this...
确保您的代码看起来像这样......
using (Pipeline thisPipeline = thisRunspace.CreatePipeline())
{
[Set a bunch of pipLine parameters here]
thisPipeline.Invoke();
}
The brackets being the key, and they're missing in your example.
括号是关键,在你的例子中它们是缺失的。
#2
Sorry guys, the brackets and such were actually ok - I must've left them out of the questions. I missed these lines of code in the create method:
对不起伙计们,括号等等实际上还可以 - 我一定是把它们排除在问题之外。我在create方法中错过了这些代码行:
using (SecureString ss = new SecureString())
{
foreach (char c in Password)
ss.AppendChar(c);
thisPipeline.Commands[0].Parameters.Add("Password", ss);
}
Since the Using disposed before the Invoke got called, there was no ss as it was already Disposed. Should've looked a little deeper before posting this :-(
由于在调用Invoke之前处理了Using,因此没有ss,因为它已经被Disposed了。发布之前应该看得更深一点:-(