Skip to content

Commit

Permalink
Merge pull request #938 from colinin/fix-ServiceInvocationJob
Browse files Browse the repository at this point in the history
fix(jobs): fix ServiceInvocationJob
  • Loading branch information
colinin authored Apr 23, 2024
2 parents 7eebffd + de14c9e commit c61ddb3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 47 deletions.
23 changes: 23 additions & 0 deletions aspnet-core/LINGYUN.MicroService.TaskManagement.sln
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.AspNetCore.Http
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.Security", "framework\security\LINGYUN.Abp.Security\LINGYUN.Abp.Security.csproj", "{BEE1B759-5B65-481B-928B-8BC6A89A7C13}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "oss-management", "oss-management", "{F36E71F7-B05F-4513-A923-81E2A7474EAE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.OssManagement.Domain.Shared", "modules\oss-management\LINGYUN.Abp.OssManagement.Domain.Shared\LINGYUN.Abp.OssManagement.Domain.Shared.csproj", "{94D1C512-09E4-4A68-8989-6DB49FEABA67}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.OssManagement.Application.Contracts", "modules\oss-management\LINGYUN.Abp.OssManagement.Application.Contracts\LINGYUN.Abp.OssManagement.Application.Contracts.csproj", "{96B4D255-BB21-4BAF-902A-ADE3F25A44C2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.OssManagement.HttpApi.Client", "modules\oss-management\LINGYUN.Abp.OssManagement.HttpApi.Client\LINGYUN.Abp.OssManagement.HttpApi.Client.csproj", "{44778F3C-7610-4F1C-A2B6-DF8925B1CBE0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -354,6 +362,18 @@ Global
{BEE1B759-5B65-481B-928B-8BC6A89A7C13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEE1B759-5B65-481B-928B-8BC6A89A7C13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEE1B759-5B65-481B-928B-8BC6A89A7C13}.Release|Any CPU.Build.0 = Release|Any CPU
{94D1C512-09E4-4A68-8989-6DB49FEABA67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{94D1C512-09E4-4A68-8989-6DB49FEABA67}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94D1C512-09E4-4A68-8989-6DB49FEABA67}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94D1C512-09E4-4A68-8989-6DB49FEABA67}.Release|Any CPU.Build.0 = Release|Any CPU
{96B4D255-BB21-4BAF-902A-ADE3F25A44C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96B4D255-BB21-4BAF-902A-ADE3F25A44C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96B4D255-BB21-4BAF-902A-ADE3F25A44C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96B4D255-BB21-4BAF-902A-ADE3F25A44C2}.Release|Any CPU.Build.0 = Release|Any CPU
{44778F3C-7610-4F1C-A2B6-DF8925B1CBE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44778F3C-7610-4F1C-A2B6-DF8925B1CBE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44778F3C-7610-4F1C-A2B6-DF8925B1CBE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44778F3C-7610-4F1C-A2B6-DF8925B1CBE0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -412,6 +432,9 @@ Global
{B3E81983-629A-4380-B4C2-50E247916F5E} = {5A41C31A-B966-418B-B446-5BA1D7E61A62}
{088064DD-D141-4E9A-B185-E332920A323A} = {5A41C31A-B966-418B-B446-5BA1D7E61A62}
{BEE1B759-5B65-481B-928B-8BC6A89A7C13} = {5A41C31A-B966-418B-B446-5BA1D7E61A62}
{94D1C512-09E4-4A68-8989-6DB49FEABA67} = {F36E71F7-B05F-4513-A923-81E2A7474EAE}
{96B4D255-BB21-4BAF-902A-ADE3F25A44C2} = {F36E71F7-B05F-4513-A923-81E2A7474EAE}
{44778F3C-7610-4F1C-A2B6-DF8925B1CBE0} = {F36E71F7-B05F-4513-A923-81E2A7474EAE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E1FD1F4C-D344-408B-97CF-B6F1F6D7D293}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using LINGYUN.Abp.Dapr.Client.DynamicProxying;
using Castle.DynamicProxy.Internal;
using LINGYUN.Abp.Dapr.Client.DynamicProxying;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Http.Client;
Expand Down Expand Up @@ -61,7 +64,18 @@ public async virtual Task ExecuteAsync(JobRunnableContext context)
var type = context.GetString(PropertyService);
var method = context.GetString(PropertyMethod);
var serviceType = Type.GetType(type, true);
var serviceMethod = serviceType.GetMethod(method);
var serviceMethod = serviceType.GetMethod(method, BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
if (serviceMethod == null)
{
foreach (var ifce in serviceType.GetAllInterfaces())
{
serviceMethod = ifce.GetMethod(method, BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
if (serviceMethod != null)
{
break;
}
}
}
context.TryGetString(PropertyCulture, out var culture);
context.TryGetString(PropertyProvider, out var provider);
provider ??= "http";
Expand All @@ -83,67 +97,51 @@ public async virtual Task ExecuteAsync(JobRunnableContext context)
switch (provider)
{
case "http":
await ExecuteWithHttpProxy(context, serviceType, serviceMethod);
break;
case "dapr":
await ExecuteWithDaprProxy(context, serviceType, serviceMethod);
// 接口代理无差异
await ExecuteProxy(context, serviceType, serviceMethod);
break;
}
}
}
}

