Skip to content

Commit

Permalink
添加Dapr集成事件操作
Browse files Browse the repository at this point in the history
  • Loading branch information
UtilCore committed Aug 28, 2023
1 parent 4b53b78 commit 766b0b4
Show file tree
Hide file tree
Showing 111 changed files with 3,639 additions and 411 deletions.
7 changes: 7 additions & 0 deletions Util.sln
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04-Util.Microservices.Healt
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Util.Microservices.Dapr.WebApiSample", "test\Util.Microservices.Dapr.WebApiSample\Util.Microservices.Dapr.WebApiSample.csproj", "{AE19ED93-AFC4-4F91-93BD-B00D343D3BA0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Util.Microservices.Dapr.PubsubSample", "test\Util.Microservices.Dapr.PubsubSample\Util.Microservices.Dapr.PubsubSample.csproj", "{BEEA3FA6-D6B6-48F5-AE6D-9625430B7A86}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -747,6 +749,10 @@ Global
{AE19ED93-AFC4-4F91-93BD-B00D343D3BA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE19ED93-AFC4-4F91-93BD-B00D343D3BA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE19ED93-AFC4-4F91-93BD-B00D343D3BA0}.Release|Any CPU.Build.0 = Release|Any CPU
{BEEA3FA6-D6B6-48F5-AE6D-9625430B7A86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BEEA3FA6-D6B6-48F5-AE6D-9625430B7A86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEEA3FA6-D6B6-48F5-AE6D-9625430B7A86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEEA3FA6-D6B6-48F5-AE6D-9625430B7A86}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -898,6 +904,7 @@ Global
{DF1E55A5-7EA8-41B2-BEBA-72544D256069} = {547FD391-A56B-47A6-B20C-5FC04FA15C56}
{9C58E50F-0589-4A4E-B94D-2EC0BF02F61F} = {1DEC52C8-6692-4DA7-AE34-B76D728F895A}
{AE19ED93-AFC4-4F91-93BD-B00D343D3BA0} = {547FD391-A56B-47A6-B20C-5FC04FA15C56}
{BEEA3FA6-D6B6-48F5-AE6D-9625430B7A86} = {547FD391-A56B-47A6-B20C-5FC04FA15C56}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94347832-A36D-4C42-9C4D-B848BD4F5DA9}
Expand Down
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>7</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionPatch>17</VersionPatch>
<VersionPatch>20</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace Util.Applications.Controllers;
/// </summary>
[ApiController]
[Route( "api/[controller]" )]
[ExceptionHandler]
[ErroLogFilter]
[ExceptionHandler( Order = 1 )]
[ErrorLogFilter( Order = 2 )]
public abstract class WebApiControllerBase : ControllerBase {
/// <summary>
/// 会话
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace Util.Applications.Filters;
/// <summary>
/// 错误日志过滤器
/// </summary>
public class ErroLogFilterAttribute : ExceptionFilterAttribute {
public class ErrorLogFilterAttribute : ExceptionFilterAttribute {
/// <summary>
/// 异常处理
/// </summary>
public override void OnException( ExceptionContext context ) {
if( context == null )
return;
var log = context.HttpContext.RequestServices.GetService<ILogger<ErroLogFilterAttribute>>();
var log = context.HttpContext.RequestServices.GetService<ILogger<ErrorLogFilterAttribute>>();
var exception = context.Exception.GetRawException();
if( exception is Warning warning ) {
log.LogWarning( warning, exception.Message );
Expand Down
8 changes: 8 additions & 0 deletions src/Util.AspNetCore/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ namespace Util;
/// AspNetCore操作扩展
/// </summary>
public static class AppBuilderExtensions {
/// <summary>
/// 配置授权访问控制
/// </summary>
/// <param name="builder">应用生成器</param>
public static IAppBuilder AddAcl( this IAppBuilder builder ) {
return builder.AddAcl<DefaultPermissionManager>();
}

/// <summary>
/// 配置授权访问控制
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions src/Util.AspNetCore/Helpers/Web.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ public static class Web {

#endregion

#region Body(请求正文)

/// <summary>
/// 请求正文
/// </summary>
public static byte[] Body {
get {
Request.EnableBuffering();
return File.ReadToBytes( Request.Body );
}
}

#endregion

#region GetBodyAsync(获取请求正文)

/// <summary>
/// 获取请求正文
/// </summary>
public static async Task<byte[]> GetBodyAsync() {
Request.EnableBuffering();
return await File.ReadToBytesAsync( Request.Body );
}

#endregion

#region Environment(主机环境)

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Util.Applications;

namespace Util.Security.Authorization;
namespace Util.Security.Authorization;

/// <summary>
/// 授权中间件结果处理器
Expand All @@ -16,8 +14,9 @@ public class AclMiddlewareResultHandler : IAuthorizationMiddlewareResultHandler
/// </summary>
public async Task HandleAsync( RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult ) {
if ( authorizeResult.Succeeded == false ) {
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync( new { Code = StateCode.Unauthorized } );
var factory = context.RequestServices.GetRequiredService<IUnauthorizedResultFactory>();
context.Response.StatusCode = factory.HttpStatusCode;
await context.Response.WriteAsJsonAsync( factory.CreateResult( context ) );
return;
}
await defaultHandler.HandleAsync( next, context, policy, authorizeResult );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Util.Security.Authorization;

/// <summary>
/// 未授权返回结果工厂
/// </summary>
public interface IUnauthorizedResultFactory : ISingletonDependency {
/// <summary>
/// 设置Http状态码
/// </summary>
int HttpStatusCode { get; }
/// <summary>
/// 创建未授权返回结果
/// </summary>
/// <param name="context">Http上下文</param>
object CreateResult( HttpContext context );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Util.Applications;

namespace Util.Security.Authorization;

/// <summary>
/// 未授权返回结果工厂
/// </summary>
public class UnauthorizedResultFactory : IUnauthorizedResultFactory {
/// <inheritdoc />
public virtual int HttpStatusCode => 200;

/// <inheritdoc />
public virtual object CreateResult( HttpContext context ) {
return new { Code = StateCode.Unauthorized };
}
}
3 changes: 2 additions & 1 deletion src/Util.AspNetCore/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
global using Microsoft.AspNetCore.Mvc.Authorization;
global using Microsoft.AspNetCore.Mvc.Filters;
global using Microsoft.AspNetCore.DataProtection;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Builder;
global using Util.Dependency;
24 changes: 23 additions & 1 deletion src/Util.Core/Helpers/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ public CommandLine Env( IDictionary<string, string> env ) {
/// <summary>
/// 设置重定向标准输出流
/// </summary>
/// <param name="value">是否重定向标准输出流,默认值: true,注意: 如果要设置UseShellExecute为 false,则必须将RedirectStandardOutput设置为true</param>
/// <param name="value">是否重定向标准输出流,默认值: true,注意: 如果要设置UseShellExecute为 false,
/// 则必须将RedirectStandardOutput设置为true</param>
public CommandLine RedirectStandardOutput( bool value = true ) {
_redirectStandardOutput = value;
if ( value ) {
Expand Down Expand Up @@ -314,4 +315,25 @@ private void OnOutput( object sendingProcess, DataReceivedEventArgs e ) {
public string GetDebugText() {
return $"{_command} {_arguments}";
}

/// <summary>
/// 执行PowerShell命令
/// </summary>
/// <param name="command">PowerShell命令,范例: Set-ExecutionPolicy RemoteSigned</param>
/// <param name="workingDirectory">工作目录</param>
public static void ExecutePowerShell( string command, string workingDirectory = null ) {
using Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WorkingDirectory = workingDirectory;
process.Start();
process.StandardInput.WriteLine( $"powershell {command}" );
process.StandardInput.WriteLine( "exit" );
process.StandardInput.AutoFlush = true;
process.WaitForExit();
}
}
63 changes: 63 additions & 0 deletions src/Util.Core/Helpers/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ public static string ReadToString( string filePath, Encoding encoding ) {
return reader.ReadToEnd();
}

/// <summary>
/// 读取流转换成字符串
/// </summary>
/// <param name="stream">流</param>
/// <param name="encoding">字符编码</param>
/// <param name="bufferSize">缓冲区大小</param>
/// <param name="isCloseStream">读取完成是否释放流,默认为true</param>
public static string ReadToString( Stream stream, Encoding encoding = null, int bufferSize = 1024 * 2, bool isCloseStream = true ) {
if ( stream == null )
return string.Empty;
encoding ??= Encoding.UTF8;
if ( stream.CanRead == false )
return string.Empty;
using var reader = new StreamReader( stream, encoding, true, bufferSize, !isCloseStream );
if ( stream.CanSeek )
stream.Seek( 0, SeekOrigin.Begin );
var result = reader.ReadToEnd();
if ( stream.CanSeek )
stream.Seek( 0, SeekOrigin.Begin );
return result;
}

#endregion

#region ReadToStringAsync
Expand Down Expand Up @@ -194,6 +216,47 @@ public static byte[] ReadToBytes( string filePath ) {
return reader.ReadBytes( (int)fileInfo.Length );
}

/// <summary>
/// 读取流转换成字节数组
/// </summary>
/// <param name="stream">流</param>
public static byte[] ReadToBytes( Stream stream ) {
if ( stream == null )
return null;
if ( stream.CanRead == false )
return null;
if ( stream.CanSeek )
stream.Seek( 0, SeekOrigin.Begin );
var buffer = new byte[stream.Length];
stream.Read( buffer, 0, buffer.Length );
if ( stream.CanSeek )
stream.Seek( 0, SeekOrigin.Begin );
return buffer;
}

#endregion

#region ReadToBytesAsync

/// <summary>
/// 读取流转换成字节数组
/// </summary>
/// <param name="stream">流</param>
/// <param name="cancellationToken">取消令牌</param>
public static async Task<byte[]> ReadToBytesAsync( Stream stream, CancellationToken cancellationToken = default ) {
if ( stream == null )
return null;
if ( stream.CanRead == false )
return null;
if ( stream.CanSeek )
stream.Seek( 0, SeekOrigin.Begin );
var buffer = new byte[stream.Length];
await stream.ReadAsync( buffer, 0, buffer.Length, cancellationToken );
if ( stream.CanSeek )
stream.Seek( 0, SeekOrigin.Begin );
return buffer;
}

#endregion

#region Write
Expand Down
39 changes: 38 additions & 1 deletion src/Util.Core/Helpers/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public static T ToObject<T>( string json, JsonSerializerOptions options = null )
/// 将Json字符串转换为对象
/// </summary>
/// <param name="json">Json字符串</param>
/// <param name="returnType">返回类型</param>
/// <param name="options">序列化配置</param>
/// <param name="returnType">序列化配置</param>
public static object ToObject( string json, Type returnType, JsonSerializerOptions options = null ) {
if ( string.IsNullOrWhiteSpace( json ) )
return default;
Expand All @@ -161,6 +161,30 @@ private static JsonSerializerOptions GetToObjectOptions( JsonSerializerOptions o
};
}

/// <summary>
/// 将Json字节数组转换为对象
/// </summary>
/// <param name="json">Json字节数组</param>
/// <param name="options">序列化配置</param>
public static T ToObject<T>( byte[] json, JsonSerializerOptions options = null ) {
if ( json == null )
return default;
options = GetToObjectOptions( options );
return JsonSerializer.Deserialize<T>( json, options );
}

/// <summary>
/// 将Json字节流转换为对象
/// </summary>
/// <param name="json">Json字节流</param>
/// <param name="options">序列化配置</param>
public static T ToObject<T>( Stream json, JsonSerializerOptions options = null ) {
if ( json == null )
return default;
options = GetToObjectOptions( options );
return JsonSerializer.Deserialize<T>( json, options );
}

/// <summary>
/// 将Json字符串转换为对象
/// </summary>
Expand Down Expand Up @@ -190,6 +214,19 @@ private static JsonSerializerOptions GetToObjectOptions( JsonSerializerOptions o
return await JsonSerializer.DeserializeAsync<T>( json, options, cancellationToken );
}

/// <summary>
/// 将Json流转换为对象
/// </summary>
/// <param name="json">Json流</param>
/// <param name="options">序列化配置</param>
/// <param name="cancellationToken">取消令牌</param>
public static async Task<T> ToObjectAsync<T>( byte[] json, JsonSerializerOptions options = null, CancellationToken cancellationToken = default ) {
if ( json == null )
return default;
await using var stream = new MemoryStream( json );
return await ToObjectAsync<T>( stream, options, cancellationToken );
}

/// <summary>
/// 将对象转换为字节数组
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Util;
/// <summary>
/// 主机生成器服务扩展
/// </summary>
public static class HostBuilderExtensions {
public static class IHostBuilderExtensions {
/// <summary>
/// 注册Util服务
/// </summary>
Expand Down
Loading

0 comments on commit 766b0b4

Please sign in to comment.