PowershellおよびExchange2007コマンドレットを使用してExchangeでユーザーアカウントをプロビジョニングするWindowsフォームアプリケーションがあります。このアプリケーションには、新しいユーザーの情報を取得してPowershellコマンドを実行するフォームが1つしかありません。良いプログラマのように、私はコードをリファクタリングして、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);
、私は次のようしている:
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)
をthisPipeline.Invokeは、エラーが発生し、私が持っています何が処分されているのか分かりません。このコードは、フォームのコードビハインドに含まれていたときに問題なく動作しました。私は別のクラスライブラリに流出したいくつかのActive Directoryメソッドもあり、うまく動作しているようです。
これをやめるにはどうすればよいですか?ありがとう!
うん、これは何が起こっているかです。 – JasonMArcher