#region HttpClient

protected readonly static string CallRequestMethod = nameof(DynamicHttpProxyInterceptorClientProxy<object>.CallRequestAsync);
protected readonly static MethodInfo ClientProxyMethod = typeof(DynamicHttpProxyInterceptorClientProxy<>)
.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.First(m => m.Name == CallRequestMethod && !m.IsGenericMethodDefinition);
protected readonly static MethodInfo CallRequestAsyncMethod = typeof(DynamicHttpProxyInterceptor<object>)
.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.First(m => m.Name == CallRequestMethod && m.IsGenericMethodDefinition);

protected async virtual Task ExecuteWithHttpProxy(
protected async virtual Task ExecuteProxy(
JobRunnableContext context,
Type serviceType,
MethodInfo serviceMethod)
{
// 反射所必须的参数
var clientProxyType = typeof(DynamicHttpProxyInterceptorClientProxy<>).MakeGenericType(serviceType);
var clientProxy = context.GetRequiredService(clientProxyType);

// 调用远程服务发现端点
var actionApiDescription = await GetActionApiDescriptionModel(context, serviceType, serviceMethod);
var clientProxy = context.GetRequiredService(serviceType);

// 调用参数
var invokeParameters = new Dictionary<string, object>();
var methodParamters = serviceMethod.GetParameters();
var invokeParameters = new List<object>();
if (context.TryGetString(PropertyData, out var data))
{
var jsonSerializer = context.GetRequiredService<IJsonSerializer>();
invokeParameters = jsonSerializer.Deserialize<Dictionary<string, object>>(data);
var json = JsonNode.Parse(data);
foreach ( var param in methodParamters)
{
var input = json[param.Name];
if (input != null)
{
invokeParameters.Add(input.Deserialize(param.ParameterType));
}
}
}

// 构造服务代理上下文
var clientProxyRequestContext = new ClientProxyRequestContext(
actionApiDescription,
invokeParameters,
serviceType);

if (serviceMethod.ReturnType.GenericTypeArguments.IsNullOrEmpty())
{
// 直接调用
var taskProxy = (Task)ClientProxyMethod
.Invoke(clientProxy, new object[] { clientProxyRequestContext });
var taskProxy = (Task)serviceMethod.Invoke(clientProxy, invokeParameters.ToArray());
await taskProxy;
}
else
{
// 有返回值的调用
var returnType = serviceMethod.ReturnType.GenericTypeArguments[0];
var result = (Task)CallRequestAsyncMethod
.MakeGenericMethod(returnType)
.Invoke(this, new object[] { context });

var callResult = serviceMethod.Invoke(clientProxy, invokeParameters.ToArray());
var result = (Task)callResult;
context.SetResult(await GetResultAsync(result, returnType));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

<ItemGroup>
<ProjectReference Include="..\..\migrations\LY.MicroService.TaskManagement.EntityFrameworkCore\LY.MicroService.TaskManagement.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\modules\oss-management\LINGYUN.Abp.OssManagement.HttpApi.Client\LINGYUN.Abp.OssManagement.HttpApi.Client.csproj" />
<ProjectReference Include="..\..\modules\task-management\LINGYUN.Abp.BackgroundTasks.DistributedLocking\LINGYUN.Abp.BackgroundTasks.DistributedLocking.csproj" />
<ProjectReference Include="..\..\modules\task-management\LINGYUN.Abp.BackgroundTasks.ExceptionHandling\LINGYUN.Abp.BackgroundTasks.ExceptionHandling.csproj" />
<ProjectReference Include="..\..\modules\task-management\LINGYUN.Abp.BackgroundTasks.Jobs\LINGYUN.Abp.BackgroundTasks.Jobs.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using LINGYUN.Abp.Http.Client.Wrapper;
using LINGYUN.Abp.Localization.CultureMap;
using LINGYUN.Abp.LocalizationManagement.EntityFrameworkCore;
using LINGYUN.Abp.OssManagement;
using LINGYUN.Abp.Saas.EntityFrameworkCore;
using LINGYUN.Abp.Serilog.Enrichers.Application;
using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
Expand Down Expand Up @@ -52,6 +53,7 @@ namespace LY.MicroService.TaskManagement;
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
typeof(AbpAuthorizationOrganizationUnitsModule),
typeof(AbpEmailingExceptionHandlingModule),
typeof(AbpOssManagementHttpApiClientModule),
typeof(AbpHttpClientIdentityModelWebModule),
typeof(AbpAspNetCoreMultiTenancyModule),
typeof(AbpAspNetCoreMvcLocalizationModule),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
},
"MySql": {
"TableNamePrefix": "tsk",
"ConnectionString": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456"
"ConnectionString": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456;SslMode=None"
},
"RabbitMQ": {
"HostName": "127.0.0.1",
"Port": 5672,
"UserName": "admin",
"Password": "admin",
"Password": "123456",
"ExchangeName": "LINGYUN.Abp.Application",
"VirtualHost": "/"
}
Expand All @@ -50,21 +50,21 @@
"quartz.jobStore.dataSource": "tkm",
"quartz.jobStore.type": "Quartz.Impl.AdoJobStore.JobStoreTX,Quartz",
"quartz.jobStore.driverDelegateType": "Quartz.Impl.AdoJobStore.MySQLDelegate,Quartz",
"quartz.dataSource.tkm.connectionString": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456",
"quartz.dataSource.tkm.connectionString": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456;SslMode=None",
"quartz.dataSource.tkm.connectionStringName": "TaskManagement",
"quartz.dataSource.tkm.provider": "MySqlConnector",
"quartz.jobStore.clustered": "true",
"quartz.serializer.type": "json"
}
},
"ConnectionStrings": {
"Default": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456",
"TaskManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456",
"AbpFeatureManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456",
"AbpPermissionManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456",
"AbpLocalizationManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456",
"AbpSettingManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456",
"AbpSaas": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456"
"Default": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456;SslMode=None",
"TaskManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456;SslMode=None",
"AbpFeatureManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456;SslMode=None",
"AbpPermissionManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456;SslMode=None",
"AbpLocalizationManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456;SslMode=None",
"AbpSettingManagement": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456;SslMode=None",
"AbpSaas": "Server=127.0.0.1;Database=Platform-v70;User Id=root;Password=123456;SslMode=None"
},
"RemoteServices": {
"AbpOssManagement": {
Expand All @@ -80,7 +80,7 @@
"GrantType": "client_credentials",
"Scope": "lingyun-abp-application",
"ClientId": "InternalServiceClient",
"ClientSecret": "1q2w3E*"
"ClientSecret": "1q2w3e*"
}
},
"DistributedLock": {
Expand Down

0 comments on commit c61ddb3

Please sign in to comment.