How-To's

SourceLink, Consuming APIs, and NuGet Dependent Code in ReSharper and Rider 2019.3

Rider and ReSharper 2019.3 shipped last year already, but we still have a couple of features to tell you about! In this blog post, we will look at a few additions in the area of navigation. These include better support for SourceLink, a new Consuming APIs context navigation, and a Find Dependent Code action for NuGet packages. Without further ado, let’s see these new features in action.

Navigation and Debugging with SourceLink

The idea of SourceLink is to provide a first-class navigation and debugging experience for source code that doesn’t belong to our own codebase, such as NuGet package references. This is achieved by embedding additional information into so-called symbol packages. As a package author, here is a minimal example on how to support SourceLink for your packages:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <PublishRepositoryUrl>true</PublishRepositoryUrl>
    <SymbolPackageFormat>snupkg</SymbolPackageFormat>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
  </ItemGroup>

</Project>

Each hosting provider usually has its own support package for making SourceLink integration smooth and easy. In this example, we are using Microsoft.SourceLink.GitHub. Probably, there will be one for JetBrains Space at some point, too :-) Also, there are a lot more configuration parameters to further tweak SourceLink support.

As a package consumer, life is much easier. Rider and ReSharper supported SourceLink v2 since version 2018.1, but we have now fixed a minor protocol bug to support browsing external source code with ease again. As a reminder, the symbols.nuget.org endpoint is included by default, but the options Use sources from symbol files when available and Allow downloading symbols from remote locations should be enabled for symbols and SourceLink to work when navigating/debugging in external symbols:

Options for External Sources

We are still working on supporting SourceLink with private repositories that require authentication (RIDER-16260).

Finding Consuming APIs

We introduce a new member to the context navigation family: Consuming APIs. This search can be of great help while working with codebases that implement the CQRS pattern. Looking at the creation of an event, how can we efficiently navigate to all the message handlers that are handling this particular event? Invoke the Consuming APIs action:
Find consuming APIs

Thanks to Vladimir for bringing this use case to our attention!

Similar to its older brother Exposing APIs, the Consuming APIs search performs a Find Usage search, but excludes irrelevant symbols like type declarations (generics), constructors, or other new expressions.

Finding dependent code for NuGet references

Whenever feasible, we should consider swapping external library code with our own implementation. This has several benefits. Firstly, we are completely in charge. We can add new features or adapt to our own needs whenever we need. Also, we naturally avoid update issues and version conflicts and can introduce signing more easily. Secondly, and most importantly, we achieve the highest confidence in security, simply because we rely only on our own code!

ReSharper and Rider 2019.3 can help us to decide which NuGet package we may consider for removal. After browsing for a specific package in the Solution Explorer tool window, we can open the context menu for that item, and execute Find Dependent Code action in regards to the owning project:
Find dependent code for NuGet packages

In ReSharper we can even search for a package in context of the full solution by using Find Dependent Code in Solution. Subscribe to RIDER-35024 to stay up-to-date when this feature also gets supported in Rider.

Of course we can group search results in the usual manner. For instance, by project structure, namespace, type, file and combinations of those. However, even more importantly, we can filter results by various criteria, including direct and related usages:
Filter results by direct and related usages

Direct Usage includes code that will definitely break compilation if we remove the NuGet package reference. On the other hand, Related Usage covers code that is related to types from inspected package reference.

Jumping to relative and absolute locations

Last but not least, we addressed a small issue that allows jumping to directories and files by pasting their absolute or relative path into the Search Everywhere popup (RSRP-474817):
Goto relative and absolute paths

Windows users might want to watch RSRP-477646 for updates.

Download ReSharper 2019.3 or give Rider 2019.3 a try. Enjoy the new navigation features and tell us what you think. Every feedback is appreciated!

Discover more

Refactor code to use new C# language features

重构代码以使用新的 C# 语言功能

在使用任何语言时,向现代语言功能进行现代化改造或迁移有助于提高代码的可读性、效率和安全性。在这篇博文中,我们将研究对代码进行现代化改造的方式,从而优化代码。 利用现代语言功能 为何只为使代码现代化就要重构代码?倘若完好,何必修正?有时,人们会有充分的理由去升级或迁移到新版本的平台或语言。例如,语言逐渐演变并添加了功能,以便利用操作系统功能、设备和浏览器功能、云或其他技术。此外,语言更新还包括引入错误修正、更简洁的语法和语法糖,以及更高效的对象和数据处理方式。 对代码进行现代化改造的重构可以通过提高可读性和可维护性来显著提高代码质量。这是因为随着语言的逐渐演变,新添加的语法往往会有助于减少您必须编写的代码行数并降低代码的复杂性。 ReSharper 和 Rider 等工具可以帮助您实现代码库的现代化。它们会随时更新以支持 .NET 语言的新功能,并且可以在有机会充分利用这些功能时提供建议。附带的好处是,这些建议有助于使您的团队不断更新自身的 C# 技能。 顶级语句 顶级语句在 C# 9 中引入,使您无需恪守显式定义命名空间或类的旧规即可立即开始编写代码。在引入顶级语句之前,即使是打印单个“Hello World”风格消息的最基础应用,也需要包含三层嵌套中括号才能支持一行实际运行代码。这样就会牵扯到大量不必要的样板代码。这真的有必要吗? usi