支持编译依赖的多个版本 (vNext)
Supporting Multiple Versions of a Compilation Dependency (vNext)
我为目前支持 MVC 2 - MVC 5 的开源库做出贡献,我也希望支持 MVC 6(及更高版本)。为了支持每个版本的 MVC,我们利用 MSBuild 的 Condition 功能在构建时包含正确版本的 MVC 及其依赖项(取决于 DefineConstants
的值)。这使得通过使用相同的项目文件和源代码为每个 MVC 版本创建一个单独的 DLL,可以将单个项目文件用于所有受支持的 MVC 版本。
<ItemGroup Condition=" $(DefineConstants.Contains('MVC2')) ">
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('MVC3')) ">
<!-- Due to the windows update MS14-059, we need this hack to ensure we can build MVC3 both on machines that have the update and those that don't -->
<Reference Condition=" Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Condition=" !Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.3.0.20105.1\lib\net40\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Razor.1.0.20105.408\lib\net40\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.1.0.20105.408\lib\net40\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('MVC4')) ">
<Reference Include="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.4.0.20710.0\lib\net40\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Razor.4.0.20715.0\lib\net40\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.4.0.20710.0\lib\net40\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('MVC5')) ">
<Reference Include="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.0.0\lib\net45\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.0.0\lib\net45\System.Web.WebPages.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.0.0\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
</ItemGroup>
我查看了 ASP.NET 5/MVC 6 的项目结构,并且已经放弃使用 project.json
文件而不是 MVC 6 的 .csproj
文件。但是,我阅读了 project.json documentation 并且似乎没有办法通过单个 project.json
文件支持多个版本的 MVC。
理想情况下,我想放弃 MSBuild
并为每个 MVC 版本(包括 MVC 2 - MVC 5)使用 Roslyn
。但是有没有一种方法可以支持多个 MVC 版本,而不必为每个 MVC 版本创建一个项目文件(和项目目录,因为它们都必须命名为 project.json
)?如果没有,是否有另一种方法不必将所有 project.json
配置复制 5 次?
我找到了解决该问题的(不是很好的)解决方法。我还是想知道有没有更好的办法
我想到的解决方案是使用通配符来包含要在项目目录之外编译的文件。例如,我的项目结构如下所示:
MyProject.sln
// This is where the legacy MVC2-5 support goes
MyProject/
MyProject.csproj
// This is where the MVC6 support is compiled from
MyProject.MVC6/
MyProject.MVC6.xproj
project.json
所有 .cs
个文件都包含在 MyProject
中。然后我的 project.json
文件如下所示:
{
"version": "1.0.0-*",
"description": "MyProject Description",
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-beta7",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta7",
"Microsoft.AspNet.Routing": "1.0.0-beta7"
},
"compile": "../MyProject/**/*.cs",
"compilationOptions": {
"define": ["MVC6", "NET46"]
},
"frameworks": {
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23225",
"System.Collections": "4.0.11-beta-23225",
"System.Linq": "4.0.1-beta-23225",
"System.Runtime": "4.0.21-beta-23225",
"System.Threading": "4.0.11-beta-23225"
}
}
}
}
但是,此解决方案还有另一个问题 - Visual Studio 2015 不显示 MyProject.MVC6
中的任何文件,因为它们仅包含用于编译。此外,不可能包含 .csproj
文件,因为这会导致整个项目无法编译。
所以,我想出了另一个解决方法 - 在实际项目中包含一个 project.json
文件和 MyProject.DNX.Debug.xproj
文件,我将这个文件包含在 MVC6 解决方案中而不是 [=18] =] 文件.
MyProject.sln
// This is where the legacy MVC2-5 support goes
MyProject/
MyProject.csproj
MyProject.DNX.Debug.xproj
project.json
// This is where the MVC6 support is compiled from
MyProject.MVC6/
MyProject.MVC6.xproj
project.json
这些文件仅用于提供一种在 MVC6 中调试它们的方法,其想法是当 MVC7 出现时我将能够创建另一个项目文件夹,然后根据要调试的版本的需要交换此配置。
MyProject.sln
// This is where the legacy MVC2-5 support goes
MyProject/
MyProject.csproj
MyProject.DNX.Debug.xproj
project.json // This project will be swapped between MVC6 and MVC7 based on compilationOptions
// This is where the MVC6 support is compiled from
MyProject.MVC6/
MyProject.MVC6.xproj
project.json
// This is where the MVC7 support is compiled from
MyProject.MVC7/
MyProject.MVC7.xproj
project.json
这离理想还很远。如果有请提供更好的答案。
您可以创建一个编译为多个运行时的项目,即 .NET Core、.NET 4.5.1 等。
- 文件 -> 新建项目。
- Web -> Class 库(包)
- 编辑 project.json 文件。
在frameworks
元素下,可以输入多个框架。这里我的目标是 .NET Core 和 .NET 4.5.1:
"frameworks": {
"dnx451": {
"frameworkAssemblies": {
"System.Net.Http": "4.0.0.0", // Example reference
}
},
"dnxcore50": {
"dependencies": {
"System.Net.Http": "4.0.1-beta-23225" // Example reference
}
}
}
然后在您的代码中,您可以使用预处理器指令来编写特定于特定框架或运行时的代码。
public void Foo()
{
#if DNX451
// Code specific to .NET 4.5.1
#endif
}
我为目前支持 MVC 2 - MVC 5 的开源库做出贡献,我也希望支持 MVC 6(及更高版本)。为了支持每个版本的 MVC,我们利用 MSBuild 的 Condition 功能在构建时包含正确版本的 MVC 及其依赖项(取决于 DefineConstants
的值)。这使得通过使用相同的项目文件和源代码为每个 MVC 版本创建一个单独的 DLL,可以将单个项目文件用于所有受支持的 MVC 版本。
<ItemGroup Condition=" $(DefineConstants.Contains('MVC2')) ">
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('MVC3')) ">
<!-- Due to the windows update MS14-059, we need this hack to ensure we can build MVC3 both on machines that have the update and those that don't -->
<Reference Condition=" Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Condition=" !Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.3.0.20105.1\lib\net40\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Razor.1.0.20105.408\lib\net40\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.1.0.20105.408\lib\net40\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('MVC4')) ">
<Reference Include="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.4.0.20710.0\lib\net40\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Razor.4.0.20715.0\lib\net40\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.4.0.20710.0\lib\net40\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('MVC5')) ">
<Reference Include="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.0.0\lib\net45\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.0.0\lib\net45\System.Web.WebPages.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.0.0\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
</ItemGroup>
我查看了 ASP.NET 5/MVC 6 的项目结构,并且已经放弃使用 project.json
文件而不是 MVC 6 的 .csproj
文件。但是,我阅读了 project.json documentation 并且似乎没有办法通过单个 project.json
文件支持多个版本的 MVC。
理想情况下,我想放弃 MSBuild
并为每个 MVC 版本(包括 MVC 2 - MVC 5)使用 Roslyn
。但是有没有一种方法可以支持多个 MVC 版本,而不必为每个 MVC 版本创建一个项目文件(和项目目录,因为它们都必须命名为 project.json
)?如果没有,是否有另一种方法不必将所有 project.json
配置复制 5 次?
我找到了解决该问题的(不是很好的)解决方法。我还是想知道有没有更好的办法
我想到的解决方案是使用通配符来包含要在项目目录之外编译的文件。例如,我的项目结构如下所示:
MyProject.sln
// This is where the legacy MVC2-5 support goes
MyProject/
MyProject.csproj
// This is where the MVC6 support is compiled from
MyProject.MVC6/
MyProject.MVC6.xproj
project.json
所有 .cs
个文件都包含在 MyProject
中。然后我的 project.json
文件如下所示:
{
"version": "1.0.0-*",
"description": "MyProject Description",
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-beta7",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta7",
"Microsoft.AspNet.Routing": "1.0.0-beta7"
},
"compile": "../MyProject/**/*.cs",
"compilationOptions": {
"define": ["MVC6", "NET46"]
},
"frameworks": {
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23225",
"System.Collections": "4.0.11-beta-23225",
"System.Linq": "4.0.1-beta-23225",
"System.Runtime": "4.0.21-beta-23225",
"System.Threading": "4.0.11-beta-23225"
}
}
}
}
但是,此解决方案还有另一个问题 - Visual Studio 2015 不显示 MyProject.MVC6
中的任何文件,因为它们仅包含用于编译。此外,不可能包含 .csproj
文件,因为这会导致整个项目无法编译。
所以,我想出了另一个解决方法 - 在实际项目中包含一个 project.json
文件和 MyProject.DNX.Debug.xproj
文件,我将这个文件包含在 MVC6 解决方案中而不是 [=18] =] 文件.
MyProject.sln
// This is where the legacy MVC2-5 support goes
MyProject/
MyProject.csproj
MyProject.DNX.Debug.xproj
project.json
// This is where the MVC6 support is compiled from
MyProject.MVC6/
MyProject.MVC6.xproj
project.json
这些文件仅用于提供一种在 MVC6 中调试它们的方法,其想法是当 MVC7 出现时我将能够创建另一个项目文件夹,然后根据要调试的版本的需要交换此配置。
MyProject.sln
// This is where the legacy MVC2-5 support goes
MyProject/
MyProject.csproj
MyProject.DNX.Debug.xproj
project.json // This project will be swapped between MVC6 and MVC7 based on compilationOptions
// This is where the MVC6 support is compiled from
MyProject.MVC6/
MyProject.MVC6.xproj
project.json
// This is where the MVC7 support is compiled from
MyProject.MVC7/
MyProject.MVC7.xproj
project.json
这离理想还很远。如果有请提供更好的答案。
您可以创建一个编译为多个运行时的项目,即 .NET Core、.NET 4.5.1 等。
- 文件 -> 新建项目。
- Web -> Class 库(包)
- 编辑 project.json 文件。
在
frameworks
元素下,可以输入多个框架。这里我的目标是 .NET Core 和 .NET 4.5.1:"frameworks": { "dnx451": { "frameworkAssemblies": { "System.Net.Http": "4.0.0.0", // Example reference } }, "dnxcore50": { "dependencies": { "System.Net.Http": "4.0.1-beta-23225" // Example reference } } }
然后在您的代码中,您可以使用预处理器指令来编写特定于特定框架或运行时的代码。
public void Foo() { #if DNX451 // Code specific to .NET 4.5.1 #endif }