Beispiel #1
0
        public static AdditionalFormatContent[] GetFormattedContentToStore(this IAdditionalFormatProvider providerObject, params string[] formatExtensions)
        {
            List <AdditionalFormatContent> contentList = new List <AdditionalFormatContent>(formatExtensions.Length);

            foreach (string extension in formatExtensions)
            {
                AdditionalFormatContent content = null;
                switch (extension)
                {
                case "json":
                {
                    DataContractJsonSerializer serializer =
                        new DataContractJsonSerializer(providerObject.GetType());

                    byte[] dataContent;
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        serializer.WriteObject(memoryStream, providerObject);
                        dataContent = memoryStream.ToArray();
                    }
                    content = new AdditionalFormatContent {
                        Extension = "json", Content = dataContent
                    };
                }
                break;

                default:
                    throw new NotSupportedException("Not supported extension for automatic format support: " + extension);
                }
                contentList.Add(content);
            }
            return(contentList.ToArray());
        }
        public static AdditionalFormatContent[] GetFormattedContentToStore(this IAdditionalFormatProvider providerObject, string masterBlobETag, params string[] formatExtensions)
        {
            if (formatExtensions.Length == 0)
                formatExtensions = providerObject.GetAdditionalFormatExtensions();
            List<AdditionalFormatContent> contentList = new List<AdditionalFormatContent>(formatExtensions.Length);
            IInformationObject iObj = providerObject as IInformationObject;
            string oldMasterETag = null;
            if (iObj != null)
            {
                oldMasterETag = iObj.MasterETag;
            }
            try
            {
                iObj.MasterETag = masterBlobETag;
                foreach (string extension in formatExtensions)
                {
                    AdditionalFormatContent content = null;
                    switch (extension)
                    {
                        case "json":
                            {
                                DataContractJsonSerializer serializer =
                                    new DataContractJsonSerializer(providerObject.GetType());

                                byte[] dataContent;
                                using (MemoryStream memoryStream = new MemoryStream())
                                {
                                    serializer.WriteObject(memoryStream, providerObject);
                                    dataContent = memoryStream.ToArray();

                                }
                                content = new AdditionalFormatContent {Extension = "json", Content = dataContent};
                            }
                            break;
                        default:
                            throw new NotSupportedException("Not supported extension for automatic format support: " +
                                                            extension);
                    }
                    contentList.Add(content);
                }
            }
            finally
            {
                if (iObj != null)
                    iObj.MasterETag = oldMasterETag;
            }

            return contentList.ToArray();
        }