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

  • Question


  • 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?

    Sunday, June 23, 2019 3:14 AM

All replies

  • Hi

    From the error message I think it is because .net core doesn't have System.Diagnostics.Eventing.EventDescriptor.


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Monday, June 24, 2019 5:48 AM
  • Hi

    From the error message I think it is because .net core doesn't have System.Diagnostics.Eventing.EventDescriptor.


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Right. Currently, there seems to be no way to issue commands or interact with Exchange management from .NET Core application. Any insight on when Microsoft is going to resolve it or make it available?

    For instance, AWS WorkMail provide full administrative SDK but for Exchange, we have to use Powershell commands even though both products are of Microsoft's.

    Monday, June 24, 2019 12:32 PM
  • I haven't found any reference for that.

    Maybe you can try to ask about that if it is a common requirement.

    https://github.com/dotnet/core/blob/master/roadmap.md


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, June 25, 2019 2:20 AM