Mon*_*nku 0 c# asp.net asp.net-web-api
我在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();
}
Run Code Online (Sandbox Code Playgroud)
另外,我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
运行代码时,出现file错误configuration.json NOT FOUND。当我在上述函数中检查当前目录的源代码时,我看到Directory.GetCurrentDirectory()返回PATH C:\\Program Files\\IIS Express而不是当前项目目录。
我的问题是为什么将路径设置为IIS目录?我怎样才能解决这个问题 ?
这是Github中报告的ASP.NET Core 2.2中的错误,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
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后按如下所示在SetCurrentDirectory()方法中调用该方法Main:
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();
}
Run Code Online (Sandbox Code Playgroud)
现在一切正常!
| 归档时间: |
|
| 查看次数: |
769 次 |
| 最近记录: |