public ServerHost(GrpcChannelType channelType = GrpcChannelType.GrpcCore) { _server = new Server { Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; Channel = GrpcChannelFactory.CreateChannel(channelType, "localhost", Port); }
static void Main(string[] args) { var configuration = new AppSettingsChannelConfiguration(); var grpcChanngelFactory = new GrpcChannelFactory(configuration); var walletClientFactory = new WalletClientFactory(grpcChanngelFactory); var wallet = new Wallet(walletClientFactory, new AllClientsDefaultCallConfiguration()); var nodes = wallet.ListNodesAsync().GetAwaiter().GetResult(); foreach (var node in nodes.Nodes) { Console.WriteLine($"Node: {node.Address}"); } }
public async Task <KestrelHost> StartAsync(HttpProtocols protocols = HttpProtocols.Http2) { GrpcChannelExtensions.Http2UnencryptedSupport = true; _host = Host .CreateDefaultBuilder() .ConfigureServices(services => { services.AddGrpc(); services.AddServiceModelGrpc(); _configureServices?.Invoke(services); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.Configure(app => { app.UseRouting(); _configureApp?.Invoke(app); if (_configureEndpoints != null) { app.UseEndpoints(_configureEndpoints); } }); webBuilder.UseKestrel(o => o.ListenLocalhost(_port, l => l.Protocols = protocols)); }) .Build(); try { await _host.StartAsync().ConfigureAwait(false); } catch { await DisposeAsync().ConfigureAwait(false); throw; } ClientFactory = new ClientFactory(_clientFactoryDefaultOptions); Channel = GrpcChannelFactory.CreateChannel(_channelType, "localhost", DefaultPort); return(this); }
static async Task Main(string[] args) { Console.WriteLine("Hello World!"); // The below USING disposes of the channel when execution goes outside the current set of curly braces. using GrpcChannel serviceAChannel = GrpcChannelFactory.MakeGrpcChannel(GrpcSettings.ServiceAClientHttpAddressString); // Make the protobuf client side proxy to allow calling protobuf rpc methods on ServiceADemo. SvcADemo.SvcADemoClient serviceADemoProxy = new SvcADemo.SvcADemoClient(serviceAChannel); bool keepRunning = true; while (keepRunning) { Console.WriteLine("Input a-bft arg1 arg2 ENTER to execute ServiceA.DoBasicFunctionalTestAsync(message)."); Console.WriteLine("OR the following cmds: a-pubevent, a-pubeventmulti, others TBD."); Console.WriteLine(" OR Press ENTER to quit."); string userInput = Console.ReadLine(); if (string.IsNullOrEmpty(userInput)) { keepRunning = false; } if (keepRunning) { SvcAPublishEventRequest request; SvcAStringReply pubEventReply = new SvcAStringReply(); pubEventReply.Message = "No results returned from the service! Input Error?"; string resultsMsg; ParsedUserInput cmdNArgs = ParseUserInput(userInput); switch (cmdNArgs.Cmd) { // Run a "Basic Functional Test" to verify ServiceA is operational. case "a-bft": SvcABftRequest svcABftRequest = new SvcABftRequest { TestData1 = $"SvcADemo.SvcADemoClient request: Cmd={cmdNArgs.Cmd} Arg1={cmdNArgs.Arg1}, Arg2={cmdNArgs.Arg2}.", TestData2 = $"Test data, test data, test data, ...." }; SvcABftReply svcABftReply = await serviceADemoProxy.DoBasicFunctionalTestAsync(svcABftRequest); resultsMsg = $"SvcADemo.DoBasicFunctionalTestAsync() response =\n{svcABftReply.Message}\n"; Console.WriteLine(resultsMsg); break; // Publish a single event. case "a-pubevent": // Usage: a-pubevent pubsubkind pubsubname topicname somepayloadstring // Example: a-pubevent daprpubsub svs-pubsub-asbtopic svcADemoEvent1 payload=tttttt // The only pubsubkind supported in this sample is daprpubsub // The only pubsubname supported in this sample is svs-pubsub-asbtopic, although it is easy // for YOU to add the code to ServiceB to support the default dapr redis pubsubname = pubsub. request = new SvcAPublishEventRequest(); request.PubSubKind = cmdNArgs.Arg1; request.PubSubName = cmdNArgs.Arg2; request.TopicName = cmdNArgs.Arg3; request.EventPayload = cmdNArgs.Arg4; // TODO -- ALWAYS USE the ASYNC version of serviceAProxy.PublishEventViaDapr as shown below. // TODO -- Using the non-async version results in GetAwaiter() error. pubEventReply = await serviceADemoProxy.PublishEventAsync(request); resultsMsg = $"Service response =\n {pubEventReply.Message}\n"; Console.WriteLine(resultsMsg); break; // Publish a stream of multiple events case "a-pubeventmulti": // Usage: a-pubeventmulti pubsubkind pubsubname topicname somepayloadstring nevents delaymsec // Example: a-pubeventmulti daprpubsub svs-pubsub-asbtopic svcADemoEvent1 payload=tttttt 100 20 // Above example will send 100 events (nevents = 100) with a time delay of 20 millisec between // each send (delaymsec = 20); // The only pubsubkind supported in this sample is daprpubsub // The only pubsubname supported in this sample is svs-pubsub-asbtopic, although it is easy // for YOU to add the code to ServiceB to support the default dapr redis pubsubname = pubsub. request = new SvcAPublishEventRequest(); request.PubSubKind = cmdNArgs.Arg1; request.PubSubName = cmdNArgs.Arg2; request.TopicName = cmdNArgs.Arg3; int nEvents; bool isParsed = Int32.TryParse(cmdNArgs.Arg5, out nEvents); if (!isParsed) { Console.WriteLine("\n** ERROR: Input value 'nevents' is <= 0. Please try again."); } int delayMsec; Int32.TryParse(cmdNArgs.Arg6, out delayMsec); for (int i = 1; i < nEvents + 1; i++) { request.EventPayload = ComposeEventPayload(i, cmdNArgs.Arg4); pubEventReply = await serviceADemoProxy.PublishEventAsync(request); Console.WriteLine($"Sent Message number {i} of {nEvents} messages to send.\n\t\t\t\t\tMsg = {request.EventPayload}"); // Don't clutter up the display unless there are errors. if (pubEventReply.Message.Contains("Error")) { resultsMsg = $"Service response = {pubEventReply.Message}\n"; Console.WriteLine(resultsMsg); } await Task.Delay(delayMsec); } break; default: Console.WriteLine($"Unrecognized cmd. userInput = {userInput}. Try again!"); break; } // End switch. } else { Console.WriteLine($"QuickTestClient quitting on user pressing Enter with no other input."); } } // End while keep running. Console.WriteLine("QuickTestClient EXITING!"); await Task.Delay(1000); }