gpt4 book ai didi

c# - 生成 NSwag 客户端作为构建的一部分

转载 作者:行者123 更新时间:2023-12-03 17:01:50 26 4
gpt4 key购买 nike

我有一个项目,它使用 NSwag 从 swagger 文件生成客户端和契约(Contract)。我不希望 git 跟踪这些生成的文件,因此当项目在构建服务器上构建时,它会将它们作为构建的一部分生成。

我一直在尝试使用 MSBuild 目标来尝试让它工作,它生成文件但随后构建失败,因为还有一些其他类引用了生成的类。

这是我目前在 csproj 文件中的内容:

<Target Name="NSwag" BeforeTargets="BeforeBuild;BeforeRebuild">
<Exec Command="$(NSwagExe_Core21) run nswag.json /variables:Configuration=$(Configuration)" IgnoreExitCode="true" />
</Target>

这有可能吗?

编辑

Nswag 规范在这里:
{
"runtime": "NetCore21",
"defaultVariables": null,
"swaggerGenerator": {
"fromSwagger": {
"url": "swagger.json",
"output": null
}
},
"codeGenerators": {
"swaggerToCSharpClient": {
"clientBaseClass": "ClientBase", //name of your client base class
"configurationClass": null,
"generateClientClasses": true,
"generateClientInterfaces": true,
"generateDtoTypes": true,
"injectHttpClient": true,
"disposeHttpClient": true,
"protectedMethods": [],
"generateExceptionClasses": true,
"exceptionClass": "SwaggerException",
"wrapDtoExceptions": true,
"useHttpClientCreationMethod": false,
"httpClientType": "System.Net.Http.HttpClient",
"useHttpRequestMessageCreationMethod": true, //allows you to add headers to each message
"useBaseUrl": true,
"generateBaseUrlProperty": true,
"generateSyncMethods": false,
"exposeJsonSerializerSettings": false,
"clientClassAccessModifier": "internal", //make client generated client implementations internal
"typeAccessModifier": "public", //make your models and client interfaces public
"generateContractsOutput": true,
"contractsNamespace": "MyNamspace.Client.Contracts",
"contractsOutputFilePath": "Contracts.g.cs",
"parameterDateTimeFormat": "s",
"generateUpdateJsonSerializerSettingsMethod": true,
"serializeTypeInformation": false,
"queryNullValue": "",
"className": "CorvetteClient",
"operationGenerationMode": "MultipleClientsFromOperationId",
"additionalNamespaceUsages": [],
"additionalContractNamespaceUsages": [],
"generateOptionalParameters": false,
"generateJsonMethods": true,
"enforceFlagEnums": false,
"parameterArrayType": "System.Collections.Generic.IEnumerable",
"parameterDictionaryType": "System.Collections.Generic.IDictionary",
"responseArrayType": "System.Collections.Generic.ICollection",
"responseDictionaryType": "System.Collections.Generic.IDictionary",
"wrapResponses": false,
"wrapResponseMethods": [],
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"namespace": "MyNamespace.Client",
"requiredPropertiesMustBeDefined": true,
"dateType": "System.DateTimeOffset",
"jsonConverters": null,
"dateTimeType": "System.DateTimeOffset",
"timeType": "System.TimeSpan",
"timeSpanType": "System.TimeSpan",
"arrayType": "System.Collections.Generic.ICollection",
"arrayInstanceType": "System.Collections.ObjectModel.Collection",
"dictionaryType": "System.Collections.Generic.IDictionary",
"dictionaryInstanceType": "System.Collections.Generic.Dictionary",
"arrayBaseType": "System.Collections.ObjectModel.Collection",
"dictionaryBaseType": "System.Collections.Generic.Dictionary",
"classStyle": "Poco",
"generateDefaultValues": true,
"generateDataAnnotations": true,
"excludedTypeNames": [],
"handleReferences": false,
"generateImmutableArrayProperties": false,
"generateImmutableDictionaryProperties": false,
"jsonSerializerSettingsTransformationMethod": null,
"inlineNamedDictionaries": false,
"inlineNamedTuples": true,
"templateDirectory": null,
"typeNameGeneratorType": null,
"propertyNameGeneratorType": null,
"enumNameGeneratorType": null,
"serviceHost": null,
"serviceSchemes": null,
"output": "Client.g.cs"
}
}
}

最佳答案

我设法解决了使用干净目标播放的相同问题并包含源...

<!--Custom task to generate source code from OpenApi Specification before compilation-->
<Target Name="GenerateSources" BeforeTargets="Compile">
<Exec Command="$(NSwagExe_Core31) run config.nswag" />
</Target>

<!--Custom task to remove generate source code before clean project-->
<Target Name="RemoveGenerateSources" BeforeTargets="CoreClean">
<RemoveDir Directories="bin/debug/GeneratedSources" />
</Target>

<!--Register generated source code as project source code-->
<ItemGroup>
<Compile Include="bin/debug/GeneratedSources/**/*.cs" />
</ItemGroup>

关于c# - 生成 NSwag 客户端作为构建的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59393267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com