예제 #1
0
        /// <summary>
        /// Asynchronously adds a metadata payload to Google PubSub
        /// </summary>
        /// <param name="metadata">metadata to add to queue</param>
        /// <returns>success of operation</returns>
        public async Task <bool> Enqueue(QueueMetadata metadata)
        {
            try
            {
                PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();

                TopicName topicName = new TopicName(this.cloud.GoogleProjectID, this.cloud.QueueName);
                await this.CreateTopicIfNotExistsAsync(publisher, topicName);

                var message = new PubsubMessage()
                {
                    Data = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(metadata)),
                };

                var messageList = new List <PubsubMessage>()
                {
                    message
                };

                await publisher.PublishAsync(topicName, messageList);
            }
            catch
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        /// <inherithdoc/>
        public IBatchContainer FromPullResponseMessage(PubsubMessage msg, long sequenceId)
        {
            var batchContainer = _serializationManager.DeserializeFromByteArray <PubSubBatchContainer>(msg.Data.ToByteArray());

            batchContainer.RealSequenceToken = new EventSequenceTokenV2(sequenceId);
            return(batchContainer);
        }
        public async Task PublishAsync()
        {
            string projectId = _fixture.ProjectId;
            string topicId   = _fixture.CreateTopicId();

            // Snippet: PublishAsync(*,*,CallSettings)
            // Additional: PublishAsync(*,*,CancellationToken)
            PublisherClient client = PublisherClient.Create();
            // Make sure we have a topic to publish to
            string topicName = PublisherClient.FormatTopicName(projectId, topicId);
            await client.CreateTopicAsync(topicName);

            PubsubMessage message = new PubsubMessage
            {
                // The data is any arbitrary ByteString. Here, we're using text.
                Data = ByteString.CopyFromUtf8("Hello, Pubsub"),
                // The attributes provide metadata in a string-to-string dictionary.
                Attributes =
                {
                    { "description", "Simple text message" }
                }
            };
            await client.PublishAsync(topicName, new[] { message });

            // End snippet
        }
예제 #4
0
        public void StartPeriodicallyPublish(int timeIntervalInSeconds)
        {
            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();
            TopicName topicName = new TopicName("k8s-brewery", "sdk-example-test-topic");

            try
            {
                publisher.CreateTopic(topicName);
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to create topic");
            }

            int j = 0;

            Task.Run(() =>
            {
                while (!token.IsCancellationRequested)
                {
                    // Publish a message to the topic.
                    PubsubMessage message = new PubsubMessage
                    {
                        Data       = ByteString.CopyFromUtf8("Test message number " + j),
                        Attributes =
                        {
                            { "description", "Simple text message number " + j }
                        }
                    };
                    publisher.Publish(topicName, new[] { message });
                    j++;
                    Thread.Sleep(timeIntervalInSeconds * 1000);
                }
            });
        }
예제 #5
0
        public void Publish()
        {
            string projectId = _fixture.ProjectId;
            string topicId   = _fixture.CreateTopicId();

            // Snippet: Publish(*,*,*)
            PublisherClient client = PublisherClient.Create();
            // Make sure we have a topic to publish to
            TopicName topicName = new TopicName(projectId, topicId);

            client.CreateTopic(topicName);

            PubsubMessage message = new PubsubMessage
            {
                // The data is any arbitrary ByteString. Here, we're using text.
                Data = ByteString.CopyFromUtf8("Hello, Pubsub"),
                // The attributes provide metadata in a string-to-string dictionary.
                Attributes =
                {
                    { "description", "Simple text message" }
                }
            };

            client.Publish(topicName, new[] { message });
            // End snippet
        }
예제 #6
0
        public Topic CreateTopic(string projectId, string topicId)
        {
            PublisherServiceApiClient publisher =
                PublisherServiceApiClient.Create();
            var   topicName = TopicName.FromProjectTopic(projectId, topicId);
            Topic topic     = null;

            try
            {
                topic = publisher.CreateTopic(topicName);
                Console.WriteLine($"Topic {topic.Name} created.");
            }
            catch (RpcException e) when(e.Status.StatusCode ==
                                        StatusCode.AlreadyExists)
            {
                Console.WriteLine($"Topic {topicName} already exists.");
            }

            PubsubMessage message = new PubsubMessage
            {
                // The data is any arbitrary ByteString. Here, we're using text.
                Data = ByteString.CopyFromUtf8("Hello, Pubsub"),
                // The attributes provide metadata in a string-to-string dictionary.
                Attributes =
                {
                    { "description", "Simple text message" }
                }
            };

            publisher.Publish(topicName, new[] { message });

            return(topic);
        }
