簡體   English   中英

創建RunSpace時發生內部錯誤

[英]Internal Error Occurred When Creating RunSpace

我有一個應用程序,允許用戶通過簡單的Web界面在Exchange上更新其他用戶的不在辦公室的郵件。

我最初嘗試使用Microsoft.Exchange.WebServices創建此連接來連接到Exchange服務,但由於系統帳戶(通過EWS更新OOF郵件的帳戶需要使用該帳戶),因此我不得不放棄了此操作(即使它確實起作用了) 完整的郵箱權限)。

因此,為了克服這一點,我創建了以下類,該類使用PowerShell遠程處理來實現相同的目的:

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

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

        try
        {
            MyRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);
        }
        catch (Exception ex)
        {
            Email.Send($"Unable to connect \n\n Error: {ex.Message} ");
            Environment.Exit(Environment.ExitCode);
        }
    }

    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("SERVICEACCOUNT", secureString);
    }
}

當在我的計算機上本地運行或作為服務器上的EXE運行時,此方法也適用。

但是,當我通過IIS托管此主機時,我在此行看到錯誤:

MyRunSpace = RunspaceFactory.CreateRunspace(connectionInfo);

發生了內部錯誤。

這不是非常有用的錯誤消息,並且我不確定如何調試此消息。 有人對此有任何建議嗎?


更新資料

我在web.config附加了一個跟蹤器,這是在選擇用戶以檢索其不在辦公室的詳細信息后的一些跟蹤信息:

Category      Message                  From First(s)     From Last(s)
aspx.page     Begin PreInit   
aspx.page     End PreInit              0.000025          0.000025 
aspx.page     Begin Init               0.000035          0.000009 
aspx.page     End Init                 0.000057          0.000022 
aspx.page     Begin InitComplete       0.000065          0.000008 
aspx.page     End InitComplete         0.000073          0.000008 
aspx.page     Begin PreLoad            0.000081          0.000008 
aspx.page     End PreLoad              0.000093          0.000012 
aspx.page     Begin Load               0.000101          0.000008 

但是我真的不知道該怎么處理這些信息-它似乎並沒有太多揭示運行runspace和服務器之間實際發生的runspace

堆棧跟蹤:

在System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.ctor(Guid runspacePoolInstanceId,WSManConnectionInfo connectionInfo,PSRemotingCryptoHelper的System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.Initialize(Uri connectionUri,WSManConnectionInfo connectionInfo)處.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession會話,PSRemotingCryptoHelper cryptoHelper,RunspaceConnectionInfo connectionInfo,URIDirectionReported uriRedirectionHandler)在System.Management.Automation.Remoting.ClientRemoteSessionImpl.ctor(RemoteRunspacePoolInternal。 .ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool,TypeTable typeTable)在System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal..ctor(Int32 minRunspaces,Int32 maxRunspaces,Typ eTable typeTable,PSHost主機,PSPrimitiveDictionary applicationArguments,RunspaceConnectionInfo connectionInfo),位於System.Management.Automation.Runspaces.RunspacePool..ctor(Int32 minRunspaces,Int32 maxRunspaces,TypeTable typeTable,PSHost主機,PSHost,PSPrimitiveDictionary applicationArguments,RunspaceConnectionInfo connectionInfo。 .RemoteRunspace.ctor(TypeTable typeTable,RunspaceConnectionInfo connectionInfo,PSHost主機,PSPrimitiveDictionary applicationArguments)位於System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo,PSHost主機,TypeTable typeTable,PSPrimitiveDictionary applicationArguments)在SetOutOftor.ExchangeShell。 ()

如果要調試,則應該可以在.NET中使用跟蹤,也可以僅附加調試器

一個常見的問題是證書檢查失敗,因此您可能希望嘗試繞過那些檢查,例如

connectionInfo.SkipCACheck = true; connectionInfo.SkipCNCheck = true; connectionInfo.MaximumConnectionRedirectionCount = 4;

引起此問題的原因是我在服務器上安裝了錯誤版本的PowerShell。

很遺憾, Internal Error消息沒有提到這一點,但是可能是檢查何時發生此錯誤的好人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM