小编Luk*_*oen的帖子

我可以从 Roslyn 分析器分析上下文获取项目路径或属性吗?

我正在实现一个 Roslyn 分析器,我想根据 csproj 中某些属性的设置方式采取不同的操作。

目前,我通过在使用分析器导入的 props 文件中设置“AdditionalFiles”节点来实现此目的。这指向 .csproj,然后我手动对项目文件进行 xml 解析,查找我关心的属性。

    <ItemGroup>
      <AdditionalFiles Include="$(ProjectPath)" />
    </ItemGroup>   
Run Code Online (Sandbox Code Playgroud)
        private void AnalyzeAdditionalFiles(CompilationStartAnalysisContext context)
        {
            ICompilationStartAnalysisContextWrapper wrappedContext = this.compilationStartAnalysisContextWrapperFactory.Create(context);

            if (wrappedContext.GetAdditionalTexts()
                              .Any(addtionalFile => <xml parse and validate csproj>))
            {
                context.RegisterSyntaxNodeAction(this.AnalyzeSyntaxNode, PossibleSyntaxKinds);
            }
        }
Run Code Online (Sandbox Code Playgroud)

我被告知可能有一种一流的受支持方法来执行其中一个或两个操作,而不需要感觉像黑客版本的:

  • 找到csproj的路径
  • 从 csproj 获取属性

这可能吗?理想情况下,我会寻找道德上的等价物

AnalysisContext.Project.Properties["MyCustomProp"]
Run Code Online (Sandbox Code Playgroud)

c# roslyn roslyn-code-analysis

5
推荐指数
1
解决办法
1439
查看次数

如何通过依赖注入使用 RateLimiter 的 HttpClient DelegatingHandler?

我已经在我的项目中成功使用RateLimiter ( github ) 一段时间了。我最近发现了依赖注入,并尝试按原样迁移我的代码以使用它,但我陷入了 RateLimiter。

文档中的正常用法是

var handler = TimeLimiter
        .GetFromMaxCountByInterval(25, TimeSpan.FromMinutes(1))
        .AsDelegatingHandler();
var Client = new HttpClient(handler)
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试在依赖注入期间复制它

    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddHttpClient<IMyApiClient, MyApiClient>(client => client.BaseAddress = new Uri("https://api.myapi.com/"))
               .AddPolicyHandler(GetRetryPolicy())
               .AddHttpMessageHandler(() => TimeLimiter.GetFromMaxCountByInterval(25, TimeSpan.FromMinutes(1)).AsDelegatingHandler());
     }
Run Code Online (Sandbox Code Playgroud)

我收到错误:

[2021-05-17T21:58:00.116Z] Microsoft.Extensions.Http: The 'InnerHandler' property must be null. 'DelegatingHandler' instances provided to 'HttpMessageHandlerBuilder' must not be reused or cached.
[2021-05-17T21:58:00.117Z] Handler: 'ComposableAsync.DispatcherDelegatingHandler'.
[2021-05-17T21:58:00.122Z] An unhandled host error has occurred.
Run Code Online (Sandbox Code Playgroud)

我的类客户端结构(主要是为了单元测试)如下所示:

using System.Net.Http;
using System.Threading.Tasks;

public class MyApiClient : HumbleHttpClient, …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection dotnet-httpclient .net-core

2
推荐指数
1
解决办法
1529
查看次数