예제 #7
0
        private static void RePublishMessages()
        {
            var json = File.ReadAllText(jsonFile);
            List <PubsubMessage> unPublishedMessages = new List <PubsubMessage>();

            try
            {
                var jsonObj      = JObject.Parse(json);
                var messageArray = jsonObj.GetValue("messages") as JArray;

                if (messageArray != null)
                {
                    foreach (var newMessage in messageArray)
                    {
                        PubsubMessage pubsubMessage = new PubsubMessage
                        {
                            Data       = ByteString.CopyFromUtf8(newMessage["Data"].ToString()),
                            Attributes =
                            {
                                { "description", newMessage["Attributes"]["description"].ToString() }
                            }
                        };

                        //Now we can call Google Publish Method Again
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Add Error : " + ex.Message.ToString());
            }
        }
예제 #8
0
        private void HandlePubSubEvent(object sender, PubSubEventArgs pubSubEventArgs)
        {
            // The PubSubMessage header should only contain the message type.
            // The message data should contain all information to process the message.

            PubsubMessage pubsubMessage     = pubSubEventArgs.PubsubMessage;
            string        pubSubMessageType = GetPubSubMessageAttribute(pubsubMessage, "type");
            // the gameId should not be stored in the message Attributes but rather in the data
            string gameIdString = GetPubSubMessageAttribute(pubsubMessage, "comp_id");
            Guid   gameId       = Guid.Parse(gameIdString);
            string pubsubData   = pubsubMessage.Data.ToStringUtf8();

            switch (pubSubMessageType)
            {
            case "marketOdds":
                HandleMarketOddsUpdate(pubsubData, gameId);
                break;

            case "liveOdds":
                HandleLiveMarketOddsUpdate(pubsubData, gameId);
                break;

            case "odds":
                HandleOddsUpdate(pubsubData, gameId);
                break;

            case "test":
                HandleTest(pubsubData);
                break;

            default:
                Logger.Error($"Could not handle PubSub message type = '{pubSubMessageType}'");
                return;
            }
        }
예제 #9
0
        static async Task <SubscriberClient.Reply> HandleMessage(PubsubMessage message, CancellationToken token)
        {
            var messageBody = Encoding.UTF8.GetString(message.Data.ToByteArray());

            if (messageBody != "RUN_SPEEDTEST")
            {
                return(SubscriberClient.Reply.Nack);
            }

            Console.WriteLine("Running speedtest!");

            var runner   = new SpeedTestRunner(_loggerConfig.LoggerLocation);
            var testData = runner.RunSpeedTest();
            var results  = new TestResult
            {
                User      = _loggerConfig.UserId,
                Device    = _loggerConfig.LoggerId,
                Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
                Data      = testData
            };

            var success = false;

            using (var client = new SpeedTestApiClient(_loggerConfig.ApiUrl))
            {
                success = await client.PublishTestResult(results);
            }

            Console.WriteLine($"SpeedTest {(success == true ? "complete" : "failed")}!");
            return(success == true ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack);
        }
        public async Task <bool> PublishAsync(Event[] message, string topicName)
        {
            try{
                TopicName       topic = new TopicName(_busSettings.ProjectId, topicName);
                PublisherClient publisher;
                publisher = await PublisherClient.CreateAsync(topic);

                var pubSubMessage = new PubsubMessage {
                    Data = ByteString.CopyFrom(JsonConvert.SerializeObject(message), Encoding.UTF8)
                };
                if (_busSettings.Token != null && _busSettings.Token != "")
                {
                    pubSubMessage.Attributes["token"] = _busSettings.Token;
                }
                var result = await publisher.PublishAsync(pubSubMessage);

                if (result == "")
                {
                    return(false);
                }
                return(true);
            } catch (Exception ex) {
                return(false);
            }
        }
        /// <summary>
        /// Extracts and deserializes a given message
        /// </summary>
        /// <param name="message">The message that needs deserialized</param>
        /// <returns>T.</returns>
        /// <exception cref="InvalidOperationException">Cannot access the message content for message {message.MessageId}</exception>
        private T GetTypedMessageContent <T>(PubsubMessage message) where T : class
        {
            string content = message.Data.ToStringUtf8();

            // Check for no content (we cannot process this).
            if (content.IsNullOrEmpty())
            {
                throw new InvalidOperationException($"Cannot access the message content for message {message.MessageId}");
            }

            try
            {
                // Deserialize to a specific type.
                return(JsonConvert.DeserializeObject <T>(content));
            }
            catch (Exception ex) when(ex is JsonReaderException || ex is JsonSerializationException)
            {
                // If we are actually expecting T to be a system type, just return without serialization.
                if (typeof(T).IsSystemType())
                {
                    return(content as T);
                }

                _logger?.LogWarning($"Could not map message to {typeof(T)}, sending message to error flow");

                // NOTE: Maybe should automatically dead-letter the message if conversion fails?
                return(null);
            }
        }
예제 #12
0
파일: Program.cs 프로젝트: jatin-dev/PubSub
        private static void PublishToTopic()
        {
            var topicName = new TopicName(ProjectId, TopicId);

            var publisher = PublisherServiceApiClient.Create();

            // Create a message
            var message = new PubsubMessage()
            {
                Data = ByteString.CopyFromUtf8("hello world")
            };

            message.Attributes.Add("myattrib", "its value");

            // Add it to the list of messages to publish
            var messageList = new List <PubsubMessage>()
            {
                message
            };

            // Publish it
            Console.WriteLine("Publishing...");
            var response = publisher.Publish(topicName, messageList);

            // Get the message ids GCloud gave us
            Console.WriteLine("  Message ids published:");
            foreach (string messageId in response.MessageIds)
            {
                Console.WriteLine($"  {messageId}");
            }
        }
예제 #13
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                    if (CanConnectToPubSub())
                    {
                        // Subscribe to the topic.
                        SubscriberServiceApiClient subscriber = CreateSubscriber();
                        var subscriptionName = new SubscriptionName(GCP_PROJECT, GCP_SUBSCRIBER);

                        PullResponse response = subscriber.Pull(subscriptionName, returnImmediately: true, maxMessages: 100);
                        foreach (ReceivedMessage received in response.ReceivedMessages)
                        {
                            PubsubMessage msg = received.Message;
                            _logger.LogInformation($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
                            _logger.LogInformation($"Text: '{msg.Data.ToStringUtf8()}'");
                        }

                        if (response.ReceivedMessages.Any())
                        {
                            subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId));
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError($"{e.Message} - {e.StackTrace}");
                }

                await Task.Delay(5000, stoppingToken);
            }
        }
