diff --git a/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer.UnitTests/ServiceBusTests/MessagePropertiesTests.cs b/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer.UnitTests/ServiceBusTests/MessagePropertiesTests.cs index 4769208a5307..aa5fa407ce26 100644 --- a/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer.UnitTests/ServiceBusTests/MessagePropertiesTests.cs +++ b/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer.UnitTests/ServiceBusTests/MessagePropertiesTests.cs @@ -36,6 +36,7 @@ public sealed class MessagePropertiesTests public void MessageProperties() { string queueName = UsesUniqueQueueAttribute.QueueName; + DateTimeOffset originalDateTime = DateTimeOffset.Now; BrokeredMessageSettings messageSettings = BrokeredMessageSettings.CreateFromText("text/plain", "This is a test."); messageSettings.Properties.Add("StringProperty", "Test"); @@ -43,6 +44,7 @@ public void MessageProperties() messageSettings.Properties.Add("BoolPropertyFalse", false); messageSettings.Properties.Add("NullProperty", null); messageSettings.Properties.Add("NumberProperty", 123); + messageSettings.Properties.Add("TimeProperty", originalDateTime); Configuration.ServiceBus.SendMessageAsync(queueName, messageSettings).AsTask().Wait(); BrokeredMessageInfo message = Configuration.ServiceBus.GetQueueMessageAsync(queueName, TimeSpan.FromSeconds(10)).AsTask().Result; @@ -52,6 +54,7 @@ public void MessageProperties() Assert.True(message.Properties.ContainsKey("BoolPropertyTrue")); Assert.True(message.Properties.ContainsKey("BoolPropertyFalse")); Assert.True(message.Properties.ContainsKey("NumberProperty")); + Assert.True(message.Properties.ContainsKey("TimeProperty")); // The server does not store/return null properties. Assert.False(message.Properties.ContainsKey("NullProperty")); @@ -59,6 +62,15 @@ public void MessageProperties() Assert.Equal((bool)message.Properties["BoolPropertyTrue"], true); Assert.Equal((bool)message.Properties["BoolPropertyFalse"], false); Assert.Equal(Convert.ToInt32(message.Properties["NumberProperty"]), 123); + + // Times must be identical to a second. + DateTimeOffset readDateTime = (DateTimeOffset)message.Properties["TimeProperty"]; + Assert.Equal(originalDateTime.Year, readDateTime.Year); + Assert.Equal(originalDateTime.Month, readDateTime.Month); + Assert.Equal(originalDateTime.Day, readDateTime.Day); + Assert.Equal(originalDateTime.Hour, readDateTime.Hour); + Assert.Equal(originalDateTime.Minute, readDateTime.Minute); + Assert.Equal(originalDateTime.Second, readDateTime.Second); } /// diff --git a/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer/Microsoft.WindowsAzure.ServiceLayer.csproj b/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer/Microsoft.WindowsAzure.ServiceLayer.csproj index ea5f50b4bf97..9b78a2f7f42e 100644 --- a/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer/Microsoft.WindowsAzure.ServiceLayer.csproj +++ b/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer/Microsoft.WindowsAzure.ServiceLayer.csproj @@ -139,6 +139,9 @@ + + + 11.0 diff --git a/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer/ServiceBus/CustomPropertiesDictionary.cs b/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer/ServiceBus/CustomPropertiesDictionary.cs index e40adb81630d..3faf69ecdae6 100644 --- a/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer/ServiceBus/CustomPropertiesDictionary.cs +++ b/microsoft-azure-servicelayer/Microsoft.WindowsAzure.ServiceLayer/ServiceBus/CustomPropertiesDictionary.cs @@ -1,4 +1,19 @@ -using System; +// +// Copyright 2012 Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; using System.Collections.Generic; using System.Globalization; using System.Diagnostics; @@ -42,7 +57,6 @@ internal CustomPropertiesDictionary(HttpResponseMessage response) } else { - // The string could not be deserialized into Json value; storing raw string data. Add(key, valueString); } } @@ -80,9 +94,20 @@ private static object DecodeValue(JsonValue value) return value.GetNumber(); default: - Debug.Assert(value.ValueType == JsonValueType.String); - return value.GetString(); + break; } + + // Extract extra types that are stored as strings. + Debug.Assert(value.ValueType == JsonValueType.String); + string stringValue = value.GetString(); + DateTimeOffset dateTime; + + if (DateTimeOffset.TryParse(stringValue, out dateTime)) + { + return dateTime.ToUniversalTime(); + } + return stringValue; + } /// @@ -134,9 +159,13 @@ private static JsonValue EncodeValue(object value) { return JsonValue.CreateStringValue((string)value); } + else if (IsType(type)) + { + return CreateDateTimeValue((DateTimeOffset)value); + } else { - //TODO: error message! + //TODO: Error message. throw new InvalidCastException(); } } @@ -151,5 +180,17 @@ private static bool IsType(Type type) { return object.ReferenceEquals(typeof(T), type); } + + /// + /// Creates a Json string for the given date/time. + /// + /// Date/time. + /// Json string value with the date/time. + private static JsonValue CreateDateTimeValue(DateTimeOffset dateTime) + { + dateTime = dateTime.ToUniversalTime(); + string dateTimeString = dateTime.ToString("r"); + return JsonValue.CreateStringValue(dateTimeString); + } } }