我正在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()
返回的路径为C:\\Program Files\\IIS Express
,而不是当前项目目录。
我的问题是为什么将路径设置为IIS目录?我该如何解决?
答案 0 :(得分:4)
这是 ASP.NET Core 2.2 中的一个错误,该错误已在Github中进行了报告,Microsoft ASP.NET Core团队已提供了以下解决方案,并将在以下位置添加此解决方案: ASP.NET Core的功能版本。
编写如下的帮助类:
function checkFlash() {
var hasFlash = false;
try {
var flash =
navigator.mimeTypes &&
navigator.mimeTypes["application/x-shockwave-flash"]
? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin
: 0;
if (flash) hasFlash = true;
} catch (e) {
if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined)
hasFlash = true;
}
return hasFlash;
}
然后按如下所示在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
}
}
}
方法中调用SetCurrentDirectory()
方法:
Main
现在一切正常!
答案 1 :(得分:0)
在.net core 3.0中提供永久修复程序之前,您也可以不进行此操作。 要更改此设置,您可以从以下位置更改csproj文件中的设置:
<AspNetCoreHostingModel>inprocess</AspNetCoreHostingModel>
到
<AspNetCoreHostingModel>outofprocess</AspNetCoreHostingModel>
或者,如果您正在IISExpress下运行,则可以在launchsettings.json文件中设置主机模型。在Visual Studion和属性->调试-> Web服务器设置->主机模型中,右键单击项目文件。 将其设置为“未处理”将添加
"ancmHostingModel": "OutOfProcess"
到launchsettings.json中的IIS Express配置文件