-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
The following code snippets show how to automatically generate the client code with a T4 template in Visual Studio.
The following code loads an ASP.NET Web API assembly and generates the client for a configured Web API controller class.
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NSwag.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NSwag.Core.dll" #>
<#@ import namespace="NSwag.CodeGeneration" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="Microsoft.CSharp" #>
<#@ output extension=".ts" #>
<#
// CONFIGURATION
var assemblyPath = @"../../bin/NSwag.Demo.Web.dll";
var controllerClass = "NSwag.Demo.Web.Controllers.PersonsController";
var urlTemplate = "api/{controller}/{action}/{id}";
// -------------
var fullAssemblyPath = Path.GetFullPath(Path.GetDirectoryName(Host.TemplateFile) + assemblyPath);
var generator = new WebApiAssemblyToSwaggerGenerator(assemblyPath);
var service = generator.Generate(controllerClass, urlTemplate);
// TODO: Add client code generation (see below)
#>
<#= code #>
After the client code is generated the given .NET DLL file is unloaded.
To generate the client code from the Swagger specification which is stored in a file in your project, use the following code in your T4 file:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NSwag.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NSwag.Core.dll" #>
<#@ import namespace="NSwag" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.CSharp" #>
<#@ output extension=".ts" #>
<#
// CONFIGURATION
var filePath = @"../../ServiceDefinitions/MyService.swagger";
// -------------
var fullFilePath = Path.GetFullPath(Path.GetDirectoryName(Host.TemplateFile) + filePath);
var service = SwaggerService.FromJson(File.ReadAllText(fullFilePath));
// TODO: Add client code generation (see below)
#>
<#= code #>
Only the path to the Swagger specification file is required.
To generate the client code from the Swagger specification from a remote server, use the following code in your T4 file:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NSwag.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NSwag.Core.dll" #>
<#@ import namespace="NSwag" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.CSharp" #>
<#@ output extension=".ts" #>
<#
// CONFIGURATION
var url = @"http://localhost:22093/api/Persons/Swagger";
// -------------
var service = SwaggerService.FromUrl(url);
// TODO: Add client code generation (see below)
#>
<#= code #>
Only the URL to where the Swagger specification is served is required.
Required namespaces:
<#@ import namespace="NSwag.CodeGeneration.ClientGenerators.TypeScript" #>
Required code:
var generator = new SwaggerToTypeScriptGenerator(service);
var provider = new CSharpCodeProvider();
generator.Class = provider.CreateEscapedIdentifier(Path.GetFileNameWithoutExtension(Host.TemplateFile));
var code = generator.GenerateFile();
Required namespaces:
<#@ import namespace="NSwag.CodeGeneration.ClientGenerators.CSharp" #>
Required code:
var generator = new SwaggerToCSharpGenerator(service);
var provider = new CSharpCodeProvider();
generator.Class = provider.CreateEscapedIdentifier(Path.GetFileNameWithoutExtension(Host.TemplateFile));
generator.Namespace = Host.ResolveParameterValue("directiveId", "namespaceDirectiveProcessor", "namespaceHint");
var code = generator.GenerateFile();