polly .net_使用Try .NET创建出色的交互式文档-Polly NuGet库做到了!

polly .net

polly .net

I've blogged at length about the great open source project called "Polly"

我已经在博客中详细介绍了一个伟大的开源项目“ Polly”

and I've blogged about "Try .NET" which is a wonderful .NET Core global tool that lets you make interactive in-browser documentation and create workshops that can be run both online and locally (totally offline!)

并且我已经在博客中介绍了“ Try .NET”,它是一个出色的.NET Core全局工具,可让您制作交互式浏览器内文档并创建可以在线和本地(完全脱机!)运行的研讨会。

If you've got .NET Core installed, you can try it in minutes! Just do this:

如果您安装了.NET Core ,则可以在几分钟内尝试! 只是这样做:

dotnet tool install --global dotnet-try
dotnet try demo

Even better, you can just clone a Try .NET enabled repository with markdown files that have a few magic herbs and spices, then run "dotnet try" in that cloned folder.

更好的是,您可以仅使用带有一些魔术和香料的markdown文件克隆启用了Try .NET的存储库,然后在该克隆文件夹中运行“ dotnet try ”。

What does this have to do with Polly, the lovely .NET resilience and transient fault handling library that YOU should be using every day? Well, my friends, check out this lovely bit of work by Bryan J Hogan! He's created some interactive workshop-style demos using Try .NET!

这与Polly (您每天应该使用的可爱的.NET弹性和瞬态故障处理库)有什么关系? 好吧,我的朋友们,看看Bryan J Hogan所做的这项可爱的工作! 他使用Try .NET创建了一些交互式的工作坊式演示!

How easy is it to check out? Let's give it a try. I've run dotnet tool install --global dotnet-try already. You may need to run update if you've installed it a while back.

结帐有多容易? 试一试吧。 我已经运行dotnet tool install --global dotnet-try 。 如果您已经安装了更新,则可能需要运行它。

git clone https://github.com/bryanjhogan/trydotnet-polly.git
dotnet try

That's it. What does it do? It'll launch your browser to a local website powered by Try .NET that looks like this!

而已。 它有什么作用? 它会将您的浏览器启动到由Try .NET驱动的本地网站,如下所示!

Interactive local documentation with Try.NET

Sweet! Ah, but Dear Reader, scroll down! Let me try out one of the examples. You'll see a Monaco-based local text editor (the same edit that powers VS Code) and you're able to run - and modify - local code samples IN THE BROWSER!

甜! 啊,但是亲爱的读者,请向下滚动! 让我尝试其中一个示例。 您将看到基于摩纳哥的本地文本编辑器(与VS Code相同的编辑功能),并且您可以在浏览器中运行-并修改-本地代码示例!

Checking out Polly exception retries

Here's the code as text to make it more accessible.

这是文本代码,以使其更易于访问。

RetryPolicy retryPolicy = Policy.Handle<Exception>()
.Retry(3, (exception, retryCount) =>
{
Console.WriteLine($"{exception.GetType()} thrown, retrying {retryCount}.");
});

int result = retryPolicy.Execute(() => errorProneCode.QueryTheDatabase());

Console.WriteLine($"Received a response of {result}.");

And the output appears below the sample, again, in a console within the browser:

输出再次出现在示例下方,在浏览器的控制台中:

System.Exception thrown, retrying 1.
System.InsufficientMemoryException thrown, retrying 2.
Received a response of 0.

You can see that Polly gives you a RetryPolicy that can envelop your code and handle things like transient errors, occasional flaky server responses, or whatever else you want it to do. It can be configured as a policy outside your code, or coded inline fluently like this.

您会看到Polly为您提供了一个RetryPolicy,它可以封装您的代码并处理诸如瞬时错误,偶尔的不稳定服务器响应或您想要执行的其他任何操作。 可以将其配置为代码外的策略,也可以像这样流畅地进行内联编码。

NOTE the URL! See that it's a .MD or Markdown file? Try .NET has a special handler that reads in a regular markdown file and executes it. The result is an HTML representation of your Markdown *and* your sample, now executable!

注意URL! 看到它是.MD或Markdown文件吗? Try .NET具有一个特殊的处理程序,该处理程序读取常规的markdown文件并执行该文件。 结果是Markdown *和*示例HTML表示,现在可以执行了!

What's the page/image above look like as Markdown? Like this:

上面的页面/图像看起来像Markdown吗? 像这样:

# Polly Retries Part 2

### Retrying When an Exception Occurs
The Polly NuGet package has been added and we are going to use the Retry Policy when querying database.
The policy states that if an exception occurs, it will retry up to three times.

Note how you execute the unreliable code inside the policy. `retryPolicy.Execute(() => errorProneCode.QueryTheDatabase());`


``` cs --region retryIfException --source-file .\src\Program.cs --project .\src\PollyDemo.csproj
```

#### Next: [Retrying Based on a Result &raquo;](./retryIfIncorrectStatus.md) Previous: [Before You Add Polly &laquo;](../lettingItFail.md)

Note the special ``` region. The code isn't inline, but rather it lives in a named region in Program.cs in a project in this same repository, neatly under the /src folder. The region is presented in the sample, but as samples are usually more complex and require additional libraries and such, the region name and project context is passed into your app as Try.NET executes it.

注意特殊的```区域。 该代码不是内联的,而是位于同一存储库中项目的Program.cs的命名区域中,位于/ src文件夹下。 该区域显示在示例中,但是由于示例通常更复杂并且需要其他库,因此,Try.NET执行时,区域名称和项目上下文将传递到您的应用程序中。

Go check out some Try .NET enabled sample repositories. Just make sure you have the Try .NET global tool installed, then go clone and "dotnet try" any of these!

去看看一些尝试启用.NET的示例存储库。 只需确保已安装Try .NET全局工具,然后进行克隆并“ dotnet try”即可!

If you're doing classwork, teaching workshops, making assignments for homework, or even working in a low-bandwidth or remote environment this is great as you can put the repositories on a USB key and once they've run once they'll run offline!

如果您正在做课堂作业,教学讲习班,进行家庭作业,甚至在低带宽或远程环境中工作,这都很棒,因为您可以将存储库放在USB密钥上,一旦存储库运行,它们就会运行离线!

Now, be inspired by (and star on GitHub) Bryan's great work and go make your own interactive .NET documentation!

现在,受Bryan的出色工作启发(并在GitHub上出名),并制作自己的交互式.NET文档!

翻译自: https://www.hanselman.com/blog/create-exceptional-interactive-documentation-with-try-net-the-polly-nuget-library-did

polly .net

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值