首页 > 解决方案 > 从 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

标签: asp.netasp.net-mvcpowershellmodel-view-controllerrouting

解决方案


推荐阅读