Skip to content

Generated code

For the Weather model in Modeling contracts, the generator emits:

ModelGenerated C#
Structures, including inline operation inputs and outputsRecords such as GetCityInput, GetCityOutput, and CityCoordinates
@error structuresExceptions such as NoSuchResource, thrown by handlers and caught by clients
Lists and mapsIReadOnlyList<T> and IReadOnlyDictionary<string, T> where they are used, with no class for the list or map shape itself
The service, when SmithyGenerateServer is onA handler interface IWeatherHandler composed of one interface per operation, plus AddWeatherHandler and MapWeather extensions for ASP.NET Core
The service, when SmithyGenerateClient is onA WeatherClient with a typed async method per operation
Every shapeA schema class

Operation @examples become <example> code samples in the XML documentation of the generated client methods. Each sample constructs the input, calls the method on a client variable, and shows the expected output, or for an error example a typed try/catch with the expected exception properties.

Each shape’s schema is a class in the child Schemas namespace: Example.Weather.Person has Example.Weather.Schemas.PersonSchema. Import that namespace when you hand a schema to a codec.

A schema carries the shape’s members and its trait values, with typed accessors for each trait. It leaves out traits used only at build time, such as @documentation, @examples, @tags, @deprecated, and test traits. How NSmithy works explains what reads them.

A Smithy name that clashes with a name C# or the generator already uses gets a suffix. Only the colliding name changes; wire names stay as modeled.

ModelGenerated C#
structure DomainName { domainName: String }DomainName.DomainNameValue
A member, enum value, or shape named Equals, ToString, GetHashCode, or another inherited memberEqualsValue
A union variant named value, tag, unknown, match, or like its unionVariant class ValueCase, factory FromValue

A shape that would generate a type named Schemas fails generation, because that name is reserved for the schema namespace.

Generation starts from the service and covers the shapes its operations reach. To generate other shapes, such as the payloads of events a service publishes, declare a shape closure in the model and name it in the closure setting:

model/events.smithy
$version: "2"
metadata shapeClosures = [
{
id: "example.orders#events"
includeBySelector: "structure[trait|tags|(values) = event]"
}
]
smithy-build.json
{
"version": "1.0",
"plugins": {
"csharp-codegen": { "closure": "example.orders#events" }
}
}

A project with a closure and no SmithyService generates only the types in the closure and their schemas. With both, the closure is the generated set and the service, which it must contain, still gets its client and server. A closure covering the service’s namespace generates the service together with every shape declared there:

metadata shapeClosures = [
{ id: "example.orders#all", includeNamespaces: ["example.orders"] }
]

Closures are part of the model, so a contracts package ships them with its model files.