/// <summary> /// 处理请求 /// </summary> /// <param name="context">请求上下文</param> /// <param name="entity">Soa服务信息实体</param> /// <returns>Soa响应</returns> private async Task <SoaResponseType> RequestHandler(HttpContext context, SoaServiceEntity entity) { return(await Task.Run(() => { var request = BuildRequest(context.Request.Body, entity.RequestType); // 通过DI获取 IService service = (IService)context.RequestServices.GetService(entity.ServiceType); logger.Debug($"Create instance : {entity.ServiceType}, description : {service.Description}"); var tags = (Dictionary <string, string>)entity.ServiceType.GetMethod("LogTag").Invoke(service, new object[] { request }); LogProvider.GetInstance().Add(tags); logger.Info("Verify request"); entity.ServiceType.GetMethod("Verify").Invoke(service, new object[] { request }); logger.Info("Processing"); var resp = entity.ServiceType.GetMethod("Process").Invoke(service, new object[] { request }); dynamic task = resp; SoaResponseType response = task.Result; if (response == null) { response = GetDefaultResponseEntity(); } return response; })); }
/// <summary> /// 将Soa服务保存到Soa服务列表中 /// </summary> /// <param name="entity"></param> /// <param name="services"></param> private void SaveSoaService(SoaServiceEntity entity, IServiceCollection services) { if (serviceList.ContainsKey(entity.Service)) { if (serviceList[entity.Service].ContainsKey(entity.Operation)) { logger.Warn($"Soa service found : {entity.Service}.{entity.Operation}, " + $"fullname : {entity.ServiceType.FullName}, " + $"but it was registered already. current service will not effected"); return; } serviceList[entity.Service].Add(entity.Operation, entity); } else { serviceList.Add(entity.Service, new Dictionary <string, SoaServiceEntity> { { entity.Operation, entity } }); } logger.Info($"Soa service found : {entity.Service}.{entity.Operation}, fullname : " + $"{entity.ServiceType.FullName}, description : {entity.Description}"); // 增加到Ioc容器中 services.AddSingleton(entity.ServiceType); }