예제 #14
0
        public async Task <string> EnviarMensagem(string mensagem)
        {
            var    topicName                    = new TopicName("estudo-ci-cd", "tp-poc-ex");
            string retornoIdMensagem            = "";
            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();

            // Create a message
            var message = new PubsubMessage()
            {
                Data = ByteString.CopyFromUtf8(mensagem)
            };
            //message.Attributes.Add("myattrib", "its value");
            var messageList = new List <PubsubMessage>()
            {
                message
            };

            var response = await publisher.PublishAsync(topicName, messageList);

            foreach (var item in response.MessageIds)
            {
                retornoIdMensagem = item;
            }
            return(retornoIdMensagem);
        }
예제 #15
0
    async Task <SimpleSubscriber.Reply> Consume(PubsubMessage message, CancellationToken token)
    {
        using (var tokenSource = new CancellationTokenSource())
        {
            var processed                = false;
            var errorHandled             = false;
            var numberOfDeliveryAttempts = 0;
            var headers   = message.Attributes.ToDictionary(k => k.Key, v => v.Value);
            var body      = message.Data.ToByteArray();
            var messageId = message.MessageId;

            while (!processed && !errorHandled)
            {
                try
                {
                    var messageContext = new MessageContext(messageId, headers, body ?? emptyBody, transportTransaction, tokenSource, contextBag);
                    await onMessage(messageContext).ConfigureAwait(false);

                    processed = true;
                }
                catch (Exception ex)
                {
                    ++numberOfDeliveryAttempts;
                    var errorContext = new ErrorContext(ex, headers, messageId, body ?? emptyBody, transportTransaction, numberOfDeliveryAttempts);
                    errorHandled = await onError(errorContext).ConfigureAwait(false) == ErrorHandleResult.Handled;
                }
            }

            if (processed && tokenSource.IsCancellationRequested)
            {
                return(SimpleSubscriber.Reply.Nack);
            }
            return(SimpleSubscriber.Reply.Ack);
        }
    }
