none
Since no TimeZoneInfo in SL, created Domain Data Service: ComplexType issue.

    Question

  • Hello,

    Our Silverlight 5 RIA application needs to present a list of TimeZoneInfo objects to the user, so he/she can select a particular timezone, which is stored in the database and used for some mapping functions.  Unfortunately, Silverlight does not provide TimeZoneInfo.GetSystemTimeZones() nor many of the related functions from .NET, so I am trying to create operations in a domain data service (TimeZoneDDS) to provide these function from the server.  I know that there are a lot of ways to deal with TimeZone information, but this is the method that we must use in this project. 

    Right now, I am trying to return the list from TimeZoneInfo.GetSystemTimeZones(), but am hitting an error.  Unfortunately, I am receiving an error that seems to indicate that RIA is not happy about returning a list of TimeZoneInfo objects.  From my research on the Net, the latest version of RIA supports complex objects, so I am at a loss as to how to fix this issue.  I would appreciate your help in tweaking the following code so it will work.  I am sure that this posting will help others who need to accomplish this same kind of task.

    ERROR:
    Operation named 'GetSystemTimeZones' does not conform to the required signature. Return types must be an entity or complex type, a collection of entities or complex types, or one of the predefined serializable types.

    Thank you for your time and help,

    Mike

    SILVERLIGHT CLIENT:
                InvokeOperation<IEnumerable<TimeZoneInfo>> io = context.GetSystemTimeZones();
                io.Completed += (s1, ea1) =>
                {
                    if (!io.HasError && !io.IsCanceled)
                    {  
                        timeZoneLookUpEdit.ItemsSource = io.Value;
                    }
                };
    
    DOMAIN SERVICE:
            /// <summary>
            /// Provides a list of all the TimeZoneInfo objects from the server.
            /// </summary>
            /// <remarks>Silverlight does not provide this functionality,
            /// so this method provides the list of TimeZoneInfo objects
            /// that the server contains.  These can be used by the application.</remarks>
            /// <returns>List of TimeZoneInfo objects, which can be empty.</returns>
            [Invoke]
            public List<TimeZoneInfo> GetSystemTimeZones()
            {
                ReadOnlyCollection<TimeZoneInfo> tzi = TimeZoneInfo.GetSystemTimeZones();
                return (tzi == null) ? new List<TimeZoneInfo>() : tzi.ToList<TimeZoneInfo>();
            }
    Saturday, February 11, 2012 10:26 PM

Answers

  • I couldn't figure out how to return a list of TimeZoneInfo items (complex type), so I created my own lightweight class that contained the bare minimum that I needed: Id and Display name.  I used it to serialize what was needed from the DDS to the client. 

    I still would love to learn why I couldn't pass back a list of TimeZoneInfo objects, but at least I have a work-around that works.

    /////////////////////////////////////

        [Serializable]
        public class AbohTimeZoneInfo
        {
            /// <summary>
            /// A friendly version of the time zone name.
            /// </summary>
            public string DisplayName { get; private set; }

            [Key]
            // The unique id assigned to the time zone.
            public string Id { get; private set; }

            /// <summary>
            /// Constructor
            /// </summary>
            public AbohTimeZoneInfo()
            {
                Id = string.Empty;
                DisplayName = string.Empty;
            }

            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="tzi">A TimeZoneInfo</param>
            public AbohTimeZoneInfo(TimeZoneInfo tzid)
            {
                Id = tzid.Id;
                DisplayName = tzid.DisplayName;
            }
        }

    //////////////////////////// DDS /////////////////////////

             [Query]
             public List<AbohTimeZoneInfo> GetSystemTimeZones()
            {
                 List<AbohTimeZoneInfo> list = new List<AbohTimeZoneInfo>();
                 foreach(TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
                 {
                     list.Add(new AbohTimeZoneInfo(tzi));
                 }

                 return list;
             }

    ///////////////////////////////// CLIENT ////////////////

    LoadOperation<AbohTimeZoneInfo> lo = tzContext.Load(tzContext.GetSystemTimeZonesQuery());
            lo.Completed += (s, ea) =>
            {
                if (!lo.HasError && !lo.IsCanceled)
                {
                    foreach (AbohTimeZoneInfo p in lo.Entities)
                    {
                        ;
                    }
                }
            };

    Sunday, February 12, 2012 7:54 AM