Skip to content
Rico Suter edited this page Nov 16, 2015 · 23 revisions

The following code snippets show how to automatically generate the client code with a T4 template in Visual Studio.

Note: If you use ReSharper, you should check out ForTea for enhanced Visual Studio T4 editing support.

For a complete T4 template which generates client code, choose two snippets from this page: One to load the Swagger specification (for example from a Swagger file or a Web API assembly) and one to transform the read Swagger specification into code.

From controller in precompiled Web API assembly

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 defaultUrlTemplate = "api/{controller}/{action}/{id}";
	// -------------
	
	var generator = new WebApiAssemblyToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings
	{
		AssemblyPath = Path.GetFullPath(Path.GetDirectoryName(Host.TemplateFile) + assemblyPath), 
		DefaultUrlTemplate = defaultUrlTemplate
	});
	var service = generator.GenerateForSingleController(controllerClass);

	// TODO: Add client code generation (see below)
#>
<#= code #>

After the client code is generated the given .NET DLL file is unloaded.

From a Swagger specification in the project

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.

From a Swagger specification served from a remote server

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.

Generate the client code

TypeScript

Required namespaces:

<#@ import namespace="NSwag.CodeGeneration.ClientGenerators.TypeScript" #>

Required code:

var provider = new CSharpCodeProvider();
var clientGenerator = new SwaggerToTypeScriptGenerator(service, new SwaggerToTypeScriptGeneratorSettings
{
    ClassName = provider.CreateEscapedIdentifier(Path.GetFileNameWithoutExtension(Host.TemplateFile))
});
var code = clientGenerator.GenerateFile();

CSharp

Required namespaces:

<#@ import namespace="NSwag.CodeGeneration.ClientGenerators.CSharp" #>

Required code:

var provider = new CSharpCodeProvider();
var clientGenerator = new SwaggerToCSharpGenerator(service, new SwaggerToCSharpGeneratorSettings
{
    ClassName = provider.CreateEscapedIdentifier(Path.GetFileNameWithoutExtension(Host.TemplateFile)), 
    Namespace = Host.ResolveParameterValue("directiveId", "namespaceDirectiveProcessor", "namespaceHint")
});
var code = clientGenerator.GenerateFile();