😪 减少不必要的”克隆“代码

remotes/github-mirror-56/3.0.0-beta.5
百小僧 4 years ago
parent 462a78e331
commit 77282daa1e

@ -148,25 +148,11 @@ namespace Furion.FriendlyException
{
if (action == null) throw new ArgumentNullException(nameof(action));
// 不断重试
while (true)
{
try
{
action(); return;
}
catch (Exception ex)
{
// 如果可重试次数小于或等于0则终止重试
if (--numRetries <= 0) throw;
// 如果填写了 exceptionTypes 且异常类型不在 exceptionTypes 之内,则终止重试
if (exceptionTypes != null && exceptionTypes.Length > 0 && !exceptionTypes.Any(u => u.IsAssignableFrom(ex.GetType()))) throw;
// 如果可重试异常数大于 0则间隔指定时间后继续执行
if (retryTimeout > 0) Thread.Sleep(retryTimeout);
}
}
_ = Retry(() =>
{
action();
return 0;
}, numRetries, retryTimeout, exceptionTypes);
}
/// <summary>

@ -25,21 +25,12 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <typeparam name="TFilter"></typeparam>
/// <param name="mvcBuilder"></param>
/// <param name="extraConfigure"></param>
/// <param name="configure"></param>
/// <returns></returns>
public static IMvcBuilder AddMvcFilter<TFilter>(this IMvcBuilder mvcBuilder, Action<MvcOptions> extraConfigure = default)
public static IMvcBuilder AddMvcFilter<TFilter>(this IMvcBuilder mvcBuilder, Action<MvcOptions> configure = default)
where TFilter : IFilterMetadata
{
// 非 Web 环境跳过注册
if (App.WebHostEnvironment == default) return mvcBuilder;
mvcBuilder.AddMvcOptions(options =>
{
options.Filters.Add<TFilter>();
// 其他额外配置
extraConfigure?.Invoke(options);
});
mvcBuilder.Services.AddMvcFilter<TFilter>(configure);
return mvcBuilder;
}
@ -49,9 +40,9 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <typeparam name="TFilter"></typeparam>
/// <param name="services"></param>
/// <param name="extraConfigure"></param>
/// <param name="configure"></param>
/// <returns></returns>
public static IServiceCollection AddMvcFilter<TFilter>(this IServiceCollection services, Action<MvcOptions> extraConfigure = default)
public static IServiceCollection AddMvcFilter<TFilter>(this IServiceCollection services, Action<MvcOptions> configure = default)
where TFilter : IFilterMetadata
{
// 非 Web 环境跳过注册
@ -62,7 +53,7 @@ namespace Microsoft.Extensions.DependencyInjection
options.Filters.Add<TFilter>();
// 其他额外配置
extraConfigure?.Invoke(options);
configure?.Invoke(options);
});
return services;

@ -148,25 +148,11 @@ namespace Furion.FriendlyException
{
if (action == null) throw new ArgumentNullException(nameof(action));
// 不断重试
while (true)
{
try
{
action(); return;
}
catch (Exception ex)
{
// 如果可重试次数小于或等于0则终止重试
if (--numRetries <= 0) throw;
// 如果填写了 exceptionTypes 且异常类型不在 exceptionTypes 之内,则终止重试
if (exceptionTypes != null && exceptionTypes.Length > 0 && !exceptionTypes.Any(u => u.IsAssignableFrom(ex.GetType()))) throw;
// 如果可重试异常数大于 0则间隔指定时间后继续执行
if (retryTimeout > 0) Thread.Sleep(retryTimeout);
}
}
_ = Retry(() =>
{
action();
return 0;
}, numRetries, retryTimeout, exceptionTypes);
}
/// <summary>

@ -25,21 +25,12 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <typeparam name="TFilter"></typeparam>
/// <param name="mvcBuilder"></param>
/// <param name="extraConfigure"></param>
/// <param name="configure"></param>
/// <returns></returns>
public static IMvcBuilder AddMvcFilter<TFilter>(this IMvcBuilder mvcBuilder, Action<MvcOptions> extraConfigure = default)
public static IMvcBuilder AddMvcFilter<TFilter>(this IMvcBuilder mvcBuilder, Action<MvcOptions> configure = default)
where TFilter : IFilterMetadata
{
// 非 Web 环境跳过注册
if (App.WebHostEnvironment == default) return mvcBuilder;
mvcBuilder.AddMvcOptions(options =>
{
options.Filters.Add<TFilter>();
// 其他额外配置
extraConfigure?.Invoke(options);
});
mvcBuilder.Services.AddMvcFilter<TFilter>(configure);
return mvcBuilder;
}
@ -49,9 +40,9 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <typeparam name="TFilter"></typeparam>
/// <param name="services"></param>
/// <param name="extraConfigure"></param>
/// <param name="configure"></param>
/// <returns></returns>
public static IServiceCollection AddMvcFilter<TFilter>(this IServiceCollection services, Action<MvcOptions> extraConfigure = default)
public static IServiceCollection AddMvcFilter<TFilter>(this IServiceCollection services, Action<MvcOptions> configure = default)
where TFilter : IFilterMetadata
{
// 非 Web 环境跳过注册
@ -62,7 +53,7 @@ namespace Microsoft.Extensions.DependencyInjection
options.Filters.Add<TFilter>();
// 其他额外配置
extraConfigure?.Invoke(options);
configure?.Invoke(options);
});
return services;

@ -296,6 +296,8 @@ throw Oops.Oh("哈哈哈哈");
throw Oops.Oh(errorCode: "x1001");
throw Oops.Oh(1000, typeof(Exception));
throw Oops.Oh(1000).StatusCode(400); // 设置错误码
throw Oops.Bah("用户名或密码错误"); // 抛出业务异常,状态码为 400
throw Oops.Bah(1000);
```
## 7.7 多个异常信息类型

@ -49,3 +49,10 @@ Oops.Retry(() => {
}, 3, 1000, typeof(ArgumentNullException));
```
## 4.6 抛出业务异常
```cs
throw Oops.Bah("用户名或密码错误");
throw Oops.Bah(1000);
```

@ -164,7 +164,7 @@ function Gitee() {
className={"furion-log-jiao" + (isDarkTheme ? " dark" : "")}
></div>
<div className="furion-log-number">
<div style={{ color: "#1fd898" }}>850,285</div>
<div style={{ color: "#1fd898" }}>857,390</div>
<span className={isDarkTheme ? " dark" : ""}>Downloads</span>
</div>
</div>

Loading…
Cancel
Save