Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IoTSharp
IoTSharp
提交
0ba378b4
IoTSharp
项目概览
IoTSharp
/
IoTSharp
9 个月 前同步成功
通知
15
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
IoTSharp
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
0ba378b4
编写于
11月 03, 2019
作者:
麦壳饼
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add Asp.Net Core HostExtension
上级
6fbe50fc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
221 addition
and
0 deletion
+221
-0
IoTSharp.Extensions.AspNetCore/HostExtension.cs
IoTSharp.Extensions.AspNetCore/HostExtension.cs
+202
-0
IoTSharp.Extensions.AspNetCore/IoTSharp.Extensions.AspNetCore.csproj
...tensions.AspNetCore/IoTSharp.Extensions.AspNetCore.csproj
+19
-0
未找到文件。
IoTSharp.Extensions.AspNetCore/HostExtension.cs
0 → 100644
浏览文件 @
0ba378b4
using
Microsoft.AspNetCore
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.Extensions.Hosting
;
using
Microsoft.AspNetCore.Identity
;
using
Microsoft.AspNetCore.Mvc
;
using
System
;
using
System.Diagnostics
;
using
System.Reflection
;
using
System.Runtime.InteropServices
;
using
System.Security.Cryptography
;
using
System.Text
;
using
Microsoft.Extensions.Configuration
;
namespace
IoTSharp.Extensions.AspNetCore
{
public
static
class
HostExtension
{
public
static
IHostBuilder
ConfigureWindowsServices
(
this
IHostBuilder
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
);
System
.
IO
.
Directory
.
SetCurrentDirectory
(
AppContext
.
BaseDirectory
);
hostBuilder
.
UseWindowsService
();
}
return
hostBuilder
;
}
public
static
IHostBuilder
UseJsonToSettings
(
this
IHostBuilder
hostBuilder
,
string
filename
)
{
return
hostBuilder
.
ConfigureAppConfiguration
(
builder
=>
{
try
{
if
(
System
.
IO
.
File
.
Exists
(
filename
))
{
builder
.
AddJsonFile
(
filename
,
true
);
}
}
catch
(
System
.
Exception
ex
)
{
System
.
Diagnostics
.
Debug
.
WriteLine
(
ex
.
Message
);
}
});
}
/// Hashes an email with MD5. Suitable for use with Gravatar profile
/// image urls
public
static
string
Gravatar
(
this
IdentityUser
user
)
{
string
email
=
user
.
Email
;
// Create a new instance of the MD5CryptoServiceProvider object.
MD5
md5Hasher
=
MD5
.
Create
();
// Convert the input string to a byte array and compute the hash.
byte
[]
data
=
md5Hasher
.
ComputeHash
(
Encoding
.
Default
.
GetBytes
(
email
));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder
sBuilder
=
new
StringBuilder
();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for
(
int
i
=
0
;
i
<
data
.
Length
;
i
++)
{
sBuilder
.
Append
(
data
[
i
].
ToString
(
"x2"
));
}
return
string
.
Format
(
"http://www.gravatar.com/avatar/{0}"
,
sBuilder
.
ToString
());
;
// Return the hexadecimal string.
}
public
static
BadRequestObjectResult
ExceptionRequest
(
this
ControllerBase
@base
,
Exception
exception
)
{
MethodBase
mb
=
new
StackTrace
(
exception
).
GetFrame
(
0
).
GetMethod
();
MethodBase
cu
=
new
StackTrace
(
true
).
GetFrame
(
0
).
GetMethod
();
return
@base
.
BadRequest
(
new
{
exception
.
Message
,
exception
.
StackTrace
,
ExceptionMethod
=
mb
.
DeclaringType
.
FullName
+
"."
+
mb
.
Name
,
MethodName
=
cu
.
Name
});
}
public
static
BadRequestObjectResult
ExceptionRequest
<
T
>(
this
ControllerBase
@base
,
T
code
,
string
msg
,
Exception
exception
)
{
MethodBase
mb
=
new
StackTrace
(
exception
).
GetFrame
(
0
).
GetMethod
();
MethodBase
cu
=
new
StackTrace
(
true
).
GetFrame
(
0
).
GetMethod
();
return
@base
.
BadRequest
(
new
{
code
,
msg
,
data
=
new
{
ExceptionMethod
=
mb
.
DeclaringType
.
FullName
+
"."
+
mb
.
Name
,
MethodName
=
cu
.
Name
}
});
}
public
static
BadRequestObjectResult
ExceptionRequest
(
this
ControllerBase
@base
,
int
code
,
string
msg
,
Exception
exception
)
=>
ExceptionRequest
<
int
>(
@base
,
code
,
msg
,
exception
);
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
;
}
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
;
}
}
}
IoTSharp.Extensions.AspNetCore/IoTSharp.Extensions.AspNetCore.csproj
0 → 100644
浏览文件 @
0ba378b4
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="GitVersionTask" Version="5.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.6.0" />
</ItemGroup>
</Project>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录