在我的.Net Core应用程序中,我正在使用Web服务 添加 - >连接服务 - > WCF服务预览(nuget包)并添加了Web服务并使用了服务方法。
但是,现在客户端将Web服务移动到内部Web服务器,并且我无法从我的开发环境访问该服务。所以我无法访问服务方法并构建我的解决方案并发布。
有什么办法可以从配置文件中传递服务URL吗?
例如:
对于Dev Environment - http://dev.svc
对于Prod Environment - http://prod.svc
答案 0 :(得分:0)
是的,你可以。我建议你阅读ASP.NET Core
中关于配置的整篇文章,因为你可能会发现许多有用的东西。通常,您可以使用以下代码获取每个环境的配置文件:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
// note that here we do override the values by specific file for an emvironment
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
// and this line will get the environment variables from server machine
.AddEnvironmentVariables();
Configuration = builder.Build();
}
}
JSON
个文件可能是这样的:
appsettings.json
{
"serviceUrl": "",
}
appsettings.Development.json
{
"serviceUrl": "http://dev.svc",
}
appsettings.Production.json
{
"serviceUrl": "http://prod.svc",
}
另外,您可能会发现Working with multiple environments文章很有用。