【发布时间】:2019-01-11 23:33:29
【问题描述】:
我在 https://www.c-sharpcorner.com/article/building-api-gateway-using-ocelot-in-asp-net-core/ 关注 API 网关示例
我创建了一个空的 asp.net web api 应用程序并按照上面链接中提到的步骤进行操作。
我在 Program.cs 文件中的 Main() 函数是:
public static void Main(string[] args)
{
IWebHostBuilder builder = new WebHostBuilder();
builder.ConfigureServices(s =>
{
s.AddSingleton(builder);
});
builder.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls("http://localhost:9000");
var host = builder.Build();
host.Run();
}
另外,我的 Startup.cs 文件有以下代码:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder();
builder.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("configuration.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
Action<ConfigurationBuilderCachePart> settings = (x) =>
{
x.WithMicrosoftLogging(log =>
{
log.AddConsole(LogLevel.Debug);
}).WithDictionaryHandle();
};
services.AddOcelot();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
await app.UseOcelot();
}
}
当我运行代码时,我收到文件 configuration.json NOT FOUND 的错误。
当我在上述函数中检查当前目录的源代码时,我看到 Directory.GetCurrentDirectory() 返回 PATH 为 C:\\Program Files\\IIS Express 而不是当前项目目录。
我的问题是为什么路径设置为 IIS 目录?我该如何解决这个问题?
【问题讨论】:
-
尝试使用
AppDomain.CurrentDomain.BaseDirectory或System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location。您不能依赖当前工作目录来加载资源,因为它可以随时更改。 -
您使用的是 ASP.NET Core 2.2 InPrpcess 托管模型吗?
-
@TanvirArjel 是的
-
检查我的答案!
标签: c# asp.net asp.net-web-api