asp.net - 从 BaseController 继承的 MVC 控制器后,powershell 运行空间无法打开
问题描述
在我的mvc web双语应用程序中,我有一个控制器(ProductController)通过powershell命令连接交换服务器以从交换服务器获取数据,如果ProductController继承自Controller,它工作正常,但如果ProductController继承自BaseController,则失败,在调试模式,我在 runspace.Open() 上收到此错误消息
Connecting to remote server xxxxxx.ca failed with the
following error message :
<f:WSManFault
xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault"
Code="3762507597" Machine="xxxxxx.CA">
<f:Message><f:ProviderFault provider="PowerShellplugin" path="%windir%\system32\pwrshplugin.dll">
</f:ProviderFault>
</f:Message>
有关详细信息,请参阅 about_Remote_Troubleshooting 帮助 在此处输入代码
这是 ProductController 的代码
public class ProductController : BaseController
PSCredential newCred;
WSManConnectionInfo connectionInfo;
Runspace runspace;
PowerShell powershell;
PSCommand command;
System.Collections.ObjectModel.Collection<PSObject> psresult3 = new System.Collections.ObjectModel.Collection<PSObject>();
string sResult = "";
string sUser = System.Configuration.ConfigurationManager.AppSettings.Get("ProdUser").ToString();
string sPassword = ConfigurationManager.AppSettings.Get("ProdPassword").ToString();
SecureString securePassword = new SecureString();
foreach (char c in sPassword.ToCharArray())
{
securePassword.AppendChar(c);
}
newCred = new PSCredential(sUser, securePassword);
connectionInfo = new WSManConnectionInfo(new Uri(ConfigurationManager.AppSettings.Get("WSManConnectionInfo").ToString()), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", newCred);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
runspace = RunspaceFactory.CreateRunspace(connectionInfo);
try
{
runspace.Open(); //error message happens here when the controller inherits from BaseController
powershell = PowerShell.Create();
enter code here
这是 BaseController 的代码
[Localization("en")]
public abstract class BaseController : Controller
{
private string CurrentLanguageCode { get; set; }
protected override void Initialize(RequestContext requestContext)
{
if (requestContext.RouteData.Values["lang"] != null && requestContext.RouteData.Values["lang"] as string != "null")
{
CurrentLanguageCode = (string)requestContext.RouteData.Values["lang"];
if (CurrentLanguageCode != null)
{
try
{
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(CurrentLanguageCode);
}
catch (Exception)
{
throw new NotSupportedException($"Invalid language code '{CurrentLanguageCode}'.");
}
}
}
base.Initialize(requestContext);
}
} enter code here
解决方案
推荐阅读
- python-3.x - 如何在 python 中使用 pypeteer 为网站设置多个 cookie
- javascript - 当前 HTML 页面在新选项卡中打开时滚动(网络中没有 JS),如何避免滚动?
- reactjs - HOC组件如何设置key?
- java - 为什么单击登录后我的应用程序总是崩溃?
- php - [Route:..] 缺少所需参数是否有解决方案?
- html - 春季 MVC CRUD 操作中的更新方法不起作用
- pgadmin-4 - 在 centos 7 中运行 pgadmin4
- android - Log.d(TAG, "downloadImgsUriArrayList:$downloadImagesUriList ") 之后为迭代器的 .addOnCompleteListener 上传代码执行
- shell - Shell:在字母数字字符后添加换行符
- android - AOSP - 错误:覆盖目标 (...) 的命令,之前定义在 (...)