Skip to content

MSBuild Configuration

SourceGen.Ioc reads several MSBuild properties during generation.

Start with this minimal setup in your .csproj:

xml
<PropertyGroup>
    <SourceGenIocName>MyApp</SourceGenIocName>
</PropertyGroup>

Properties Reference

PropertyDefaultDescription
SourceGenIocNameAssembly nameName used by generated Add{Name} extension methods.
SourceGenIocDefaultLifetimeTransientFallback lifetime when no explicit/default lifetime is provided.
SourceGenIocFeaturesRegister,Container,PropertyInject,MethodInjectFeature flags controlling generated outputs and injection member support.
RootNamespaceAssembly nameNamespace used for generated code output.

SourceGenIocName

Use this to set the generated registration method name:

xml
<PropertyGroup>
    <SourceGenIocName>MyApp</SourceGenIocName>
</PropertyGroup>
Generated Code
csharp
// <auto-generated/>
public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddMyApp(this IServiceCollection services)
    {
        // registration code...
        return services;
    }
}

SourceGenIocDefaultLifetime

Use this to change global lifetime fallback:

xml
<PropertyGroup>
    <SourceGenIocDefaultLifetime>Scoped</SourceGenIocDefaultLifetime>
</PropertyGroup>

Lifetime decision order is:

  1. Explicit lifetime on [IocRegister] / [IocRegisterFor]
  2. Matching [IocRegisterDefaults]
  3. SourceGenIocDefaultLifetime
  4. Built-in fallback Transient
Generated Code
csharp
// <auto-generated/>
// No explicit lifetime on attribute, and no matching defaults
// -> generator uses SourceGenIocDefaultLifetime=Scoped
services.AddScoped<global::MyNamespace.MyService, global::MyNamespace.MyService>();

SourceGenIocFeatures

Enable/disable generated outputs and injection member kinds:

xml
<PropertyGroup>
    <SourceGenIocFeatures>Register,Container,PropertyInject</SourceGenIocFeatures>
</PropertyGroup>

Available Feature Flags

FlagDescription
RegisterGenerate IServiceCollection extension output.
ContainerGenerate container output for [IocContainer] classes.
PropertyInjectEnable [IocInject] support on properties.
FieldInjectEnable [IocInject] support on fields.
MethodInjectEnable [IocInject] support on methods.

NOTE

FieldInject is available but not included in the default feature set. Add it explicitly when you want field injection generation.

Parsing behavior:

  • Comma-separated values.
  • Case-insensitive matching.
  • Whitespace around values is ignored.
  • Invalid values are ignored.
Generated Code
csharp
// <auto-generated/>
// SourceGenIocFeatures: Register,Container,PropertyInject
// MethodInject and FieldInject disabled -> only property injection is emitted.
services.AddSingleton<global::MyNamespace.MyService>((global::System.IServiceProvider sp) =>
{
    var s0_p0 = sp.GetRequiredService<global::MyNamespace.IMyDependency>();
    var s0 = new global::MyNamespace.MyService() { Dependency = s0_p0 };
    return s0;
});

WARNING

If a member uses [IocInject] but its feature flag is disabled, analyzer SGIOC022 is reported.

RootNamespace

The generator uses RootNamespace for output namespaces. If not set, assembly name is used.

xml
<PropertyGroup>
    <RootNamespace>MyCompany.MyProduct</RootNamespace>
</PropertyGroup>
Generated Code
csharp
// <auto-generated/>
namespace MyCompany.MyProduct;

public static class ServiceCollectionExtensions
{
    // ...
}

← Back to Overview

Released under the MIT License.