GetCurrentDirectory returns the worker directory of the process started by IIS rather than the app's directory (for example, C:\Windows\System32\inetsrv for w3wp.exe).
which means config loader will not be able to find appsettings.*
files, or any other files such as custom config files, that depend on a GetCurrentDirectory
call. In order to solve it in your Program.cs right after public static void Main(string[] args) {
add the following line
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
https://docs.microsoft.com/en-us/aspnet/core/migration/21-to-22?view=aspnetcore-2.2&tabs=visual-studio#call-configurekestrel-instead-of-usekestrel
https://devblogs.microsoft.com/aspnet/asp-net-core-2-2-0-preview2-now-available/
https://www.cnblogs.com/stulzq/p/10069412.html
var path = Directory.GetCurrentDirectory();
<aspNetCore processPath="dotnet" arguments=".\MyApp.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="InProcess"> <handlerSettings> <handlerSetting name="debugFile" value="aspnetcore-debug.log" /> <handlerSetting name="debugLevel" value="FILE,TRACE" /> </handlerSettings> </aspNetCore>
<aspNetCore processPath="dotnet" arguments=".\MyApp.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="InProcess"> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> <environmentVariable name="CONFIG_DIR" value="f:\application_config" /> </environmentVariables> </aspNetCore>
eventvwr.msc
解决方式
主动设置一下当前目录为程序根目录:
System.IO.Directory.SetCurrentDirectory(hostingEnvironment.ContentRootPath);
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\MyApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" /> </system.webServer> </location> </configuration>
以下 web.config 发布用于独立部署:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath=".\MyApp.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" /> </system.webServer> </location> </configuration>
<PropertyGroup> <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel> </PropertyGroup>
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
Try changing the section in csproj
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
to the following ...
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
<AspNetCoreModuleName>AspNetCoreModule</AspNetCoreModuleName>
</PropertyGroup>
var rand = new Random(1);
var s = Stopwatch.StartNew();
for (var i = 0; i < Count; i++)
{
var ip = new IPAddress(rand.Next(int.MaxValue));
try
{
reader.City(ip);
}
catch (AddressNotFoundException) { }
}
s.Stop();
Console.WriteLine("{0:N0} queries per second", Count / s.Elapsed.TotalSeconds);
RepositoryContext
https://code-maze.com/net-core-web-development-part4/
petapoco
https://blog.csdn.net/weixin_42930928/article/details/89513174
var service = (IFooService)serviceProvider.GetService(typeof(IFooService));
var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
配置
https://medium.com/@kritner/net-core-console-application-ioptions-t-configuration-ae74bfafe1c5
https://stackoverflow.com/questions/38114761/asp-net-core-configuration-for-net-core-console-application
string url1 = UriHelper.GetEncodedPathAndQuery(HttpContext.Request);
string baseUrl = string.Format("{0}://{1}{2}", Request.Scheme, Request.Host, Request.PathBase);
//redirect to register page
string url = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetEncodedPathAndQuery(context.HttpContext.Request);
if (url!=null && url.StartsWith("/"))
{
url = url.Substring(1);
}
context.Result = new RedirectToActionResult("login", "account", new { ReturnUrl = url});
public static string AppBaseUrl => $"{Current.Request.Scheme}://{Current.Request.Host}{Current.Request.PathBase}";
var displayUrl = UriHelper.GetDisplayUrl(Request);
https://sensibledev.com/how-to-get-the-base-url-in-asp-net/