【发布时间】:2017-02-12 16:55:02
【问题描述】:
我有课来保持 PowerShell 会话。这样我就可以在不创建新会话的情况下访问 powershell 会话。下面是我的sn-p代码
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之前,您应该为其设置添加手表并查看值是什么。 -
我从来没有看到你设置了会话。如果
HttpContext.Current.Session是null,则您的第一个 if 条件进入。当您刚刚证明它是null时,您没有为该属性分配会话但尝试访问它
标签: asp.net-mvc powershell session asp.net-web-api