【问题标题】:.Net Core web api needs to consume web service from configuration file.Net Core web api 需要使用配置文件中的 web 服务
【发布时间】:2017-04-27 06:37:29
【问题描述】:

在我的 .Net Core 应用程序中,我正在使用 Web 服务 添加 -> 连接的服务 -> WCF 服务预览(nuget 包)并添加 Web 服务并使用服务方法。

但是,现在客户将 Web 服务移至内部 Web 服务器,我无法从我的开发环境访问该服务。所以我无法访问服务方法并构建我的解决方案并发布。

有什么方法可以从配置文件传递服务 URL?
示例:
对于开发环境 - http://dev.svc
对于 Prod 环境 - http://prod.svc

【问题讨论】:

  • Is there any way that I can give the Web Service URL from Configuration file as I do not have access to the service from my development environment。你在问什么?
  • 有什么方法可以从配置文件中传递服务 URL 吗?示例:对于开发环境 - dev.svc 对于生产环境 - prod.svc
  • 您可以为每个环境的不同值使用配置转换msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx
  • 感谢@ColinM 的回复我正在使用.Net Core,我想它会有所不同
  • 我需要更改 Reference.cs 中的任何内容吗?

标签: wcf asp.net-core .net-core asp.net-core-webapi


【解决方案1】:

是的,你可以。我建议您阅读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 文章也可能对您有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2016-03-09
    • 2017-04-25
    相关资源
    最近更新 更多