
修改.csproj文件

If you've looked at csproj (C# (csharp) projects) in the past in a text editor you probably looked away quickly. They are effectively MSBuild files that orchestrate the build process. Phrased differently, a csproj file is an instance of an MSBuild file.
如果您过去在文本编辑器中查看过csproj(C#(csharp)项目),则可能会很快移开视线。 它们是有效地编排构建过程的MSBuild文件。 措辞不同,csproj文件是MSBuild文件的实例。
In Visual Studio 2017 and .NET Core 2 (and beyond) the csproj format is MUCH MUCH leaner. There's a lot of smart defaults, support for "globbing" like **/*.cs, etc and you don't need to state a bunch of obvious stuff. Truly you can take earlier msbuild/csproj files and get them down to a dozen lines of XML, plus package references. PackageReferences (references to NuGet packages) should be moved out of packages.config and into the csproj. This lets you manage all project dependencies in one place and gives you and uncluttered view of top-level dependencies.
在Visual Studio 2017和.NET Core 2(及更高版本)中,csproj格式更精简。 有很多智能默认值,支持** / *。cs之类的“ globbing”,并且您不需要声明很多明显的东西。 确实,您可以获取早期的msbuild / csproj文件,并将它们简化为12行XML以及包引用。 PackageReferences(对NuGet包的引用)应移出packages.config并移至csproj中。 这样一来,您可以在一个地方管理所有项目依赖项,并为您提供顶级依赖项的清晰视图。
However, upgrading isn't as simple as "open the old project file and have VS automatically migrate you."
但是,升级并不像“打开旧项目文件并让VS自动迁移您”那样简单。
You have some options when migrating to .NET Core and the .NET Standard.
迁移到.NET Core和.NET Standard时,您有一些选择。
First, and above all, run the .NET Portability Analyzer and find out how much of your code is portable. Then you have two choices.
首先,最重要的是,运行.NET Portability Analyzer,并找出可移植代码的数量。 然后,您有两个选择。
- Great a new project file with something like "dotnet new classlib" and then manually get your projects building from the top (most common ancestor) project down 用“ dotnet new classlib”之类的东西来制作一个新的项目文件,然后手动从顶部(最常见的祖先)项目开始构建您的项目
- Try to use an open source 3rd party migration tool尝试使用开源的第三方迁移工具
Damian on my team recommends option one - a fresh project - as you'll learn more and avoid bringing cruft over. I agree, until there's dozens of projects, then I recommend trying a migration tool AND then comparing it to a fresh project file to avoid adding cruft. Every project/solution is different, so expect to spend some time on this.
我的团队中的达米安(Damian)建议选择一种方案-一个新鲜的项目-因为您会学到更多,并避免产生麻烦。 我同意,直到有数十个项目,然后我建议尝试使用迁移工具,然后将其与新的项目文件进行比较,以避免增加麻烦。 每个项目/解决方案都是不同的,因此希望在此上花费一些时间。
The best way to learn this might be by watching it happen for real. Wade from Salesforce was tasked with upgrading his 4+ year old .NET Framework (Windows) based SDK to portable and open source .NET Core. He had some experience building for older versions of Mono and was thoughtful about not calling Windows-specific APIs so he knows the code is portable. However he needs to migrate the project files and structure AND get the Unit Tests running with "dotnet test" and the command line.
了解这一点的最好方法可能是观看它的真实发生。 来自Salesforce的Wade的任务是将其已有4年以上历史的基于.NET Framework(Windows)的SDK升级到可移植的开源.NET Core。 他有一些针对较旧版本的Mono进行构建的经验,并且考虑不调用Windows特定的API,因此他知道代码是可移植的。 但是,他需要迁移项目文件和结构,并使用“ dotnet test”和命令行来运行单元测试。
I figured I'd give him a head start by actually doing part of the work. It's useful to do this because, frankly, things go wrong and it's not pretty!
我想我应该通过实际做一部分工作来给他一个开端。 这样做很有用,因为坦率地说,事情出了问题,而且很不漂亮!
I started with Hans van Bakel's excellent CsProjToVS2017 global tool. It does an excellent job of getting your project 85% of the way there. To be clear, don't assume anything and not every warning will apply to you. You WILL need to go over every line of your project files, but it is an extraordinarily useful tool. If you have .NET Core 2.1, install it globally like this:
我从Hans van Bakel出色的CsProjToVS2017全局工具开始。 在使项目达到目标的85%方面,它做得非常好。 明确一点,不要承担任何责任,并非所有警告都会对您适用。 您将需要遍历项目文件的每一行,但这是一个非常有用的工具。 如果您具有.NET Core 2.1,请像这样在全局安装它:
dotnet tool install Project2015To2017.Cli --global
Then its called (unfortunately) with another command "csproj-to-2017" and you can pass in a solution or an individual csproj.
然后(不幸的是)使用另一个命令“ csproj-to-2017”调用了它,您可以传入一个解决方案或一个单独的csproj。
After you've done the administrivia of the actual project conversion, you'll also want to make educated decisions about the 3rd party libraries you pull in. For example, if you want to make your project cross-platform BUT you depend on some library that is Windows only, why bother trying to port? Well, many of your favorite libraries DO have "netstandard" or ".NET Standard" versions. You'll see in the video below how I pull Wade's project's reference forward with a new version of JSON.NET and a new NuUnit. By the end we are building and the command line and running tests as well with code coverage.
完成实际项目转换的管理后,您还需要对引入的第三方库做出有根据的决定。例如,如果要使项目跨平台,但您需要依赖某些库那只是Windows,为什么还要麻烦移植呢? 好吧,您最喜欢的许多库确实具有“ netstandard”或“ .NET Standard”版本。 您将在下面的视频中看到如何使用新版本的JSON.NET和新的NuUnit推动Wade项目参考。 到最后,我们将构建命令行,运行测试以及代码覆盖率。
Please head over to my YouTube and check it out. Note this happened live and spontaneously plus I had a YouTube audience giving me helpful comments, so I'll address them occasionally.
请前往我的YouTube并进行检查。 请注意,这种情况是实时发生的,而且是自然而然的,而且我有一个YouTube观众向我提供了有用的评论,因此我会不定期地与他们联系。
LIVE:将较旧的.NET SDK升级到.NET Core和.NET Standard (LIVE: Upgrading an older .NET SDK to .NET Core and .NET Standard)
If you find things like this useful, let me know in the comments and maybe I'll do more of them. Also, do you think things like this belong on the Visual Studio Twitch Channel? Go follow my favs on Twitch CSharpFritz and Noopkat for more live coding fun!
如果您觉得这样的事情有用,请在评论中让我知道,也许我会做更多的事情。 另外,您认为这样的事情属于Visual Studio Twitch Channel吗? 在Twitch CSharpFritz和Noopkat上关注我的收藏,以获取更多现场编码乐趣!
Friend of the Blog: Want to learn more about .NET for free? Join us at DotNetConf! It's a free virtual online community conference September 12-14, 2018. Head over to https://www.dotnetconf.net to learn more and for a Save The Date Calendar Link.
博客之友:想免费了解有关.NET的更多信息? 加入我们的DotNetConf ! 这是2018年9月12日至14日的免费虚拟在线社区会议。 前往https://www.dotnetconf.net了解更多信息并获取“保存日期日历”链接。
修改.csproj文件