docs/tutorials/GenerateProxyClientWithCLI/generate-proxy-client.md
You've recently joined a large project, with a distributed team, collaborating on building a Client Application - which could be a Mobile, Desktop, or Web App - and the application being built needs to integrate with a 3rd-Party Service that you don't have much visibility into, or little ability to change.
The 3rd-Party Service does have well defined contracts, deployed to integration-test endpoints, and the Specs are shared with you via an OpenAPI Spec.
You are asked to find a repeatable approach to generate interfaces and DTOs from the spec, based on the above constraints.
Also, since you've come to this repo, we'll assume you want to use NSwag as part of your solution.
NSwag - This process we'll cover requires the NSwag commandline tool to help automate generation of a service client, an interface definition and DTOs. Follow install instructions to install the command line version of nswag.Notes:
nswag version /runtime:Net50 to run nswag on your local machine, since the sample nswag config we'll use specifies runtime as Net50.[YourRemoteService].MainApp > Services > [YourRemoteService], and MainApp > Contracts > [YourRemoteService], if they are missing, create them[YourRemoteService] has a public unauthenticated Open API Spec endpoint, one that gets appropriately versioned before changes are published, you can use it directly.nswag CLI:nswag run sample.nswag /runtime:Net50
MainApp > Services > [YourRemoteService], and MainApp > Contracts > [YourRemoteService] folderssample.nswag configuration based on the starter sample below.MainApp > Services > [YourRemoteService], and MainApp > Contracts > [YourRemoteService], if missing, create themdocumentGenerator.fromDocument.url parameter in the nswag config file. The parameter can point to the local Yaml or Json file you downloaded, or an http address.codeGenerators.openApiToCSharpClient.className parameter in the nswag config file, to something other than SampleServicecodeGenerators.openApiToCSharpClient.namespace parameter in the nswag config file, to something other than MainApp.Services.SampleServicecodeGenerators.openApiToCSharpClient.output parameter in the nswag config file.codeGenerators.openApiToCSharpClient.generateContractsOutput is set to true, and
codeGenerators.openApiToCSharpClient.contractsNamespace parameter in the nswag config file, to something other than MainApp.Services.SampleService.ContractscodeGenerators.openApiToCSharpClient.contractsOutputFilePath parameter in the nswag config file.codeGenerators.openApiToCSharpClient.clientBaseClass to null, and codeGenerators.openApiToCSharpClient.useHttpRequestMessageCreationMethod to falsenswag CLI:nswag run sample.nswag /runtime:Net50
MainApp > Services > [YourRemoteService], and MainApp > Contracts > [YourRemoteService] folders{
"runtime": "Net50",
"documentGenerator": {
"fromDocument": {
"json": "",
"url": "YOUR_OPENAPI_SPEC_LOCATION_HERE",
"output": null,
"newLineBehavior": "Auto"
}
},
"codeGenerators": {
"openApiToCSharpClient": {
"generateClientClasses": true,
"suppressClientClassesOutput": false,
"generateClientInterfaces": true,
"suppressClientInterfacesOutput": false,
"generateDtoTypes": true,
"injectHttpClient": true,
"disposeHttpClient": true,
"generateExceptionClasses": true,
"exceptionClass": "ServiceException",
"wrapDtoExceptions": false,
"useHttpClientCreationMethod": false,
"httpClientType": "System.Net.Http.HttpClient",
"useHttpRequestMessageCreationMethod": true,
"useBaseUrl": true,
"generateBaseUrlProperty": true,
"generateSyncMethods": false,
"exposeJsonSerializerSettings": false,
"clientClassAccessModifier": "public",
"clientBaseClass": "MainApp.Services.BaseService",
"typeAccessModifier": "public",
"generateContractsOutput": true,
"contractsNamespace": "MainApp.Services.SampleService.Contracts",
"contractsOutputFilePath": "GENERATEDCONTRACTS.cs",
"parameterDateTimeFormat": "s",
"generateUpdateJsonSerializerSettingsMethod": true,
"serializeTypeInformation": false,
"queryNullValue": "",
"className": "SampleService",
"operationGenerationMode": "MultipleClientsFromOperationId",
"includedOperationIds": [ "SampleOperationId" ],
"excludedOperationIds": [],
"generateOptionalParameters": false,
"generateJsonMethods": true,
"parameterArrayType": "System.Collections.Generic.IEnumerable",
"parameterDictionaryType": "System.Collections.Generic.IDictionary",
"responseArrayType": "System.Collections.ObjectModel.ObservableCollection",
"responseDictionaryType": "System.Collections.Generic.Dictionary",
"wrapResponses": false,
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"namespace": "MainApp.Services.SampleService",
"requiredPropertiesMustBeDefined": true,
"dateType": "System.DateTime",
"dateTimeType": "System.DateTime",
"timeType": "System.TimeSpan",
"timeSpanType": "System.TimeSpan",
"arrayType": "System.Collections.ObjectModel.ObservableCollection",
"arrayInstanceType": "System.Collections.ObjectModel.ObservableCollection",
"dictionaryType": "System.Collections.Generic.Dictionary",
"arrayBaseType": "System.Collections.ObjectModel.ObservableCollection",
"dictionaryBaseType": "System.Collections.Generic.Dictionary",
"classStyle": "poco",
"generateDefaultValues": true,
"generateDataAnnotations": false,
"excludedTypeNames": [],
"handleReferences": false,
"generateImmutableArrayProperties": false,
"generateImmutableDictionaryProperties": false,
"output": "GENERATEDCODE.cs"
}
}
}