public async Task Subscribe(string endpointName, Action <IRoutableMessage> MessageHandler, Func <Exception, Task> ErrorHandler) { // Get the subscription information from the endpointName setting in the configuration file IApplicationSecretsConnectionStrings subscriber = _applicationSecrets.Secret(endpointName); string connectionString = subscriber.Value; string topicName = subscriber[Topic]; string subscriptionName = subscriber[Subscription]; string path = EntityNameHelper.FormatSubscriptionPath(topicName, subscriptionName); var receiver = new MessageReceiver(connectionString, path); receiver.RegisterMessageHandler( async(message, cancellationToken) => { await receiver.CompleteAsync(message.SystemProperties.LockToken); string body = Encoding.UTF8.GetString(message.Body); IRoutableMessage msg = body.MessageFromBus(); MessageHandler(msg); }, new MessageHandlerOptions(e => ErrorHandler(e.Exception)) { AutoComplete = false, MaxConcurrentCalls = 2 }); }
static void MessageHandler(IRoutableMessage message) { message.TraceInformation("Received from service bus"); RegisterClientRequest request = message.GetPayload <RegisterClientRequest>(); request.TraceInformation("Payload"); }
public async Task Publish(IRoutableMessage message) { try { // Get the connection string from the destination address in the message IApplicationSecretsConnectionStrings publisher = _applicationSecrets.Secret(message.Recipient); // Create a sender for the topic string topicName = publisher[Topic]; string connectionString = publisher.Value; Message serviceBusMessage = new Message(Encoding.UTF8.GetBytes(message.ToString())); ISenderClient topicClient = new TopicClient(connectionString, topicName); await topicClient.SendAsync(serviceBusMessage); } catch (Exception e) { Console.WriteLine(e); throw; } }
public void ExportRoutingDataTo(IRoutableMessage message) { //So, this is sorta a hack but it's a good one //for preformance if (message is RequestMessage) { RequestMessage castedMessage = message as RequestMessage; lock (syncObj) { //No reason to copy null stack if (_routingCodeStack != null) //We should transfer the routing stack but also preserve the other routing stack //We probably won't need it but just in case the user wants to do something with it still castedMessage._routingCodeStack = new Stack<int>(_routingCodeStack.Reverse()); //We must create a reverse copy of the stack:http://stackoverflow.com/questions/7391348/c-sharp-clone-a-stack } } else { if (message is ResponseMessage) { ResponseMessage castedMessage = message as ResponseMessage; lock (syncObj) { //No reason to copy null stack if (_routingCodeStack != null) //We should transfer the routing stack but also preserve the other routing stack //We probably won't need it but just in case the user wants to do something with it still castedMessage._routingCodeStack = new Stack<int>(_routingCodeStack.Reverse()); //We must create a reverse copy of the stack:http://stackoverflow.com/questions/7391348/c-sharp-clone-a-stack } } } }
public async Task Publish(IRoutableMessage message) { message.TraceInformation("Local service bus publish not implemented yet"); }
public static IRoutableMessage MessageFromBus(this string message) { IRoutableMessage msg = JsonConvert.DeserializeObject <RoutableMessage>(message); return(msg); }
public static async Task Publish(this object message, IMessageBus bus, string sender, string recipient) { IRoutableMessage msg = message.BusTopicMessage(sender, recipient); await bus.Publish(msg); }