diff --git a/IoTSharp/Extensions/MiscExtension.cs b/IoTSharp/Extensions/MiscExtension.cs
index d624506a05ce3832dcab0d1e720fd7787ba60abb..1a69b80d1e9521164ea4ea6e456b6769793078ef 100644
--- a/IoTSharp/Extensions/MiscExtension.cs
+++ b/IoTSharp/Extensions/MiscExtension.cs
@@ -1,8 +1,12 @@
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
diff --git a/IoTSharp/IoTSharp.csproj b/IoTSharp/IoTSharp.csproj
index 80ca8b66487a953d1708e7fb5164f5a46b2b1cc8..b42165ab78bdc3bfec5ef9df0b66b5214a120f6a 100644
--- a/IoTSharp/IoTSharp.csproj
+++ b/IoTSharp/IoTSharp.csproj
@@ -45,6 +45,7 @@
+
diff --git a/IoTSharp/Program.cs b/IoTSharp/Program.cs
index 66b2bf8dfca7efc6306a156cd4bc54883cc99e0e..b4dd098c94cfe607fac00cbfbb04f50e7703c542 100644
--- a/IoTSharp/Program.cs
+++ b/IoTSharp/Program.cs
@@ -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) =>
diff --git a/README.md b/README.md
index 46230deca1d15dd803070aebcb2f2c81d7cd10f0..4b61d579cab3f2d832857cd65c466b78624b4247 100644
--- a/README.md
+++ b/README.md
@@ -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)