Method PublishAsync
PublishAsync(Message, DeliveryOption, Boolean)
This method is used to Publish a message asynchronously in the cache with specified DeliveryOption and the option to notify the publisher if the message has failed to deliver because of expiration, eviction or internal system issue.
Declaration
Task PublishAsync(Message message, DeliveryOption deliverOption, bool notifyDeliveryFailure = false)
Parameters
Type | Name | Description |
---|---|---|
Message | message | Message to be published |
DeliveryOption | deliverOption | Delivery Option |
System.Boolean | notifyDeliveryFailure | Is MessageDeliveryFailure event required for this message |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | Returns task |
Examples
The following example demonstrates how to publish message on a topic.
First initialize cache.
Cache cache = NCache.InitializeCache("myCache");
Then get messaging service from cache.
IMessagingService messagingService=cache.MessagingService;
Then get topic from messagingService
ITopic topic=messagingService.GetTopic("mytopic");
if(topic==null) //If topic not exists create it.
{
topic=messagingService.CreateTopic("mytopic");
}
Then publish message on topic.
object payload = "mymessage";
Message message = new Message(payload); //creating message
topic.PublishAsync(message, DeliveryOption.All).ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion)
{
Console.WriteLine("Message Published Successfully");
}
if (task.Status == TaskStatus.Faulted)
{
Console.WriteLine("Error has occurred");
}
if (task.Exception != null)
{
Console.WriteLine(task.Exception);
}
});