public async Task TestGetClient() { var config = new ChannelConfig { ReceiveTimeout = TimeSpan.FromSeconds(20), SendTimeout = TimeSpan.FromSeconds(20) }; var host = new ServiceHost <TestService>(9091); host.AddContract <ITestService>(config); host.ServiceInstantiated += s => { //construct the created instance }; await host.Open(); var services = new ServiceCollection(); // services.AddScoped<ITestService, TestService>(); services.AddShriek().AddTcpServiceProxy(option => { option.AddTcpProxy <ITestService>("localhost", 9091, config); }); var provider = services.BuildAspectCoreServiceProvider(); var client = provider.GetService <ITestService>(); Assert.IsNotNull(client); var result = await client.GetName("elderjaems"); Assert.AreEqual("elderjaems", result); }
static async Task RunExample() { var config = new ChannelConfig { ReceiveTimeout = TimeSpan.FromSeconds(10), SendTimeout = TimeSpan.FromSeconds(10) }; var host = new ServiceHost <Service>(9091); host.AddContract <IService>(config); host.ServiceInstanciated += s => { //construct the created instance }; await host.Open(); var client = await ChannelFactory <IService> .CreateProxy("localhost", 9091, config, true); using ((IClientChannel)client) { var result = await client.Echo("message"); Console.WriteLine(result); } //client = await ChannelFactory<IService>.CreateProxy("localhost", 9091, config); //var channel = (IClientChannel)client; //await channel.Open(); //await channel.Close(); Console.ReadLine(); }
static async Task RunExample() { var config = new ChannelConfig { ReceiveTimeout = TimeSpan.FromSeconds(20), SendTimeout = TimeSpan.FromSeconds(20) }; var host = new ServiceHost <Service>(9091); host.AddContract <IService>(config); host.ServiceInstantiated += s => { //construct the created instance }; await host.Open(); var client = await ChannelFactory <IService> .CreateProxy("localhost", 9091, config, true); using ((IClientChannel)client) { var str = await client.Echo("message"); Console.WriteLine(str); var msg = new Msg { Id = 1, Body = "From Client" }; var result = await client.EchoMsg(msg); Console.WriteLine(result.Body); var msg1 = new Msg { Body = "M1" }; var msg2 = new Msg { Body = "M2" }; var multiParams = await client.EchoMany(msg1, msg2, "hello world", -123); Console.WriteLine(multiParams.Body); var noParam = await client.EchoNoParam(); Console.WriteLine(noParam); //try //{ // var err = await client.EchoServerError(); //} //catch (Exception ex) //{ // Console.WriteLine(ex.Message); //} } //client = await ChannelFactory<IService>.CreateProxy("localhost", 9091, config); //var channel = (IClientChannel)client; //await channel.Open(); //await channel.Close(); Console.ReadLine(); }
public static void Main(string[] args) { var config = new ChannelConfig { ReceiveTimeout = TimeSpan.FromSeconds(60), SendTimeout = TimeSpan.FromSeconds(60) }; var host = new ServiceHost <TcpTestService>(9091); host.AddContract <ITcpTestService>(config); host.ServiceInstantiated += s => { //construct the created instance }; host.Open().Wait(); new WebHostBuilder() .UseKestrel() .UseUrls("http://*:8080", "http://*:8081") .ConfigureServices(services => { services.AddMvcCore() .AddJsonFormatters() .AddWebApiProxy(); //服务里注册代理客户端 services.AddShriek() .AddWebApiProxy(opt => { opt.AddWebApiProxy <SampleApiProxy>("http://localhost:8081"); opt.AddWebApiProxy <Samples.Services.SampleApiProxy>("http://localhost:8080"); }).AddTcpServiceProxy(opt => { opt.AddTcpProxy <ITcpTestService>("localhost", 9091, config); }); }) .Configure(app => app.UseMvc()) .Build() .Start(); var provider = new ServiceCollection() .AddShriek() .AddWebApiProxy(opt => { opt.AddWebApiProxy <SampleApiProxy>("http://localhost:8081"); opt.AddWebApiProxy <Samples.Services.SampleApiProxy>("http://localhost:8080"); }) .AddTcpServiceProxy(opt => { opt.AddTcpProxy <ITcpTestService>("localhost", 9091, config); }) .Services .BuildAspectCoreServiceProvider(); var todoService = provider.GetService <ITodoService>(); var testService = provider.GetService <ITestService>(); var sampleTestService = provider.GetService <Samples.Services.ITestService>(); var tcpService = provider.GetService <ITcpTestService>(); Console.ReadKey(); var result = todoService.Get(1).Result; Console.WriteLine(JsonConvert.SerializeObject(result)); result = todoService.Get(2).Result; Console.WriteLine(JsonConvert.SerializeObject(result)); //这个调用服务,服务内注入了一个代理客户端调用另一个服务 var result2 = testService.Test(11); Console.WriteLine(JsonConvert.SerializeObject(result2)); var result3 = sampleTestService.Test("elderjames"); Console.WriteLine(JsonConvert.SerializeObject(result3)); Console.WriteLine("press any key to tcp testing..."); Console.ReadKey(); var result4 = tcpService.Test("hahaha").Result; Console.WriteLine(JsonConvert.SerializeObject(result4)); Console.ReadKey(); }