Skip to content

SwaggerToCSharpClientGenerator

Rico Suter edited this page Jul 27, 2016 · 52 revisions

Package: NSwag.CodeGeneration

Inherits from ClientGeneratorBase

The following code generates the C# client code and DTO classes from a given Swagger specification:

var service = SwaggerService.FromJson("...");

var settings = new SwaggerToCSharpGeneratorSettings
{
    ClassName = "MyClass", 
    Namespace = "MyNamespace"
};

var generator = new SwaggerToCSharpGenerator(service, settings);	
var code = generator.GenerateFile();

Settings

Class: SwaggerToCSharpGeneratorSettings inherits SwaggerToCSharpGeneratorSettings and ClientGeneratorBaseSettings

Properties:

  • CSharpGeneratorSettings: The C# DTO class generator settings (implemented in NJsonSchema)
    • Namespace: The namespace of the client and DTO classes
  • ClassName: The client class name
  • AdditionalNamespaceUsages
  • ClientBaseClass: Full name of the base class
  • UseHttpClientCreationMethod: Indicates whether to call CreateHttpClientAsync on the base class to create a new HttpClient instance.

Customize the generated client class

Extend the generated partial class

If you generate a C# client class, then it is marked as partial:

Services.cs:

public partial class MyClient
{
    partial void PrepareRequest(HttpClient request, ref string url);

    partial void ProcessResponse(HttpClient request, HttpResponseMessage response);

    ...
}

This way you can extend the class in another file:

Services.Extensions.cs:

public partial class MyClient
{
    public void MyAdditionalMethod() 
    {
        ...
    }

    partial void PrepareRequest(HttpClient request, ref string url)
    {
        // TODO: Prepare the request and modify the URL if needed
    }

    partial void ProcessResponse(HttpClient request, HttpResponseMessage response);
    {
        // TODO: Post-process the response
    }
}

Implement a base class

If the ClientBaseClass setting is set, then the generated client inherits from the class:

public partial class MyClient : MyBaseClass
{
    ...
}

If you need to control how the HttpClient is instantiated (or need another HTTP client class), you can also enable the UseHttpClientCreationMethod setting. This way the HTTP client class is created by the CreateHttpClientAsync method of the base class:

public partial class MyClient : MyBaseClass
{
    public Task MyOperationAsync() 
    {
        ...
        var client = await CreateHttpClientAsync(cancellationToken).ConfigureAwait(false);
        ...
    }
}

The MyBaseClass class would look like this:

public class MyBaseClass
{
    protected async Task<HttpClient> CreateHttpClientAsync(CancellationToken cancellationToken)
    {
        var client = new HttpClient();
        // TODO: Customize HTTP client
        return client; 
    }
}

Provide configuration object

It is possible to specify a configuration class via the ConfigurationClass setting. If the configuration class is set, then the generated client class has an additional constructor accepting on object of the given configuration class. This configuration object is then passed to the base constructor of the base class.

The following code snippet shows the generated client class when ClientBaseClass and ConfigurationClass is set:

public partial class MyClient : MyBaseClass
{
    public MyClient() : this("", null) { }
  
    public MyClient(string baseUrl) : this(baseUrl, null) { }
    
    public MyClient(string baseUrl, MyConfig configuration) : base(configuration)
    {
        BaseUrl = baseUrl; 
    }