private static void Main(string[] args) { Thread.Sleep(10000); var containerBuilder = new ContainerBuilder(); var builder = new ServiceHostClientBuilder(containerBuilder).UseRpcForTransfer(); using (var host = builder.Build()) { ITransportClient client = host.Container.Resolve <ITransportClientFactory>() .CreateClient(new ServerAddress("127.0.0.1", 8008)); //ITransportClient client = container.Resolve<ITransportClientFactory>() // .DecorationFactory(container.Resolve<ISerializer>()) // .CreateClient(new ServerAddress("127.0.0.1", 8007)); RemoteCallData sendMessage = new RemoteCallData { ServiceId = "base/fastdfs", Parameters = new Dictionary <string, object> { { "message", 12 }, { "myout", 122 } } }; RemoteCallBackData result = client.SendAsync(sendMessage).Result; Console.Write(result.Result.ToString()); Console.ReadLine(); } }
protected virtual async Task TerminateStrategyAsync(TerminationMessage.TerminationMessageCode terminationMessageCode) { var terminationMessage = new TerminationMessage( this.SessionId, terminationMessageCode, _vaspInfo); await _transportClient.SendAsync(new MessageEnvelope() { Topic = this.CounterPartyTopic, SigningKey = _privateSigningKey, EncryptionType = EncryptionType.Symmetric, EncryptionKey = _sharedSymKeyId }, terminationMessage); }
/// <summary> /// 服务内部调用方法 /// </summary> /// <param name="serverAddress">服务地址</param> /// <param name="sendMessage">请求内容</param> public static async Task <T> InternalCall <T>(List <ServerAddress> service, RemoteCallData sendMessage) { ContainerBuilder containerBuilder = new ContainerBuilder(); Core.Client.IServiceHostClientBuilder builder = new ServiceHostClientBuilder(containerBuilder).UseRpcForTransfer() .UsePollingAddressSelector(BalanceType.RoundRobin); using (Core.IServiceHost host = builder.Build()) { IAddressSelector addressSelector = host.Container.Resolve <IAddressSelector>(); ServerAddress desc = await addressSelector.GetAddressAsync(service); ITransportClient client = host.Container.Resolve <ITransportClientFactory>() .CreateClient(desc); RemoteCallBackData result = client.SendAsync(sendMessage).Result; return(JsonConvert.DeserializeObject <T>(result.Result.ToString())); } }
public async Task <RemoteCallBackData> InvokeAsync(List <ServerAddress> service, string serviceIdOrPath, IDictionary <string, object> paras, string token) { ServerAddress desc = await _addressSelector.GetAddressAsync(service); if (paras == null) { paras = new ConcurrentDictionary <string, object>(); } if (_retryTimes < 0) { _retryTimes = service.Count; } RemoteCallBackData result = null; Polly.Retry.RetryPolicy retryPolicy = Policy.Handle <Exception>() .RetryAsync(_retryTimes, async(ex, count) => { desc = await _addressSelector.GetAddressAsync(service); _logger.Debug( $"FaultHandling,retry times: {count},serviceId: {serviceIdOrPath},Address: {desc.Code},RemoteServiceCaller excute retry by Polly for exception {ex.Message}"); }); Polly.Wrap.PolicyWrap <RemoteCallBackData> fallbackPolicy = Policy <RemoteCallBackData> .Handle <Exception>() .FallbackAsync(new RemoteCallBackData() { ErrorCode = "500", ErrorMsg = "error occur when communicate with server. server maybe have been down." }) .WrapAsync(retryPolicy); return(await fallbackPolicy.ExecuteAsync(async() => { ITransportClient client = _transportClientFactory.CreateClient(desc); if (client == null) { return new RemoteCallBackData { ErrorCode = "400", ErrorMsg = "服务不可用" }; } _logger.Debug($"invoke: serviceId:{serviceIdOrPath}, parameters count: {paras.Count()}, token:{token}"); Payload payload = new Payload(); if (!string.IsNullOrEmpty(token) && _authorizationHandler != null && desc.EnableAuthorization) { var authorizationContext = _authorizationHandler.GetAuthorizationContext(token, desc.Roles); if (authorizationContext != null) { payload.Items = authorizationContext; } else { return new RemoteCallBackData { ErrorMsg = "没有权限", ErrorCode = "401" }; } } result = await client.SendAsync(new RemoteCallData { Payload = payload, Parameters = paras, ServiceId = serviceIdOrPath, Token = token, }); return result; })); }