I used following code to execute commands on Exchange Online for creating a new mailbox
```c#
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);
}
```c#
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?
@Pilchie the issue is happening in ASP.NET, is this something your team can address or should it be sent to another area?
@carlossanlop This doesn't appear to have anything to do with ASP.NET? It's Exchange automation using WMI...
@carlossanlop This appears to be Exchange automation with WMI - nothing to do with ASP.NET.
@Anipik can you please take a look? Should we move it to CoreFX?
@singhkamall are you able to reproduce it in HelloWorld-style app? (console app ideally)
sure i will take a look
@singhkamall can you tell me the version of .net core you are running on ?
@karelz Yes, I can reproduce it in console app also.
@Anipik Its .NET Core 2.2
Could not load type 'System.Diagnostics.Eventing.EventDescriptor' from assembly
'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
This type is not available in .Net Core 2.2 and is only available 3.0 preview 4 and upwards
Any work around for .NET Core 2.2?
When will .NET Core 3.0 stable version will be releasing?
Any work around for .NET Core 2.2?
cc @ericstj @danmosemsft
When will .NET Core 3.0 stable version will be releasing?
Release of 3.0 GA is expected in Sept.
@singhkamall On a secondary look at the exact type name. This type System.Diagnostics.Eventing.EventDescriptor is not present on .NET core 3.0.
I will check why we didn't ported this api when we ported the other eventing apis.
But for the time being, you wont be able to use this api on .NET core.
Created a new issue in corefx to track why we didn't port these apis in dotnet core. https://github.com/dotnet/corefx/issues/39670
@singhkamall just to let you know we wont be adding eventDescriptor types in .net core as they are older functionality of writing code. You should be using eventsources to do writing.
@Anipik Appreciate the update.
I am not working with EventDescriptor directly but I'm using RunspaceFactory and looking at stack trace it seems like eventDescriptor is used in its constructor. Does it means System.Management.Automation.Runspaces.RunspaceFactory is also not supported?
Not sure how else I can execute commands for automation of Exchange Online without it. Any idea in regards to that?
Looks like that assembly comes from Powershell and is implemented here: https://github.com/PowerShell/PowerShell/blob/bd6fdae73520931f0d27a29d6290e18761772141/src/System.Management.Automation/engine/hostifaces/ConnectionFactory.cs#L17 Maybe follow up with powershell team? I think they have a version of that library that works on .NETCore. @SteveL-MSFT
@singhkamall, please try https://www.nuget.org/packages/Microsoft.PowerShell.SDK which is .NET Core compatible.
Most helpful comment
@singhkamall, please try https://www.nuget.org/packages/Microsoft.PowerShell.SDK which is .NET Core compatible.