PowerShellセッションを維持するクラスがあります。私は新しいセッションを作成することなくpowershellセッションにアクセスできます。以下は私のスニペットコードHttpContext.Current.Session keep returnオブジェクト参照がクラスのオブジェクト(MVC 5)のインスタンスに設定されていません
public class PowerShellSession : IHttpHandler, IRequiresSessionState
{
public bool IsReusable
{
get
{
return false;
}
}
public void ProcessRequest(HttpContext context)
{
throw new NotImplementedException();
}
public PowerShell PowerShell2010()
{
if(HttpContext.Current.Session == null)
{
WSManConnectionInfoSession connExch = new WSManConnectionInfoSession();
var session = connExch.GetExchangeConnectionSession(2010);
Runspace runspace = RunspaceFactory.CreateRunspace(session);
runspace.Open();
PowerShell Shell = PowerShell.Create();
Shell.Runspace = runspace;
HttpContext.Current.Session["PowerShell2010"] = Shell;
return Shell;
}
if (HttpContext.Current.Session["PowerShell2010"] != null)
{
WSManConnectionInfoSession connExch = new WSManConnectionInfoSession();
var session = connExch.GetExchangeConnectionSession(2010);
Runspace runspace = RunspaceFactory.CreateRunspace(session);
runspace.Open();
PowerShell Shell = PowerShell.Create();
Shell.Runspace = runspace;
HttpContext.Current.Session["PowerShell2010"] = Shell;
return Shell;
}
else
{
return (PowerShell)HttpContext.Current.Session["PowerShell2010"];
}
}
}
は問題は常に私がセッションに値を設定しようとすると、「オブジェクトのインスタンスに設定されていないオブジェクト参照」を返す私のコードです。ここで
HttpContext.Current.Session["PowerShell2010"] = Shell;
セッション
に値を設定するコードは、私が何か間違ったことをしましたか?
'HttpContext.Current.Session'は辞書ですか?ウォッチを追加するように設定し、 'Shell'に設定しようとする前に値が何であるかを確認する必要があります。 – Gavin
セッションが設定されていないことがあります。 'HttpContext.Current.Session'が' null'の場合、最初のif条件が入ります。プロパティにセッションを割り当てずに、「null」であることが証明されたときにアクセスしようとする – Nkosi