提交 df8543d8 编写于 作者: 麦壳饼's avatar 麦壳饼

add RunAsEnv Used to run the environment adaptively

上级 b6d308d9
using IoTSharp.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.WindowsServices;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace IoTSharp.Extensions
......@@ -28,5 +32,119 @@ namespace IoTSharp.Extensions
}
});
}
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 = 0;
var handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
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 == 0 && Process32Next(handle, ref processInfo));
if (parentId > 0)
{
return Process.GetProcessById(parentId);
}
return null;
}
private static uint TH32CS_SNAPPROCESS = 2;
[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 = 260)]
public string szExeFile;
}
}
}
\ No newline at end of file
......@@ -45,6 +45,7 @@
<RpmDependency Include="libicu" Version="50.1.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="2.2.0" />
<PackageReference Include="Packaging.Targets" Version="0.1.78" />
</ItemGroup>
......
......@@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IoTSharp.Extensions;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
......@@ -15,8 +16,7 @@ namespace IoTSharp
{
public static void Main(string[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
CreateWebHostBuilder(args).Build().Run();
CreateWebHostBuilder(args).Build().RunAsEnv();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
......
......@@ -17,21 +17,7 @@ IoTSharp is an open-source IoT platform for data collection, processing, visuali
- [Stackoverflow](http://stackoverflow.com/questions/tagged/iotsharp)
## Documentation
## How to install
### Linux
- mkdir /var/iotsharp
- cp ./* /var/iotsharp/
- chmod 777 IoTSharp
- cp iotsharp.service /etc/systemd/system/iotsharp.service
- sudo systemctl enable /etc/systemd/system/iotsharp.service
- sudo systemctl start iotsharp.service
- sudo journalctl -fu iotsharp.service
- http://127.0.0.1:5000/
### Windows
- sc create iotsharp binPath= "D:\iotsharp\IoTSharp.exe" displayname= "IoTSharp" start= auto
## [Document](https://docs.iotsharp.io)
![IotSharp Logo](docs/images/iot_sharp_logo.png)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册