Richardsonke edited this page Jan 25, 2018 · 21 revisions

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

Important: The sample code is outdated and must be updated!

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.

Note: Project may need to be built at least once after adding nuget package in order for the assemblies to be available in the output directory

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="System.Runtime" #>
<#@ assembly name="$(TargetDir)\NJsonSchema.dll" #>
<#@ assembly name="$(TargetDir)\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(TargetDir)\NSwag.Core.dll" #>
<#@ assembly name="$(TargetDir)\NSwag.CodeGeneration.dll" #>
<#@ assembly name="$(TargetDir)\NSwag.AssemblyLoader.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 document = 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="System.Runtime" #>
<#@ assembly name="$(TargetDir)\NJsonSchema.dll" #>
<#@ assembly name="$(TargetDir)\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(TargetDir)\NSwag.Core.dll" #>
<#@ assembly name="$(TargetDir)\NSwag.CodeGeneration.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 document = SwaggerDocument.FromJsonAsync(File.ReadAllText(fullFilePath)).GetAwaiter().GetResult();;

	// 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="System.Runtime" #>
<#@ assembly name="$(TargetDir)\NJsonSchema.dll" #>
<#@ assembly name="$(TargetDir)\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(TargetDir)\NSwag.Core.dll" #>
<#@ assembly name="$(TargetDir)\NSwag.CodeGeneration.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 document = SwaggerDocument.FromUrlAsync(url).GetAwaiter().GetResult();

	// 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 assemblies:

<#@ assembly name="$(TargetDir)\NJsonSchema.CodeGeneration.TypeScript.dll" #>

Required namespaces:

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

Required code:

var provider = new CSharpCodeProvider();
var clientGenerator = new SwaggerToTypeScriptClientGenerator(document, new SwaggerToTypeScriptClientGeneratorSettings
{
	ClassName = provider.CreateEscapedIdentifier(Path.GetFileNameWithoutExtension(Host.TemplateFile))
});
var code = clientGenerator.GenerateFile();

CSharp

Required assemblies:

<#@ assembly name="$(TargetDir)\NJsonSchema.CodeGeneration.CSharp.dll" #>

Required namespaces:

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

Update output extension:

<#@ output extension=".cs" #>

Required code:

var provider = new CSharpCodeProvider();
var settings = new SwaggerToCSharpClientGeneratorSettings
{
	ClassName = provider.CreateEscapedIdentifier(Path.GetFileNameWithoutExtension(Host.TemplateFile)),
};
settings.CSharpGeneratorSettings.Namespace = Host.ResolveParameterValue("directiveId", "namespaceDirectiveProcessor", "namespaceHint");
var clientGenerator = new SwaggerToCSharpClientGenerator(document, settings);
var code = clientGenerator.GenerateFile();
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.