locked
Creating a mailbox in Exchange Online using ASPNET Core RRS feed

  • Question

  • User973913737 posted

    I used following code to execute commands on Exchange Online for creating a new mailbox. 

    void Create(CreateMailboxInput input)
    {
        Command myCommand = new Command("New-MailBox");
        myCommand.Parameters.Add("Name", input.Name);
    
        List<Command> commands = new List<Command>() { myCommand };
        ExecuteCommand(commands);
    }
    public static Collection<PSObject> ExecuteCommand(List<Command> commands)
    {
        string pass = Utility.AppSettings.ExchangeAppSettings.Password;
        System.Security.SecureString securePassword = new System.Security.SecureString();
    
        foreach (char c in pass.ToCharArray())
        {
            securePassword.AppendChar(c);
        }
    
        PSCredential newCred = new PSCredential(Utility.AppSettings.ExchangeAppSettings.Mailbox);
    
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
            new Uri("https://outlook.office365.com/PowerShell-LiveID"),
            "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
            newCred);
    
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
    
        Runspace myRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);
        myRunSpace.Open();
    
        Collection<PSObject> result = new Collection<PSObject>();
        foreach (var command in commands)
        {
            Pipeline pipeLine = myRunSpace.CreatePipeline();
            pipeLine.Commands.Add(command);
            result = pipeLine.Invoke();
        }
    
        myRunSpace.Close();
        return result;
    }

    It works perfectly in .NET Framework application but when I try to execute it in a ASPNET Core application, it fails. It gives following error-

    System.TypeInitializationException: The type initializer for 'System.Management.Automation.Runspaces.RunspaceFactory' 
    threw an exception. ---> System.TypeLoadException: Could not load type 'System.Diagnostics.Eventing.EventDescriptor' from assembly
    'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. at System.Management.Automation.Runspaces.RunspaceFactory..cctor() --- End of inner exception stack trace --- at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo) at ConsolePS.ExchangeOnlinePS.ExecuteCommand(List`1 commands) in C:\Projects\ConsolePS\ExchangeOnlinePS.cs:line 30

    Any thoughts on how this can be resolved?

    Saturday, June 22, 2019 7:43 PM

All replies

  • User1120430333 posted

    It looks to me that a type from a non .NET Core library is incompatible with .NET Core.

    Saturday, June 22, 2019 8:08 PM
  • User973913737 posted

    It looks to me that a type from a non .NET Core library is incompatible with .NET Core.

    I agree. As per my research so far I got to same conclusion but I couldn't find an alternate to make it work in .NET Core application.

    Saturday, June 22, 2019 8:16 PM
  • User1120430333 posted

    DA924

    It looks to me that a type from a non .NET Core library is incompatible with .NET Core.

    I agree. As per my research so far I got to same conclusion but I couldn't find an alternate to make it work in .NET Core application.

    Maybe, you write a WebAPI 2.0 solution that the Core solution can consume and call, if the rest of your application is Core. 

    Saturday, June 22, 2019 9:57 PM
  • User-1764593085 posted

    Hi kpsingh,

    Do you use the System.Management.Automation package?

    It is more related to office 365/powershell, you could post the problem on its github:

    https://github.com/PowerShell/PowerShell/issues

    And System.Diagnostics.Eventing.EventDescriptor is not supported in asp.net core

    Best Regards,

    Xing

    Monday, June 24, 2019 3:32 AM