protected override async Task<object> InternalProcessMessageAsync(i.ten.bew.Messaging.ServiceBusMessage message)
        {
            ISerializer serializer = Root.ServiceBusInstance.GetLocalService<ISerializer>();

            var inputRequest = (Payload)serializer.Deserialize(message.Data);
            object rv = null;

            object outputRequest;

            bool canHandleRequest = CanHandleRequest(inputRequest, out outputRequest);

            if (canHandleRequest)
            {
                rv = HandleRequest(outputRequest);

                if (rv != null)
                {
                    var payload = serializer.Serialize(rv);
                    ServiceBusMessage replyMessage = ServiceBusMessage.CreateReply(message, payload, DataFormatEnum.JSON);
                    Root.ServiceBusInstance.SendAsync(replyMessage);
                }
            }

            return rv;
        }
Esempio n. 2
0
        protected override async Task <object> InternalProcessMessageAsync(i.ten.bew.Messaging.ServiceBusMessage message)
        {
            ISerializer serializer = Root.ServiceBusInstance.GetLocalService <ISerializer>();
            object      rv         = null;

            switch (message.DataFormat)
            {
            case DataFormatEnum.BinaryNet:
                rv = serializer.Deserialize(message.Data);
                break;

            case DataFormatEnum.JSON:

                string json = System.Text.Encoding.UTF8.GetString(message.Data);

                if (json.IndexOf("RequestType") > 0)
                {
                    rv = JsonConvert.DeserializeObject <CacheRequest>(json);
                }
                else
                {
                    rv = JsonConvert.DeserializeObject <CacheResponse>(json);
                }

                break;

            default:
                throw new Exception("Unrecognized format enum.");
            }

            if (rv is CacheRequest)
            {
                CacheRequest request = (CacheRequest)rv;

                object outputRequest;

                bool canHandleRequest = CanHandleRequest(request, out outputRequest);

                if (canHandleRequest)
                {
                    rv = HandleRequest(request);

                    if (rv != null)
                    {
                        var payload = serializer.Serialize(rv);
                        ServiceBusMessage replyMessage = ServiceBusMessage.CreateReply(message, payload, DataFormatEnum.BinaryNet);
                        Root.ServiceBusInstance.SendAsync(replyMessage);
                    }
                }
            }
            else if (rv is CacheResponse)
            {
            }
            else
            {
                rv = null;
            }

            return(rv);
        }
Esempio n. 3
0
        protected async override Task <object> InternalProcessMessageAsync(ServiceBusMessage message)
        {
            Stopwatch watch = Stopwatch.StartNew();

            Interlocked.Increment(ref _receivedMessage);

            lock (typeof(CsvProcessor))
            {
                _maxAge = Math.Max(Math.Abs(message.Age.Ticks), _maxAge);
            }

            Payload payload = null;

            try
            {
                string jsonSerialized = System.Text.Encoding.UTF8.GetString(message.Data);

                payload = await JsonConvert.DeserializeObjectAsync <Payload>(jsonSerialized);

                if (payload.IsRequest)
                {
                    Interlocked.Increment(ref _requestCount);

                    var responsePayload = new Payload()
                    {
                        IsRequest = false,
                        Sql       = payload.Sql,
                        Result    = new List <string>()
                    };

                    string connectionString = ConfigurationManager.ConnectionStrings["default"].ConnectionString;

                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        await connection.OpenAsync();

                        using (SqlCommand command = new SqlCommand(payload.Sql, connection))
                        {
                            command.CommandType    = System.Data.CommandType.Text;
                            command.CommandTimeout = 60;
                            command.CommandText    = payload.Sql;

                            using (var reader = await command.ExecuteReaderAsync())
                            {
                                bool read = await reader.ReadAsync();

                                if (read)
                                {
                                    int      recordIndex     = 0;
                                    object[] fieldsFlyWeight = new object[reader.FieldCount];

                                    while (read != false)
                                    {
                                        var values = reader.GetValues(fieldsFlyWeight);
                                        recordIndex++;
                                        var csv = String.Join(",", fieldsFlyWeight);
                                        responsePayload.Result.Add(csv);
                                        read = await reader.ReadAsync();
                                    }
                                }
                            }
                        }
                    }

                    if (message.OriginatorRequiresReply)
                    {
                        var data         = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(responsePayload));
                        var replyMessage = ServiceBusMessage.CreateReply(message, data, DataFormatEnum.BinaryNet);
                        Interlocked.Increment(ref _replyCount);
                        Root.ServiceBusInstance.SendAsync(replyMessage);
                    }
                }
                else
                {
                    Interlocked.Increment(ref _responseCount);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                lock (typeof(CsvProcessor))
                {
                    _maxProcessingTime = Math.Max((watch.Elapsed).Ticks, _maxProcessingTime);
                }

                watch.Stop();
            }

            return(payload);
        }