一整天我都在试着把它弄好。
我正在通过下面的代码进行依赖注入:
public Startup(IApplicationEnviroment appEnv)
{
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
每次当我执行这段代码时,我都会得到以下错误:
我真的很恼火,因为我不能让它工作,我对它一无所知。我对Asp.Net和C#比较陌生,但这是教程告诉我要做的。大家都知道我的代码问题在哪里吗?
也许这对你有帮助。
#if DEBUG
services.AddScoped<IMailService, DebugMailService>();
#else
services.AddScoped<IMailService, RealMailService>();
#endif
我的界面:
public interface IMailService
{
bool SendMail(string to, string from, string subject, string body);
}
我的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;
}
}
发布于 2016-02-04 06:32:23
有两种可能:
http://json.schemastore.org/project
正如你在下面看到的,智能感知工作得很好,可以找到存在于Microsoft.Extensions.PlatformAbstractions
中的IApplicationEnvironment
。
然而,幸运的是在RC1中,它不再需要在Configuration()
上包含IApplicationEnvironment
中存在的ApplicationBasePath
。这意味着为您的情况向Startup
中注入IApplicationEnvironment
是可选的。我的资料来源:here和here。
所以你可以像这样改变你的启动方法:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build().ReloadOnChanged("appsettings.json");
}
最后,请确保您没有任何版本不匹配,如果您将beta8
与rc1-final
包包含在同一解决方案中,这肯定会导致问题。因为你说你对asp.net和config.json
的使用都是新手,所以告诉我你可能把测试版和RC asp.net核心发行版搞混了。尽管您可以随心所欲地为其命名,但默认命名已更改为appsettings.json
。因此,再次确保在project.json
文件中您的包版本在相同的版本上。
我希望这能帮到你。
https://stackoverflow.com/questions/34520325
复制相似问题