2

I have a very simple application which I am using for testing purposes and it is acting strangely:

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");
        }
    }
}

I can run this program on my PC without any issues whatsoever. However, when I put it on our server it crashes immediately with the following problem signature:

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

This is occurring before the first MessageBox appears, but surely MessageBox should run before the error-causing line, which I have deduced is the following:

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

as the program does run when I remove these lines. How is this even possible? It must be something on the server on which it is crashing, but how do I even begin to narrow this down? Usually I would use a MessageBox to help me identify where the issue is occurring, but I can't even do that here!

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);
    }
}

Update

I tried this with the same results:

    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);
        }
    }
Bassie
  • 9,529
  • 8
  • 68
  • 159
  • 2
    Note that even if you can't use a `MessageBox`, you can get some debugging output by logging to a file. – pie3636 Aug 03 '16 at 10:38
  • 3
    The code you are getting (e0434352) is a general exception for all .Net applications so doesn't help on it's own. You are probably missing some assemblies on the server. Can you wrap the code in a try/catch to get some more info out of it? – DavidG Aug 03 '16 at 10:40
  • @DavidG I put a `try/catch` around the first `MessageBox` but with the same results - leading me to believe this is not the issue. The problem must bewith the lines I mentioned, but I don't understand how they could possibly be crashing out BEFORE the `MessageBox` even gets a chance to run ... Surely this is impossible unless I'm using some `Async` stuff? – Bassie Aug 03 '16 at 10:44
  • @pie3636 Looking at logging some stuff to a text file now .. – Bassie Aug 03 '16 at 10:45
  • 1
    No, put the try/catch around the entire code block, then message box the exception from the catch block. – DavidG Aug 03 '16 at 10:45
  • 1
    What framework are you compiling against and what .NET runtime version is installed on that server? Which server version is it? – rene Aug 03 '16 at 10:48
  • @rene the server has `.net-4.5` and I have tried compiling the app on versions `4` and `4.5` – Bassie Aug 03 '16 at 10:49
  • @DavidG I added the `try/catch` as in the update above with the same results ... I swear this makes no sense .. – Bassie Aug 03 '16 at 10:50
  • 1
    [The answer linked by @DavidG](http://stackoverflow.com/a/6245146/69809) describes the options you've got. Probably fusion log will show you're missing some dependencies, so I would hammer this as well. – vgru Aug 03 '16 at 10:51
  • 1
    OK I'm fairly certain now that the server is missing some assembly reference that is used inside the `ExchangeShell` class, probably the powershell stuff. – DavidG Aug 03 '16 at 10:53
  • 1
    I'm pretty sure your app doesn't start at all, probably because it fails to bind to an assembly it needs to get bootstrapped. If inspection of the eventlog doesn't reveal anything, try to install and start fuslogvw (assembly fusion log viewer) and see if there are failed binds. – rene Aug 03 '16 at 10:53

0 Answers0