外部类库调用时无法访问已处置的对象错误

发布于 2024-07-18 02:21:43 字数 1819 浏览 8 评论 0原文

我有一个 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);

在类本身中,我有以下代码:

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 导致错误,我不知道 Dispose 是什么。 当这段代码位于表单的代码隐藏中时,它工作得非常好。 我还有一些 Active Directory 方法,我将它们分离到一个单独的类库中,它们似乎工作得很好。

我应该怎么做才能阻止这种情况发生? 谢谢!

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:

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.

What should I do to get this to stop happening? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2024-07-25 02:21:43

确保您的代码实际上看起来像这样...

using (Pipeline thisPipeline = thisRunspace.CreatePipeline())
{
    [Set a bunch of pipLine parameters here]

    thisPipeline.Invoke();
}

方括号是关键,它们在您的示例中丢失了。

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.

嘿看小鸭子会跑 2024-07-25 02:21:43

抱歉,伙计们,括号之类的东西实际上还可以——我一定把它们排除在问题之外了。 我在 create 方法中错过了这些代码行:

 using (SecureString ss = new SecureString())
 {
    foreach (char c in Password)
      ss.AppendChar(c);
    thisPipeline.Commands[0].Parameters.Add("Password", ss);
 }

由于在调用 Invoke 之前已放置了Using,因此没有 ss,因为它已经被释放了。 在发布此内容之前应该先深入了解一下:-(

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:

 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 :-(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文