예제 #16
0
        public async Task CreateConsumer(TopicName topic)
        {
            SubscriberServiceApiClient subscriber = SubscriberServiceApiClient.Create();

            SubscriptionName subscriptionName = new SubscriptionName(_projectId, "SubscriptionN");

            PubsubMessage messageSubscription = new PubsubMessage();

            var subscriptionCreated = subscriber.CreateSubscription(subscriptionName, topic, pushConfig: null, ackDeadlineSeconds: 60);

            SubscriberClient subscriberClient = await SubscriberClient.CreateAsync(
                subscriptionName);

            var result = subscriberClient.StartAsync(
                async(PubsubMessage message, CancellationToken cancel) =>
            {
                string text = Encoding.UTF8.GetString(message.Data.ToArray());

                messageSubscription = message;

                await Console.Out.WriteLineAsync($"Message {message.MessageId} : {text}");

                Console.WriteLine(message);

                return(await Task.FromResult(SubscriberClient.Reply.Ack));
            });

            await Task.Delay(3000);

            await subscriberClient.StopAsync(CancellationToken.None);

            Console.WriteLine($"Subscriber message {messageSubscription} ");
        }
예제 #17
0
 public IActionResult Index(MessageForm messageForm)
 {
     var model = new MessageList();
     if (!_options.HasGoodProjectId())
     {
         model.MissingProjectId = true;
         return View(model);
     }
     if (!string.IsNullOrEmpty(messageForm.Message))
     {
         // Publish the message.
         var topicName = new TopicName(_options.ProjectId,
             _options.TopicId);
         lock(s_lock) CreateTopicAndSubscriptionOnce(_publisher, topicName);
         var pubsubMessage = new PubsubMessage()
         {
             Data = ByteString.CopyFromUtf8(messageForm.Message)
         };
         pubsubMessage.Attributes["token"] = _options.VerificationToken;
         _publisher.Publish(topicName, new[] { pubsubMessage });
         model.PublishedMessage = messageForm.Message;
     }
     // Render the current list of messages.
     lock (s_lock) model.Messages = s_receivedMessages.ToArray();
     return View(model);
 }
예제 #18
0
        //public Task<SubscriberClient.Reply> OnReceivePubsubMessage(PubsubMessage pubSubMessage, CancellationToken ct, Action doWork)
        //{
        //    bool acknowledge = true;

        //    doWork(pubSubMessage, ct);

        //    return Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack);
        //}

        public Task <SubscriberClient.Reply> OnReceivePubsubMessage(PubsubMessage pubSubMessage, CancellationToken ct, Action <object, CancellationToken> doWork)
        {
            bool acknowledge = true;

            doWork(pubSubMessage, ct);

            return(Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack));
        }
예제 #19
0
        private Task <SubscriberClient.Reply> HandleMessage(PubsubMessage message, CancellationToken token)
        {
            var receivedMessage = message.Data.ToStringUtf8();

            Console.WriteLine($"New message has been received!\nMessage: {receivedMessage}");

            return(Task.Run(() => SubscriberClient.Reply.Ack));
        }
예제 #20
0
        public Task <SubscriberClient.Reply> ProcMessage3(PubsubMessage pubSubMessage, CancellationToken ct, Action a)
        {
            bool acknowledge = true;

            a();

            return(Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack));
        }
