程序在第一行之前崩溃

时间:2016-08-03 10:36:19

标签: c#

我有一个非常简单的应用程序,我用于测试目的,它的行为很奇怪:

using System;
using System.Windows.Forms;

namespace PSMod
{
    class Program
    {
        static void Main(string[] args)
        {
            MessageBox.Show("START");

            var shell = new ExchangeShell();
            var oof = shell.GetOofSettings("test.user"); 

            MessageBox.Show(oof.InternalReply);
            MessageBox.Show("END");
        }
    }
}

我可以在我的电脑上运行这个程序,没有任何问题。但是,当我把它放在我们的服务器上时,它会立即崩溃并出现以下问题签名

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: PSMod.exe
  Application Version:  1.0.0.0
  Application Timestamp:    57a1c784
  Fault Module Name:    KERNELBASE.dll
  Fault Module Version: 6.1.7601.23455
  Fault Module Timestamp:   573a54fc
  Exception Code:   e0434352
  Exception Offset: 000000000001a06d
  OS Version:   6.1.7601.2.1.0.272.7
  Locale ID:    2057
  Additional Information 1: 367e
  Additional Information 2: 367e805d0e7c1ec3f63b05bb5ce5c416
  Additional Information 3: c826
  Additional Information 4: c8268c80d2e62fec799d399d46a4e09f

这是在第一个MessageBox出现之前发生的,但肯定MessageBox应该在导致错误的行之前运行,我推断出以下内容:

var oof = shell.GetOofSettings("test.user");
MessageBox.Show(oof.InternalReply);

因为当我删除这些行时程序会运行。这怎么可能呢?它必须是服务器上崩溃的东西,但我怎么开始缩小它呢?通常我会使用MessageBox来帮助我确定问题发生的位置,但我甚至不能在这里做到这一点!

ExchangeShell.cs

public class ExchangeShell
{
    private Runspace MyRunSpace;
    private PowerShell MyPowerShell;
    private Uri ExchangeServer;

    public ExchangeShell()
    {
        var exchangeserverurl = new Uri("http://EXCHANGE-SERVER/PowerShell");
        var psCredential = GetCredentials();
        var connectionInfo = new WSManConnectionInfo(exchangeserverurl, "http://schemas.microsoft.com/powershell/Microsoft.Exchange", psCredential);

        //connectionInfo.SkipCACheck = true;
        //connectionInfo.SkipCNCheck = true;
        //connectionInfo.SkipRevocationCheck = true;
        //connectionInfo.MaximumConnectionRedirectionCount = 4;
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

        MyRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);
    }

    public OofSettings GetOofSettings(string email)
    {
        using (MyRunSpace)
        {
            MyRunSpace.Open();

            using (var powerShell = PowerShell.Create())
            {
                powerShell.Runspace = MyRunSpace;

                var command = new PSCommand();

                command.AddCommand("Get-MailboxAutoReplyConfiguration");
                command.AddParameter("Identity", email);

                powerShell.Commands = command;

                var result = powerShell.Invoke();

                var oofSetting = new OofSettings();

                oofSetting.State = (OofState)Enum.Parse(typeof(OofState), result[0].Properties["AutoReplyState"].Value.ToString());
                oofSetting.Duration = new TimeWindow((DateTime)result[0].Properties["StartTime"].Value, (DateTime)result[0].Properties["EndTime"].Value);
                oofSetting.ExternalAudience = (OofExternalAudience)Enum.Parse(typeof(OofExternalAudience), result[0].Properties["ExternalAudience"].Value.ToString());
                oofSetting.InternalReply = result[0].Properties["InternalMessage"].Value.ToString();
                oofSetting.ExternalReply = result[0].Properties["ExternalMessage"].Value.ToString();

                return oofSetting;
            }
        }
    }

    private PSCredential GetCredentials()
    {
        var secureString = new SecureString();
        foreach (char c in @"PASSWORD")
        {
            secureString.AppendChar(c);
        }
        return new PSCredential("ADMINUSER", secureString);
    }
}

更新

我尝试了同样的结果:

    static void Main(string[] args)
    {
        try
        {
            MessageBox.Show("START");

            var shell = new ExchangeShell();
            var oof = shell.GetOofSettings("seb.test");

            MessageBox.Show(oof.InternalReply);
            MessageBox.Show("END");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

0 个答案:

没有答案