如何根据所选配置将项目构建为可移植或普通类库
本文关键字:可移植 类库 构建 项目 何根 配置 | 更新日期: 2023-09-27 18:34:01
上下文:我们有一个名为"THEPROJECT"的通用库项目,该项目与 Xamarin 移动解决方案和 Visual Studio 中的 Web 项目共享。由于 Xamarin 中的限制,此共享库是 PCL 配置文件 259。在我们的 Web 应用程序解决方案中,我们希望使用适当的属性标记数据模型,以便 DataContext 正常工作(因为我们要使用 LinqToSql(。
尝试的解决方案:我们决定研究"THEPROJECT"的项目,并根据Visual Studio中选定的配置,以一种允许它存在于可移植库/普通类库状态的方式更改msbuild。
我们已经在这篇文章中研究了解决方案,但它只是解决方案的一部分。我们可以让项目作为一个或另一个存在,但是它需要手动更改项目文件。现在我们收到加载以下项目时出错。对于可移植项目,必须为".净值'。
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">DebugDesktop</Configuration>
<Platform Condition="'$(Platform)' == ''">AnyCPU</Platform>
<ProjectGuid>{426E7BD8-9DA8-4E15-9512-72E7C632B037}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>THEPROJECT</RootNamespace>
<AssemblyName>THEPROJECT</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
<BuildMode>Desktop</BuildMode>
</PropertyGroup>
<!-- Define the Build Configurations -->
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugDesktop|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin'Desktop'Debug'</OutputPath>
<DefineConstants>TRACE;DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<BuildMode>Desktop</BuildMode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseDesktop|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin'Desktop'Release'</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<BuildMode>Desktop</BuildMode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugPCL|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin'PCL'Debug'</OutputPath>
<DefineConstants>TRACE;DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<BuildMode>Portable</BuildMode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleasePCL|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin'PCL'Release'</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<BuildMode>Portable</BuildMode>
</PropertyGroup>
<!-- Determine the correct properties for PCL/Desktop -->
<Choose>
<When Condition=" '$(BuildMode)' == 'Portable' ">
<PropertyGroup>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile259</TargetFrameworkProfile>
<TargetFrameworkIdentifier>.NETPortable</TargetFrameworkIdentifier>
<DynamicImportPath>$(MSBuildExtensionsPath32)'Microsoft'Portable'$(TargetFrameworkVersion)'Microsoft.Portable.CSharp.targets</DynamicImportPath>
</PropertyGroup>
</When>
<When Condition=" '$(BuildMode)' == 'Desktop' ">
<PropertyGroup>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<DynamicImportPath>$(MSBuildToolsPath)'Microsoft.CSharp.targets</DynamicImportPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Data.Linq" />
</ItemGroup>
</When>
</Choose>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..'..'mobileapplication'packages'Newtonsoft.Json.8.0.2'lib'portable-net40+sl5+wp80+win8+wpa81'Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..'..'sqlite-net-extensions'SQLiteNetExtensions'SQLiteNetExtensions-PCL.csproj">
<Project>{f723017d-ede5-49cc-a84f-881c067c6004}</Project>
<Name>SQLiteNetExtensions-PCL</Name>
</ProjectReference>
<ProjectReference Include="..'..'sqlitenetpcl'src'SQLite.Net'SQLite.Net.csproj">
<Project>{4971d437-0694-4297-a8cc-146ce08c3bd9}</Project>
<Name>SQLite.Net</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(DynamicImportPath)" />
<!-- Source/Resx etc... -->
</Project>
要回答有关根据配置使项目"可移植"或"正常"的问题,当弹出的错误是关于<TargetFrameworkIdentifier>
时,必须.NETPortable
从配置中删除所有<ProjectTypeGuids>
。
这些项目类型的GUID为Visual Studio提供了一些仅与可移植项目相关的GUI内容,因此当配置不可移植时,它会导致您获得的错误。 但是,由于您可以轻松地编辑XML,因此显然不需要这些东西。 此外,还可以继续使用 GUI 编辑项目属性,并删除项目类型 GUID。
至少从Visual Studio 2015开始。
创建可移植类库时,需要将库创建为 Windows 8.1 可移植库,不能为此使用通用库,这听起来像是受 Xamarin 约束所做的那样。Windows 8.1 可移植库是一种混合库,允许经典 .net 与新的轻量级版本的 .net 进行通信。
可移植类库的问题是它不能通过项目引用引用经典类库,只能通过 DLL 引用来引用它。如果您不习惯编辑该文件,则无需弄乱 XML,只需找到编译的 DLL 并手动添加引用即可。如果选择执行此操作,则需要手动更新生成依赖项,否则引用可能会中断。
似乎问题并不完全在您的.csproj中,但它也存在于您的project.json中。我一直注意到视觉工作室没有生成正确的project.json文件。我必须修改 UI 中的目标才能正确生成。一旦我生成了初始 project.json,我就没有其他问题了。
在做任何事情之前,我们需要连接我们的可移植类库来处理经典类库。第一步是将目标框架从属性更新到目标 .net 4.5 或你喜欢的任何版本。
完成此操作后,请确认 project.json 文件如下所示:
{
"supports": {},
"dependencies": {},
"frameworks": {
".NETPortable,Version=v4.5,Profile=Profile259": {}
}
}
一旦可移植类库面向正确的版本,就可以将 DLL 引用添加到已编译的 .net 4.5 程序集。我没有对可移植类库的 csproj 文件进行其他更改,但这里是它的源代码,因此您可以查看。
请注意 ClassLibrary1 是如何引用的:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)'$(MSBuildToolsVersion)'Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)'$(MSBuildToolsVersion)'Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7C4EB968-4C17-438C-B981-97EDC865F312}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PortableLibrary</RootNamespace>
<AssemblyName>PortableLibrary</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile259</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin'Debug'</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin'Release'</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Properties'AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="ClassLibrary1">
<HintPath>..'ClassLibrary1'bin'Debug'ClassLibrary1.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="project.json" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)'Microsoft'Portable'$(TargetFrameworkVersion)'Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
这应该可以消除假设您的 Web 项目 ASP.NET 核心 1.0 的问题。如果运行的是经典 ASP.NET 应用程序,可能仍需要创建生成规则。
TLDR:
看起来您的 project.json 文件存在问题,请确保它正确生成,并且您不能通过项目引用直接引用 .net 4.5,您必须引用 DLL。