1

I'm building a .net core 3.1 web application and I'm experimenting with the builtin dependency injection.

I would like to inject a different service based on the runtime environment in which the application is running, I thought that I could use an attribute to define if the service is suitable for the environment, for example:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddTransient<IOperation, OperationDevelopment>();
    services.AddTransient<IOperation, OperationStaging>();
    services.AddTransient<IOperation, OperationProduction>();

    ...
}


public interface IOperation
{
    Guid OperationId { get; }
}

[Development]
public class OperationDevelopment : IOperation
{
}

[Staging]
public class OperationStaging : IOperation
{
}

[Production]
public class OperationProduction : IOperation
{
}

What should I do, skip the registration? Register all and then resolve the suitable service? Something that I missed?

If the .net core DI is too basic, what should I use?

Thanks

3 Answers 3

5

Can you use a simple if inside the method? Hope it helps

if (Environment.IsDevelopment())
{ 
    services.AddTransient<IOperation, OperationDevelopment>();
}
else
{
    services.AddTransient<IOperation, OperationProduction>();
}
2
  • There are Environment.IsStaging() and Environment.IsProduction() methods if it helps the author
    – feihoa
    Dec 27, 2019 at 14:40
  • Well your answer is good but it doesn't register or resolve the service based on the attribute.
    – Fabio
    Dec 27, 2019 at 16:03
3

Suppose in your appsettings.json,if you have configured the environment variable like this

"environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }

then in startup.cs ,capture the environment variable value

 public class Startup
    {
        public Startup(IConfiguration configuration, IWebHostEnvironment environment)
        {
            Configuration = configuration;
            Environment = environment;  
        }

        public IConfiguration Configuration { get; }
        public IWebHostEnvironment Environment { get; }

In configureServices capture host environment configuration

public void ConfigureServices(IServiceCollection services)
        {

            string currentEnvironment= Environment.EnvironmentName;
            if(currentEnvironment== "Development")
            {
                services.AddTransient<IOperation, OperationDevelopment>();
            }
            else if (currentEnvironment == "Staging")
            {
                services.AddTransient<IOperation, OperationStaging>();
            }
            else if (currentEnvironment == "Production")
            {
                services.AddTransient<IOperation, OperationProduction>();
            }
        }

IWebHostEnvironment is for .netcore 3.0 (IHostEnvironment is for .net core 2.2)

1

You could do something like this:

interface IOperationAttribute
{
}

[Development]
public class OperationDevelopment : IOperation
{
    public Guid OperationId { get; }
}

public class DevelopmentAttribute : Attribute, IOperationAttribute
{
}

Add a new extension for IServiceCollection

public static class AppServiceExtension
{    
    public static IServiceCollection AppOperationServices(this IServiceCollection services)
    {
        var typesWithOperationAttribute =
            from a in AppDomain.CurrentDomain.GetAssemblies()
            from t in a.GetTypes()
            let attributes = t.GetCustomAttributes(typeof(IOperationAttribute), true)
            where attributes != null && attributes.Length > 0
            select new { Type = t };

        foreach (var item in typesWithOperationAttribute)
            services.AddTransient((Type)item.Type);

        return services;
    }
}

And in ConfigureServices just call

services.AppOperationServices();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.