public static IWebHostBuilder UseContentRootAsEnv(this IWebHostBuilder hostBuilder)
{
bool IsWindowsService = false;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using (var process = GetParent(Process.GetCurrentProcess()))
{
IsWindowsService = process != null && process.ProcessName == "services";
}
}
if ( Environment.CommandLine.Contains("--usebasedirectory") || (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && IsWindowsService))
{
hostBuilder.UseContentRoot(AppContext.BaseDirectory);
}
else
{
if (!Debugger.IsAttached)
{
hostBuilder.UseContentRoot(System.IO.Directory.GetCurrentDirectory());
}
}
return hostBuilder;
} public static void RunAsEnv(this IWebHost host)
{
bool IsWindowsService = false; Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using (var process = GetParent(Process.GetCurrentProcess()))
{
IsWindowsService = process != null && process.ProcessName == "services";
}
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && IsWindowsService)
{
System.IO.Directory.SetCurrentDirectory(AppContext.BaseDirectory);
host.RunAsService();
}
else
{
if (Environment.CommandLine.Contains("--usebasedirectory"))
{
System.IO.Directory.SetCurrentDirectory(AppContext.BaseDirectory);
}
host.Run();
}
} private static Process GetParent(Process child)
{
var parentId = ; var handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, ); if (handle == IntPtr.Zero)
{
return null;
} var processInfo = new PROCESSENTRY32
{
dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32))
}; if (!Process32First(handle, ref processInfo))
{
return null;
} do
{
if (child.Id == processInfo.th32ProcessID)
{
parentId = (int)processInfo.th32ParentProcessID;
}
} while (parentId == && Process32Next(handle, ref processInfo)); if (parentId > )
{
return Process.GetProcessById(parentId);
}
return null;
} private static uint TH32CS_SNAPPROCESS = ; [DllImport("kernel32.dll")]
public static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); [DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID); [DllImport("kernel32.dll")]
public static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); [StructLayout(LayoutKind.Sequential)]
public struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string szExeFile;
}

使用方法:

    public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().RunAsEnv();
} public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRootAsEnv()
.UseStartup<Startup>();

Asp.Net Core 自动适应Windows服务、Linux服务、手动启动时的内容路径的扩展方法的更多相关文章

  1. asp.net core 托管到windows服务,并用iis做反向代理

    使用NSSM把.Net Core部署至 Windows 服务   为什么部署至Windows Services 在很多情况下,很少会把.Net Core项目部署至Windows服务中,特别是Asp.n ...

  2. ASP.NET Boilerplate 学习 AspNet Core2 浏览器缓存使用 c#基础,单线程,跨线程访问和线程带参数 wpf 禁用启用webbroswer右键菜单 EF Core 2.0使用MsSql/MySql实现DB First和Code First ASP.NET Core部署到Windows IIS QRCode.js:使用 JavaScript 生成

    ASP.NET Boilerplate 学习   1.在http://www.aspnetboilerplate.com/Templates 网站下载ABP模版 2.解压后打开解决方案,解决方案目录: ...

  3. ASP.NET Core 新建项目(Windows) - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 新建项目(Windows) - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 新建项目(Windows) 对于任何语言和框架,都 ...

  4. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  5. 发布到ASP.NET CORE项目到 Windows server 2012

    原文: https://github.com/zeusro/MarkdownBlog/blob/master/2018/2018-01-17-01.md 发布到ASP.NET CORE项目到 Wind ...

  6. 在 asp.net core 中使用类似 Application 的服务

    在 asp.net core 中使用类似 Application 的服务 Intro 在 asp.net 中,我们可以借助 Application 来保存一些服务器端全局变量,比如说服务器端同时在线的 ...

  7. 如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容?

    原文:如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容? 文章名称: 如何在ASP.NET Core自定义中间件读取Request.Body和 ...

  8. Linux系统开机启动时的工作原理

    Linux系统开机启动时的工作原理也是深入了解Linux系统核心工作原理的一个很好的途径. 启动第一步--加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至 ...

  9. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

随机推荐

  1. 洛谷 P3258 BZOJ 3631 [JLOI2014]松鼠的新家

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...

  2. [poj2396]Buget[上下界可行流]

    题意:有一个n*m的方阵,里面的数字未知,但是我们知道如下约束条件:每一行的数字的和每一列的数字的和某些格子有特殊的大小约束,用大于号,小于号和等于号表示问:是否存在用正数填充这个方阵的方案,满足所有 ...

  3. hdu_1014_Uniform Generator_201310141958

    Uniform Generator Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. asp.net--webservice--安全验证SoapHeader

    服务端的设置 客户端设置,需要生成一个服务端的代理,调用服务端的方法

  5. sql server使用杂记

    SqlServer导出数据库 navcat for sql server中打开连接,打开数据库,右键--数据传输,常规选项卡--模式选择dbo,目标选择连接(选择你新建的库)或者文件(导出你要的sql ...

  6. 【树状数组】POJ 2155 Matrix

    附一篇经典翻译,学习 树状数组  http://www.hawstein.com/posts/binary-indexed-trees.html /** * @author johnsondu * @ ...

  7. vim中凝视多行python代码

    在vim中凝视多行python代码比較麻烦,主要由下面几种方法: (1)将须要凝视的代码以文档字符串的形式呈现 (2)将须要凝视的代码以函数的形式呈现 (3)使用vim自身快捷键 我们主要使用第三种方 ...

  8. How to do IF NOT EXISTS in SQLite

    http://stackoverflow.com/questions/531035/how-to-do-if-not-exists-in-sqlite How about this? INSERT O ...

  9. sqlserver 创建维护计划失败(SQL Server: 14234 错误)自动备份数据库计划

    创建维护计划失败(SQL Server: 14234 错误) (2011-06-25 14:44:24) 转载▼ 标签: 杂谈 分类: MSSQL 错误信息:对于 JobStep ' testBak ...

  10. golang LMDB入门例子——key range查询

    如下,使用gomb库 package main import ( "bytes" "fmt" "io/ioutil" "os&qu ...