为什么"Environment.CurrentDirectory"设置为"C:\Program Files\IIS Express"?



我在https://www.c-sharpcorner.com/article/building-api-gateway-using-ocelot-in-asp-net-core/

我创建了一个空的asp.net web api应用程序,并按照上面链接中提到的步骤进行操作。

Program.cs文件中的My 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目录?我该怎么解决这个问题?

这是ASP.NET Core 2.2中的一个错误,已在Github中报告,Microsoft ASP.NET Core团队提供了如下解决方案,他们将在ASP.NET Core的功能版本中添加此解决方案。

编写一个助手类如下:

public class CurrentDirectoryHelpers
{
internal const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll";
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[System.Runtime.InteropServices.DllImport(AspNetCoreModuleDll)]
private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct IISConfigurationData
{
public IntPtr pNativeApplication;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzFullApplicationPath;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzVirtualApplicationPath;
public bool fWindowsAuthEnabled;
public bool fBasicAuthEnabled;
public bool fAnonymousAuthEnable;
}
public static void SetCurrentDirectory()
{
try
{
// Check if physical path was provided by ANCM
var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");
if (string.IsNullOrEmpty(sitePhysicalPath))
{
// Skip if not running ANCM InProcess
if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)
{
return;
}
IISConfigurationData configurationData = default(IISConfigurationData);
if (http_get_application_properties(ref configurationData) != 0)
{
return;
}
sitePhysicalPath = configurationData.pwzFullApplicationPath;
}
Environment.CurrentDirectory = sitePhysicalPath;
}
catch
{
// ignore
}
}
}

然后在Main方法中调用SetCurrentDirectory()方法,如下所示:

public static void Main(string[] args)
{
CurrentDirectoryHelpers.SetCurrentDirectory(); // call it here

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();
}

现在一切都应该好起来了!

您也可以在进程外运行此程序,直到在.net core 3.0中提供永久修复程序。要更改此设置,您可以从更改csproj文件中的设置

<AspNetCoreHostingModel>inprocess</AspNetCoreHostingModel>

<AspNetCoreHostingModel>outofprocess</AspNetCoreHostingModel>

或者,如果您在IISExpress下运行,您可以在launchsettings.json文件中设置Hosting Model。在Visual Studion和Properties->Debug->Web Server Setting->Hosting Model中右键单击项目文件。将其设置为进程外将添加

"ancmHostingModel": "OutOfProcess" 

到launchsettings.json 中的IIS Express配置文件

相关内容

最新更新