mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-15 05:56:37 +08:00
前端大更新vuex
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Com.Ctrip.Framework.Apollo;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Com.Ctrip.Framework.Apollo.Enums;
|
||||
using Com.Ctrip.Framework.Apollo.Logging;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Yi.Framework.WebCore.BuilderExtend
|
||||
{
|
||||
public static class ApolloExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 接入Apollo
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="jsonPath">apollo配置文件路径 如果写入appsettings.json中 则jsonPath传null即可</param>
|
||||
public static void AddApolloService(this IConfigurationBuilder builder, params string[] NameSpace)
|
||||
{
|
||||
//阿波罗的日志级别调整
|
||||
LogManager.UseConsoleLogging(LogLevel.Warn);
|
||||
var root = builder.Build();
|
||||
var apolloBuilder = builder.AddApollo(root.GetSection("apollo")).AddDefault();
|
||||
|
||||
|
||||
|
||||
foreach (var item in NameSpace)
|
||||
{
|
||||
apolloBuilder.AddNamespace(item, ConfigFileFormat.Json);
|
||||
}
|
||||
//监听apollo配置
|
||||
Monitor(builder.Build());
|
||||
|
||||
|
||||
}
|
||||
#region private
|
||||
/// <summary>
|
||||
/// 监听配置
|
||||
/// </summary>
|
||||
private static void Monitor(IConfigurationRoot root)
|
||||
{
|
||||
//TODO 需要根据改变执行特定的操作 如 mq redis 等其他跟配置相关的中间件
|
||||
//TODO 初步思路:将需要执行特定的操作key和value放入内存字典中,在赋值操作时通过标准事件来执行特定的操作。
|
||||
|
||||
//要重新Build 此时才将Apollo provider加入到ConfigurationBuilder中
|
||||
ChangeToken.OnChange(() => root.GetReloadToken(), () =>
|
||||
{
|
||||
foreach (var apolloProvider in root.Providers.Where(p => p is ApolloConfigurationProvider))
|
||||
{
|
||||
var property = apolloProvider.GetType().BaseType.GetProperty("Data", BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
var data = property.GetValue(apolloProvider) as IDictionary<string, string>;
|
||||
foreach (var item in data)
|
||||
{
|
||||
Console.WriteLine($"key {item.Key} value {item.Value}");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -43,12 +43,10 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
|
||||
switch (statusCode)
|
||||
{
|
||||
|
||||
case 401: msg = "未授权";break;
|
||||
case 403: msg = "未授权"; break;
|
||||
case 404: msg = "未找到服务"; break;
|
||||
case 502: msg = "请求错误"; break;
|
||||
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(msg))
|
||||
{
|
||||
@@ -59,7 +57,16 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
//异常错误信息捕获,将错误信息用Json方式返回
|
||||
private static Task HandleExceptionAsync(HttpContext context, int statusCode, string msg)
|
||||
{
|
||||
var result = JsonConvert.SerializeObject( Result.Error(msg).SetCode(statusCode));
|
||||
Result resp;
|
||||
if (statusCode == 401)
|
||||
{
|
||||
resp = Result.UnAuthorize(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
resp = Result.Error(msg);
|
||||
}
|
||||
var result = JsonConvert.SerializeObject(resp);
|
||||
context.Response.ContentType = "application/json;charset=utf-8";
|
||||
return context.Response.WriteAsync(result);
|
||||
}
|
||||
@@ -67,7 +74,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
//扩展方法
|
||||
public static class ErrorHandlingExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseErrorHandling(this IApplicationBuilder builder)
|
||||
public static IApplicationBuilder UseErrorHandlingService(this IApplicationBuilder builder)
|
||||
{
|
||||
return builder.UseMiddleware<ErrorHandExtension>();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using Castle.DynamicProxy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.WebCore.Utility
|
||||
{
|
||||
public class CustomAutofacAop : IInterceptor
|
||||
{
|
||||
public void Intercept(IInvocation invocation)
|
||||
{
|
||||
Console.WriteLine($"invocation.Methond={invocation.Method}");
|
||||
Console.WriteLine($"invocation.Arguments={string.Join(",", invocation.Arguments)}");
|
||||
|
||||
invocation.Proceed(); //继续执行
|
||||
|
||||
Console.WriteLine($"方法{invocation.Method}执行完成了");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Autofac;
|
||||
using Autofac.Extras.DynamicProxy;
|
||||
using Castle.DynamicProxy;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.WebCore.Utility;
|
||||
using Module = Autofac.Module;
|
||||
|
||||
namespace Yi.Framework.WebCore.Utility
|
||||
{
|
||||
public class CustomAutofacModule : Module
|
||||
{
|
||||
protected override void Load(ContainerBuilder containerBuilder)
|
||||
{
|
||||
var basePath = AppContext.BaseDirectory;
|
||||
var servicesDllFile = Path.Combine(basePath, "Yi.Framework.Service.dll");
|
||||
if (!(File.Exists(servicesDllFile)))
|
||||
{
|
||||
var msg = "service.dll 丢失,请编译后重新生成。";
|
||||
throw new Exception(msg);
|
||||
}
|
||||
var assemblysServices = Assembly.LoadFrom(servicesDllFile);
|
||||
containerBuilder.RegisterAssemblyTypes(assemblysServices)
|
||||
.AsImplementedInterfaces()
|
||||
.InstancePerDependency()
|
||||
.EnableInterfaceInterceptors();
|
||||
|
||||
|
||||
containerBuilder.Register(c => new CustomAutofacAop());//AOP注册
|
||||
//containerBuilder.RegisterType<A>().As<IA>().EnableInterfaceInterceptors();开启Aop
|
||||
|
||||
//将数据库对象注入
|
||||
//containerBuilder.RegisterType<DataContext>().As<DbContext>().InstancePerLifetimeScope().EnableInterfaceInterceptors();
|
||||
|
||||
//containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerDependency().EnableInterfaceInterceptors();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface IAutofacTest
|
||||
{
|
||||
void Show(int id, string name);
|
||||
}
|
||||
|
||||
[Intercept(typeof(CustomAutofacAop))]
|
||||
public class AutofacTest : IAutofacTest
|
||||
{
|
||||
public void Show(int id, string name)
|
||||
{
|
||||
Console.WriteLine($"This is {id} _ {name}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[assembly: HostingStartup(typeof(Yi.Framework.WebCore.Utility.CustomHostingStartup))]
|
||||
namespace Yi.Framework.WebCore.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 必须实现IHostingStartup接口
|
||||
/// 必须标记HostingStartup特性
|
||||
///
|
||||
/// 就像木马一样
|
||||
/// </summary>
|
||||
public class CustomHostingStartup : IHostingStartup
|
||||
{
|
||||
public void Configure(IWebHostBuilder builder)
|
||||
{
|
||||
Console.WriteLine("This is CustomHostingStartup Invoke");
|
||||
|
||||
//有IWebHostBuilder,一切都可以做。。
|
||||
#region MyRegion
|
||||
//builder.ConfigureAppConfiguration(configurationBuilder =>
|
||||
//{
|
||||
// configurationBuilder.AddXmlFile("appsettings1.xml", optional: false, reloadOnChange: true);
|
||||
//});//添加配置
|
||||
|
||||
//builder.ConfigureServices(services =>
|
||||
//{
|
||||
// services.AddTransient<ITestServiceA, TestServiceA>();
|
||||
//});//IOC注册
|
||||
|
||||
//builder.Configure(app =>
|
||||
//{
|
||||
// app.Use(next =>
|
||||
// {
|
||||
// Console.WriteLine("This is CustomHostingStartup-Middleware Init");
|
||||
// return new RequestDelegate(
|
||||
// async context =>
|
||||
// {
|
||||
// Console.WriteLine("This is CustomHostingStartup-Middleware start");
|
||||
// await next.Invoke(context);
|
||||
// Console.WriteLine("This is CustomHostingStartup-Middleware end");
|
||||
// });
|
||||
// });
|
||||
//});//甚至来个中间件
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,16 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac" Version="6.3.0" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
|
||||
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="6.0.0" />
|
||||
<PackageReference Include="Com.Ctrip.Framework.Apollo.Configuration" Version="2.4.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.11" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="5.0.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user