public async Task Start() { //检查grpc服务过滤器查找服务是否已经有值,如果没有,使用工厂创建 if (_grpcServiceFindFilterService == null) { _grpcServiceFindFilterService = _grpcServiceFindFilterServiceFactory.Create(); } //检查grpc服务查找服务是否已经有值,如果没有,使用工厂创建 if (_grpcServiceFindService == null) { _grpcServiceFindService = _grpcServiceFindServiceFactory.Create(); } //获取Grpc服务信息 var grpcServiceInfos = await _grpcServiceFindService.Execute(); List <GrpcServiceDescription> grpcServiceDescriptionList = new List <GrpcServiceDescription>(); GrpcServiceDescription newGrpcServiceDescription; //组装Grpc服务描述,为GrpcHostConfiguartion赋值 //以及为Gprc服务的服务定义赋值 foreach (var infoItem in grpcServiceInfos) { newGrpcServiceDescription = new GrpcServiceDescription() { NamespaceType = infoItem.NameSpaceType, ServiceType = infoItem.ServiceType }; grpcServiceDescriptionList.Add(newGrpcServiceDescription); //获取每个Grpc服务的过滤器 var serviceFilters = await _grpcServiceFindFilterService.Execute(newGrpcServiceDescription.ServiceType); newGrpcServiceDescription.Filters = serviceFilters; //获取合并后的链式拦截器 var chainInterceptor = await getChainInterceptor(_configuration.GlobalFilters, serviceFilters); //组装Grpc服务器中的服务定义 var gprcService = DIContainerContainer.Get(newGrpcServiceDescription.ServiceType); ServerServiceDefinition newServerServiceDefinition = ((ServerServiceDefinition)newGrpcServiceDescription.NamespaceType.GetMethod("BindService").Invoke(null, new object[] { gprcService })).Intercept(chainInterceptor); _grpcServer.Services.Add(newServerServiceDefinition); } //为Gprc服务的端口定义赋值 foreach (var portItem in _configuration.Ports) { _grpcServer.Ports.Add(new ServerPort(portItem.Host, portItem.Port, portItem.Credential)); } _grpcServer.Start(); }
public GrpcHost GetGrpcServiceFindService(Func <IGrpcServiceFindService> serviceGenerator) { _grpcServiceFindService = serviceGenerator(); return(this); }