Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection IApplicationEnvironment Error

The whole day I am trying to get this working.

I am doing a dependency injection via this code:

public Startup(IApplicationEnviroment appEnv)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(appEnv.ApplicationBasePath)
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();

    Configuration = builder.Build();
}

Everytime when I am executing this code I am getting the following error:

enter image description here

I am really annoyed of that because I can't get it working and I have no clue about it. I am relatively new to Asp.Net and C# but this is how the tutorial said me to do. Does everyone know what my problem about the code is?


Maybe this helps.

#if DEBUG
        services.AddScoped<IMailService, DebugMailService>();
#else
        services.AddScoped<IMailService, RealMailService>();
#endif

My Interface:

public interface IMailService
{
    bool SendMail(string to, string from, string subject, string body);
}

My DebugMailService

public class DebugMailService : IMailService
{
    public bool SendMail(string to, string from, string subject, string body)
    {
        Debug.WriteLine($"Sending mail: To: {to}, Subject: {subject}");
        return true;
    }
}
like image 481
Tom el Safadi Avatar asked Dec 29 '15 22:12

Tom el Safadi


1 Answers

There are two possibilities it could be:

  1. Your json schema in project.json is pointing to the wrong place. Mine is http://json.schemastore.org/project
  2. Your intellisense might be giving problems. The usual visual studio restart works most times, but if that doesn't, stackoverflow has a lot of responses to fix that. Just search for it.

As you can see below intellisense works fine and finds IApplicationEnvironment, which exists in Microsoft.Extensions.PlatformAbstractions.

IApplicationEnvironment

However, luckily in RC1, its no longer required to include ApplicationBasePath on Configuration(), which exists in IApplicationEnvironment. Meaning its optional to inject IApplicationEnvironment into Startup for your case. My sources: here and here.

So you can just change your Startup method like this:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddEnvironmentVariables();

    Configuration = builder.Build().ReloadOnChanged("appsettings.json");
}

Lastly, make sure you dont have any version mismatches, it will definitely causes issues if you include beta8 with rc1-final packages in the same solution. Since you said you are new to asp.net and also the use of config.json, tells me you might be confusing beta versions with RC asp.net core releases. Even though you can name it whatever you want, default naming was changed to appsettings.json. So again, make sure your package versions in project.json file is on the same release.

I hope this helps.

like image 184
Nick De Beer Avatar answered Oct 18 '22 23:10

Nick De Beer