예제 #21
0
        public void Overview()
        {
            string projectId      = _fixture.ProjectId;
            string topicId        = _fixture.CreateTopicId();
            string subscriptionId = _fixture.CreateSubscriptionId();

            // Sample: Overview
            // First create a topic.
            PublisherClient publisher = PublisherClient.Create();
            TopicName       topicName = new TopicName(projectId, topicId);

            publisher.CreateTopic(topicName);

            // Subscribe to the topic.
            SubscriberClient subscriber       = SubscriberClient.Create();
            SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId);

            subscriber.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60);

            // Publish a message to the topic.
            PubsubMessage message = new PubsubMessage
            {
                // The data is any arbitrary ByteString. Here, we're using text.
                Data = ByteString.CopyFromUtf8("Hello, Pubsub"),
                // The attributes provide metadata in a string-to-string dictionary.
                Attributes =
                {
                    { "description", "Simple text message" }
                }
            };

            publisher.Publish(topicName, new[] { message });

            // Pull messages from the subscription. We're returning immediately, whether or not there
            // are messages; in other cases you'll want to allow the call to wait until a message arrives.
            PullResponse response = subscriber.Pull(subscriptionName, returnImmediately: true, maxMessages: 10);

            foreach (ReceivedMessage received in response.ReceivedMessages)
            {
                PubsubMessage msg = received.Message;
                Console.WriteLine($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
                Console.WriteLine($"Text: '{msg.Data.ToStringUtf8()}'");
            }

            // Acknowledge that we've received the messages. If we don't do this within 60 seconds (as specified
            // when we created the subscription) we'll receive the messages again when we next pull.
            subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId));

            // Tidy up by deleting the subscription and the topic.
            subscriber.DeleteSubscription(subscriptionName);
            publisher.DeleteTopic(topicName);
            // End sample

            Assert.Equal(1, response.ReceivedMessages.Count);
            Assert.Equal("Hello, Pubsub", response.ReceivedMessages[0].Message.Data.ToStringUtf8());
            Assert.Equal("Simple text message", response.ReceivedMessages[0].Message.Attributes["description"]);
        }
예제 #22
0
        private static string GetPubSubMessageAttribute(PubsubMessage pubsubMessage, string key)
        {
            if (pubsubMessage.Attributes.ContainsKey(key))
            {
                return(pubsubMessage.Attributes[key]);
            }

            return("");
        }
예제 #23
0
        public Task <SubscriberClient.Reply> ProcMessage(PubsubMessage pubSubMessage, CancellationToken ct)
        {
            int    messageCount = 0;
            bool   acknowledge  = true;
            string text         = System.Text.Encoding.UTF8.GetString(pubSubMessage.Data.ToArray());

            Console.WriteLine($"Message {pubSubMessage.MessageId}: {text}");
            Interlocked.Increment(ref messageCount);
            return(Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack));
        }
예제 #24
0
        private PubsubMessage CreateMessage(Item item)
        {
            var message = new PubsubMessage()
            {
                Data = ByteString.CopyFromUtf8(JsonSerializer.Serialize(item))
            };

            message.Attributes.Add("itemId", item.Id.ToString());
            return(message);
        }
예제 #25
0
 public async Task Create(Entityframework.IEvent Profile)
 {
     var message       = JsonConvert.SerializeObject(Profile);
     var pubsubMessage = new PubsubMessage()
     {
         Data = ByteString.CopyFromUtf8(message)
     };
     // pubsubMessage.Attributes["token"] = _options.VerificationToken;
     await publisher.PublishAsync(pubsubMessage);
 }
