private async Task <SvcAStringReply> PublishEventViaDapr(SvcAPublishEventRequest request) { SvcAStringReply reply = new SvcAStringReply(); reply.Message = "** ServiceADemo.PublishEventViaDapr(): Publish succeeded."; try { m_Logger.LogInformation( $"** ServiceADemo.PublishEventViaDapr(): PubsubName={request.PubSubName}, TopicName={request.TopicName}, Payload={request.EventPayload}"); // For display in a running demo. Console.WriteLine( $"** ServiceADemo publish to Topic={request.TopicName}: {request.EventPayload}"); await m_DaprProxy.PublishEventAsync <string>(request.PubSubName, request.TopicName, request.EventPayload); } catch (System.Exception ex) { reply.Message = $"** ServiceADemo.PublishEventViaDapr() Error -- Caught exception = {ex}"; } // For error detection in a running demo. if (reply.Message.Contains("Error")) { Console.WriteLine(reply.Message); } return(reply); }
public override async Task <SvcAStringReply> PublishEvent(SvcAPublishEventRequest request, ServerCallContext context) { SvcAStringReply reply; switch (request.PubSubKind) { case NamesOfQueuesNPubSubs.DaprPubSubKind: reply = await PublishEventViaDapr(request); break; default: reply = new SvcAStringReply(); reply.Message = $"** ServiceADemo.PublishEvent(): Error -- Cannot publish. Unknown request. PubSubKind = {request.PubSubKind}"; break; } return(reply); }
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); }