2016-01-06 130 views
1

我是ASP.NET新手,正在致力于个人项目以深入学习ASP.NET。ASP.NET 5 MVC 6 System.Management.Automation问题与DNX

所以要开始,我创建了一个新的ASP Web项目,并选择了ASP 5 Empty MVC模板。然后,我从Nuget安装了System.Management.Automation包,然后在Models文件夹中创建了一些名为PowerShellCmd.csPowerShellModule.cs的cs文件。

PowerShellCmd.cs只包含get和set属性。请看下面的代码:

namespace Automation.Models 
{ 
    public class PowerShellCmd 
    { 
     public string CmdLets { get; set; } 
     public string CmdOutput { get; set; } 
    } 
} 

PowerShellModule.cs做自动化任务,我在上面装​​这样我就可以在代码中使用它们。下面是我的代码:

using System.Management.Automation; 

namespace Automation.Models 
{ 
    public class PowerShellModule 
    { 
     public void ExecuteCode() 
     { 
      PowerShellCmd command = new PowerShellCmd(); 
      var shell = PowerShell.Create(); 

      shell.Commands.AddScript(command.CmdLets); 

      var results = shell.Invoke(); 
     } 

    } 
} 

当我将鼠标悬停在System.Management.Automation,我得到的引用可用于DNX 4.5.1和不适用于DNX 5.0。请看下面的截图。

enter image description here

当我建立的代码,我得到以下错误:

enter image description here

默认情况下,它是使用DNX 5.0找System.Management.Automation。我该如何改变以使用DNX4.5.1?

+0

看起来你缺少一个参考。尝试添加对System.Management的引用并重新编译。 – lumee

+0

@lumee - 参考 - > DNX4.5.1 - > System.Management.Automation下存在参考。 Nuget仓库中没有可用的DNX5.0依赖项。仅适用于DNX4.5.1。 – Ray

回答

2

.NET Core 5框架(DNX Core)不支持Package System.Management.Automation。 .NET Core 5是.NET Framework的一个子集。你可以在这里读更多关于它的内容。

http://docs.asp.net/en/latest/conceptual-overview/dotnetcore.html

如果你只是想使用完整的.NET框架(DNX 4.5.1),你可以去project.json文件,并删除了核心框架。之后,你的project.json文件的框架如下所示。

"frameworks": { 
     "dnx451": { } 
    }, 
+0

它的工作!谢谢。 :) – Ray