예제 #26
0
        public async static Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Environment.SetEnvironmentVariable(
                "GOOGLE_APPLICATION_CREDENTIALS",
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "My First Project-065d320bab2e.json"));
            CompanyServiceClient companyServiceClient = CompanyServiceClient.Create();

            // GoogleCredential credential = GoogleCredential.GetApplicationDefault();
            Google.Cloud.Talent.V4Beta1.Company c = new Google.Cloud.Talent.V4Beta1.Company
            {
                Name        = "ADI",
                DisplayName = "ADI",

                ExternalId = "123"
            };
            Google.Cloud.Talent.V4Beta1.CreateCompanyRequest request = new Google.Cloud.Talent.V4Beta1.CreateCompanyRequest
            {
                Company = c
            };

            Google.Cloud.Talent.V4Beta1.Company response = companyServiceClient.CreateCompany(request);
            Console.WriteLine("Created Company");
            Console.WriteLine($"Name: {response.Name}");
            Console.WriteLine($"Display Name: {response.DisplayName}");
            Console.WriteLine($"External ID: {response.ExternalId}");

            //JobServiceClient jobServiceClient = JobServiceClient.Create();
            // TenantName tenantName = TenantName.FromProjectTenant(projectId, tenantId);
            PublisherClient publisher = await PublisherClient.CreateAsync(topicName, null, null);

            var pubsubMessage = new PubsubMessage()
            {
                Data = ByteString.CopyFromUtf8("this is new  sample message")
            };
            string messageId = await publisher.PublishAsync(pubsubMessage);

            var subscriptionName = new SubscriptionName(ProjectId, SubscriptionId);
            var subscription     = await SubscriberClient.CreateAsync(subscriptionName);

            try
            {
                // PullResponse response = subscription.(subscriptionName, true, 10);
                await subscription.StartAsync((pubsubMessage, cancellationToken) =>
                {
                    // Process the message here.
                    var all = Task.FromResult(SubscriberClient.Reply.Ack);
                    return(all);
                });
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong: {0}", e.Message);
            }
        }
        public async Task SendMessage(VehiclePositionEvent message)
        {
            var messageJson = JsonConvert.SerializeObject(message);

            var pubsubMessage = new PubsubMessage()
            {
                Data = ByteString.CopyFromUtf8(messageJson)
            };

            await _client.PublishAsync(pubsubMessage);
        }
예제 #28
0
        public async Task ProduceAsync(string key, T message)
        {
            var serialized = serializer.SerializeToBytes(message);

            var pubSubMessage = new PubsubMessage
            {
                Data = ByteString.CopyFrom(serialized)
            };

            await publisherClient.PublishAsync(pubSubMessage);
        }
예제 #29
0
        public async Task NoRetry(PubsubMessage message)
        {
            await ExecuteCloudEventRequestAsync(
                MessagePublishedData.MessagePublishedCloudEventType,
                new MessagePublishedData { Message = message });

            var logEntry = Assert.Single(GetFunctionLogEntries());

            Assert.Equal(LogLevel.Information, logEntry.Level);
            Assert.Equal("Not retrying...", logEntry.Message);
        }
예제 #30
0
        public async Task <string> SendAsync <T>(T message) where T : class
        {
            var publisher = await this.publisherLazy.Value;
            var psMessage = new PubsubMessage();

            psMessage.Attributes.Add(nameof(Type), typeof(T).FullName);
            var text = JsonConvert.SerializeObject(message);

            psMessage.Data = ByteString.CopyFromUtf8(text);
            return(await publisher.PublishAsync(psMessage));
        }
        //---------

        public PubsubMessage ConvertToPubsubMessage(IEnumerable<string> dataList)
        {
            StringBuilder sb = new StringBuilder();
            string attrMinValue = null;

            if (dataList != null)
            {
                foreach (string data in dataList)
                {
                    sb.AppendLine(data);

                    //---

                    if (this._attrMinCreate)
                    {
                        attrMinValue = this._CalculateMinValue(attrMinValue, data);
                    }
                }
            }
             
            //---

            PubsubMessage message = new PubsubMessage();

            //---

            if (this._options.MessageDataToBase64)
            {
                message.Data = ByteString.FromBase64(Convert.ToBase64String(Encoding.UTF8.GetBytes(sb.ToString())));
            }
            else
            {
                message.Data = ByteString.CopyFromUtf8(sb.ToString());
            }

            if (this._attrMinCreate)
            {
                message.Attributes.Add(this._attrMinName, (attrMinValue ?? string.Empty));
            }

            if (this._options.MessageAttrFixed != null)
            {
                foreach (KeyValuePair<string,string> kv in this._options.MessageAttrFixed)
                {
                    message.Attributes.Add(kv.Key, (kv.Value ?? string.Empty));
                }
            }

            //---

            return message;
        }
 public void CreateTopicMessage(string topicId, PublisherClient publisher)
 {
     // [START publish_message]
     string topicName = PublisherClient.FormatTopicName(_projectId,
         topicId);
     PubsubMessage message = new PubsubMessage
     {
         // The data is any arbitrary ByteString. Here, we're using text.
         Data = ByteString.CopyFromUtf8("Hello Cloud Pub/Sub!"),
         // The attributes provide metadata in a string-to-string
         // dictionary.
         Attributes =
         {
             { "description", "Simple text message" }
         }
     };
     publisher.Publish(topicName, new[] { message });
     // [END publish_message]
 }