添加Azure Secret时,NSWAG客户端发电机试图生成

发布于 2025-02-04 18:36:27 字数 3880 浏览 3 评论 0原文

在我现在参与的项目中,我们使用dotnet-nswag.dll来生成一个typescript API客户端。我现在正在尝试从使用本地秘密转变为存储在Azure键保管库中的秘密(我希望简化新开发人员对项目的条目)。但是,我遇到了一个问题,即当我使用以下类似的内容时:

builder.ConfigureAppConfiguration((ctx, cfg) =>
{
    if (ctx.HostingEnvironment.IsDevelopment())
    {
        var keyVaultEndpoint = new Uri(Environment.GetEnvironmentVariable("DevEnv_KVUri"));
        cfg.AddAzureKeyVault(keyVaultEndpoint, new DefaultAzureCredential());
    }
});

我不再可以生成NSWAG TypeScript API客户端。我的调查使我发现了NSWAG失败的发现,因为devend_kvuri在一代人中不存在。我在我的lunageSettigns.json中添加了此Env var,并且在测试应用程序时可用。但是,我想指示NSWAG不要尝试包括触发它的任何内容,也可以通过该关键保险库端点。

如果我对URL进行硬编码(并且是我可以访问的密钥vault URL),则一代会通过。生成的客户端没有任何端点指向我的硬编码URL。但是,我不喜欢该解决方案,在这里我必须硬编码(甚至是假的,但一个工作)。

不幸的是,我没有找到解决问题的方法。

编辑1: 执行生成的命令:

dotnet "C:\Users\myname\.nuget\packages\nswag.msbuild\13.16.0\build\../tools/Net50/dotnet-nswag.dll" run nswag.json /variables:Configuration=Debug`

未提供URL时发电机抛出的异常

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'uriString')
   at System.Uri..ctor(String uriString)
   at Lib.KeyVault.Extensions.IWebHostBuilderExtensions.<>c.<UseKeyVault>b__0_1(HostBuilderContext ctx, IConfigurationBuilder bld) in C:\dotnet\net\lib\Lib.KeyVault.Extensions\IWebHostBuilderExtensions.cs:line 50
   at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at NSwag.Commands.ServiceProviderResolver.GetServiceProvider(Assembly assembly) in /_/src/NSwag.Commands/HostApplication.cs:line 61
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiGeneratorCommandEntryPoint.Process(String commandContent, String outputFile, String applicationName) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiGeneratorCommandEntryPoint.cs:line 27
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at NSwag.AspNetCore.Launcher.Program.Main(String[] args) in /_/src/NSwag.AspNetCore.Launcher/Program.cs:line 132
System.InvalidOperationException: Swagger generation failed with non-zero exit code '1'.
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 231
   at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in /_/src/NSwag.Commands/NSwagDocumentBase.cs:line 275
   at NSwag.Commands.NSwagDocument.ExecuteAsync() in /_/src/NSwag.Commands/NSwagDocument.cs:line 81
   at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 85
   at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 32
   at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
   at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
   at NSwag.Commands.NSwagCommandProcessor.ProcessAsync(String[] args) in /_/src/NSwag.Commands/NSwagCommandProcessor.cs:line 61

In the project I am involved with now, we are using dotnet-nswag.dll to generate a typescript api client. I am now trying to switch from using local secrets to secrets stored in azure key vault (I hope to simplify new developers' entry to the project). I however bumped into a problem, that when I use something like below:

builder.ConfigureAppConfiguration((ctx, cfg) =>
{
    if (ctx.HostingEnvironment.IsDevelopment())
    {
        var keyVaultEndpoint = new Uri(Environment.GetEnvironmentVariable("DevEnv_KVUri"));
        cfg.AddAzureKeyVault(keyVaultEndpoint, new DefaultAzureCredential());
    }
});

I no longer can generate the nswag typescript api client. My investigation led me to the discovery that nswag fails becasue DevEnd_KVUri does not exists at the generation time. I have this env var added in my launchSettigns.json and it is available when I test my app. However, I would like to instruct nswag not to try to include whatever is triggering it to also go through that key vault endpoint.

If I hard-code the url (and it is a KeyVault url that I have access to), then the generation passes. Generated client does not have any endpoints pointing to my hard-coded url. However I do not like the solution, where I have to hard-code (not even fake but a working one) my key vault address.

Unfortunately, I did not find any solution to my problem.

Edit 1:
The command that executes generation:

dotnet "C:\Users\myname\.nuget\packages\nswag.msbuild\13.16.0\build\../tools/Net50/dotnet-nswag.dll" run nswag.json /variables:Configuration=Debug`

The exception thrown by the generator when no url is provided

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'uriString')
   at System.Uri..ctor(String uriString)
   at Lib.KeyVault.Extensions.IWebHostBuilderExtensions.<>c.<UseKeyVault>b__0_1(HostBuilderContext ctx, IConfigurationBuilder bld) in C:\dotnet\net\lib\Lib.KeyVault.Extensions\IWebHostBuilderExtensions.cs:line 50
   at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at NSwag.Commands.ServiceProviderResolver.GetServiceProvider(Assembly assembly) in /_/src/NSwag.Commands/HostApplication.cs:line 61
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiGeneratorCommandEntryPoint.Process(String commandContent, String outputFile, String applicationName) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiGeneratorCommandEntryPoint.cs:line 27
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at NSwag.AspNetCore.Launcher.Program.Main(String[] args) in /_/src/NSwag.AspNetCore.Launcher/Program.cs:line 132
System.InvalidOperationException: Swagger generation failed with non-zero exit code '1'.
   at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:line 231
   at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in /_/src/NSwag.Commands/NSwagDocumentBase.cs:line 275
   at NSwag.Commands.NSwagDocument.ExecuteAsync() in /_/src/NSwag.Commands/NSwagDocument.cs:line 81
   at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 85
   at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in /_/src/NSwag.Commands/Commands/Document/ExecuteDocumentCommand.cs:line 32
   at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
   at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
   at NSwag.Commands.NSwagCommandProcessor.ProcessAsync(String[] args) in /_/src/NSwag.Commands/NSwagCommandProcessor.cs:line 61

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文