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:
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?
#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;
}
}
There are two possibilities it could be:
http://json.schemastore.org/project
As you can see below intellisense works fine and finds IApplicationEnvironment
, which exists in Microsoft.Extensions.PlatformAbstractions
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With