{"action":"opened","issue":{"url":"https://api.github.com/repos/JimBobSquarePants/ImageProcessor/issues/164","labels_url":"https://api.github.com/repos/JimBobSquarePants/ImageProcessor/issues/164/labels{/name}","comments_url":"https://api.github.com/repos/JimBobSquarePants/ImageProcessor/issues/164/comments","events_url":"https://api.github.com/repos/JimBobSquarePants/ImageProcessor/issues/164/events","html_url":"https://github.com/JimBobSquarePants/ImageProcessor/issues/164","id":73912405,"number":164,"title":"invalidoperationexception on autoprocess","user":{"login":"Deidarius","id":7248302,"avatar_url":"https://avatars.githubusercontent.com/u/7248302?v=3","gravatar_id":"","url":"https://api.github.com/users/Deidarius","html_url":"https://github.com/Deidarius","followers_url":"https://api.github.com/users/Deidarius/followers","following_url":"https://api.github.com/users/Deidarius/following{/other_user}","gists_url":"https://api.github.com/users/Deidarius/gists{/gist_id}","starred_url":"https://api.github.com/users/Deidarius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Deidarius/subscriptions","organizations_url":"https://api.github.com/users/Deidarius/orgs","repos_url":"https://api.github.com/users/Deidarius/repos","events_url":"https://api.github.com/users/Deidarius/events{/privacy}","received_events_url":"https://api.github.com/users/Deidarius/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-05-07T09:20:37Z","updated_at":"2015-05-07T09:20:37Z","closed_at":null,"body":"/* This will run as a webjob. It will update all images on the storage, to ensure that they all have an image copy in of the presets. There will be a raw image, which the others are manipulated from. It all works fine, until it reaches the imagefactory.autoprocess. It throws the exception: InvalidOperationException method cannot be called during the application's pre-start initialization phase, and I don't know why */\r\n\r\nstatic void Main(string[] args)\r\n        {\r\n            JobHost host = new JobHost();\r\n            \r\n            host.Call(typeof(Program).GetMethod(\"UpdatePresetsBlob\"));\r\n        }\r\n[NoAutomaticTrigger]\r\n        public static void UpdatePresetsBlob()\r\n        {\r\n            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings[\"AzureWebJobsStorage\"].ConnectionString);\r\n\r\n            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();\r\n\r\n            var containerName = ConfigurationManager.AppSettings[\"azureStorageContainer\"];\r\n            CloudBlobContainer container = blobClient.GetContainerReference(containerName);\r\n\r\n            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings[\"presetFetchUrl\"]);\r\n\r\n            request.Accept = \"application/json\";\r\n\r\n            var response = request.GetResponse();\r\n            var stream = response.GetResponseStream();\r\n\r\n            Encoding encode = System.Text.Encoding.GetEncoding(\"utf-8\");\r\n            // Pipes the stream to a higher level stream reader with the required encoding format. \r\n            StreamReader readStream = new StreamReader(stream, encode);\r\n            Char[] read = new Char[256];\r\n            // Reads 256 characters at a time.     \r\n            int count = readStream.Read(read, 0, 256);\r\n            var stringBuilder = new StringBuilder();\r\n\r\n            while (count > 0)\r\n            {\r\n                // Dumps the 256 characters on a string and displays the string to the console.\r\n                String str = new String(read, 0, count);\r\n                stringBuilder.Append(str);\r\n                count = readStream.Read(read, 0, 256);\r\n            }\r\n            // Releases the resources of the response.\r\n            response.Close();\r\n            // Releases the resources of the Stream.\r\n            readStream.Close();\r\n            var jarray = JArray.Parse(stringBuilder.ToString());\r\n\r\n            XDocument doc = new XDocument();\r\n\r\n            if (container.GetBlockBlobReference(\"Images.xml\").Exists())\r\n            {\r\n                doc = XDocument.Load(container.GetBlockBlobReference(\"Images.xml\").Uri.ToString());\r\n            }\r\n\r\n            var elements = doc.Root.Elements();\r\n\r\n            XDocument presetDoc = new XDocument();\r\n\r\n            foreach (var element in elements)\r\n            {\r\n                var formatRegex = new Regex(@\"(format=(?<format>\\w+?)($|\\&))\");\r\n                using (ImageFactory imageFactory = new ImageFactory())\r\n                {\r\n                presetDoc = XDocument.Load(container.GetBlockBlobReference(element.Attribute(\"Folder\").Value + \"/Presets.xml\").Uri.ToString());\r\n                var presetList = new List<string>();\r\n\r\n                var folder = element.Attribute(\"Folder\").Value;\r\n                var extension = element.Attribute(\"Extension\").Value;\r\n\r\n                using (var presetStream = new MemoryStream())\r\n                {\r\n                    var rawBlob = container.GetBlockBlobReference(folder + \"/Raw\" + extension);\r\n                    rawBlob.DownloadToStream(presetStream);\r\n                    presetStream.Seek(0, SeekOrigin.Begin);\r\n\r\n                foreach (var jtoken in jarray)\r\n                {\r\n                    var presetName = jtoken[\"Name\"].ToString();\r\n                    var presetValue = jtoken[\"Value\"].ToString();\r\n\r\n                    var imageElement = (from e in presetDoc.Root.Elements(\"Preset\")\r\n                                        where e.Attribute(\"Name\").Value == presetName\r\n                                        select e).FirstOrDefault();\r\n                    //if (imageElement == null)\r\n                    //{\r\n                                var format = extension;\r\n\r\n                                if (presetValue.ToLower().Contains(\"format\"))\r\n                                {\r\n                                    var match = formatRegex.Match(presetValue);\r\n                                    if (match.Success)\r\n                                    {\r\n                                        format = \".\" + match.Groups[\"format\"].Value;\r\n                                    }\r\n                                }\r\n\r\n                                using (var outStream = new MemoryStream())\r\n                                {\r\n                                    imageFactory.Load(presetStream)\r\n                                                .AutoProcess(presetValue)\r\n                                                .Save(outStream);\r\n\r\n                                    outStream.Seek(0, SeekOrigin.Begin);\r\n\r\n                                    var presetPath = folder + presetName + format;\r\n                                    var image = container.GetBlockBlobReference(presetName);\r\n                                    image.UploadFromStream(outStream);\r\n                                }\r\n                                \r\n                                imageElement = new XElement(\"Preset\");\r\n                                imageElement.SetAttributeValue(\"Name\", presetName);\r\n                                imageElement.SetAttributeValue(\"Format\", format);\r\n                                presetDoc.Root.Add(imageElement);\r\n                            \r\n                            presetList.Add(presetValue);\r\n                        }\r\n                    foreach (var xelement in presetDoc.Elements().Elements())\r\n                    {\r\n\r\n                        if (!presetList.Contains(xelement.Attribute(\"Name\").Value))\r\n                        {\r\n                            xelement.Remove();\r\n                        }\r\n                    }\r\n                        using (var ms = new MemoryStream())\r\n                        {\r\n                            presetDoc.Save(ms);\r\n                            ms.Seek(0, SeekOrigin.Begin);\r\n                            container.GetBlockBlobReference(folder + /Presets.xml\").UploadFromStream(ms);\r\n                        }\r\n                    }\r\n                  }\r\n           }\r\n  }"}}