A day in the life...

public class GeekEarth : Earth { }
posts - 31, comments - 9472, trackbacks - 0

Wednesday, May 09, 2012

Ever been to Cornwall?

My wife and I visited Trentinney Farm about 7 years or so ago and it was a really nice place. Well, 7 years on and we are still going there. I even ended up creating their website for them (I know, I am no web guru.)

Anyway, they recently decided to offer a prize draw for a free 1 week holiday any of their cottages. I thought I'd post a link to it in case any of you fancy the idea of spending a week on a beautiful Cornish farm free of charge :)

Here is the link

posted @ Wednesday, May 09, 2012 5:35 PM | Feedback (0) |

Monday, April 30, 2012

C# scripting in .NET

Based on a couple of days of experimenting it seems that I now have C# scripting working in my application to a level that I am satisfied.  Because I couldn’t have achieved this without looking at other people’s examples I thought it was only fair that I share what I now have.  Firstly I want to say that my purpose was to give the user the ability to write procedural code rather than object code.  Although it is still possible for the user to write OOP source code my requirement was to let the user define a function which returned a specific return type.

public interface ICompiledFunction<T> { T Execute(Dictionary<string, object> variables); }

A typical script might look something like this

public decimal string Main() { return "Bob Monkhouse"; }

To create an instance of the ICompiledFunction<T> I use ICompilerService, which is defined like so

public interface ICompilerService { bool Compile<T>( string[] scripts, Dictionary<string, Type> variableDefinitions, IEnumerable<Assembly> referencedAssemblies, out ICompiledFunction<T> function, out string sourceCode, out IEnumerable<string> compilerErrors); }
  1. Scripts: An array of methods which should be combined to make the full script.  Each may include its own “using” statements.
  2. VariableDefinitions: A dictionary of variable names with their types.  These will appear to be global variables available to the combined scripts, but in fact they will be properties defined as part of the wrapper class which is generated automatically.
  3. ReferencedAssemblies: A collection of Assembly, used to ensure that the script has access to any types declared.
  4. Function: The compiled function
  5. SourceCode: The complete source code which is compiled after it has been merged; useful for working out what is wrong with your source code.
  6. CompilerErrors: A collection of compiler errors.

The importance of CompilerService is that the implementation will create a new AppDomain each time you call Compile.  When the CompilerService is dispose it will unload all assemblies.  I will post the code with comments inline

using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Reflection; namespace PeterMorris.Scripting { public class CompilerService : ICompilerService, IDisposable { Stack<AppDomain> SandboxAppDomains = new Stack<AppDomain>(); bool Disposed; public bool Compile<T>( string[] scripts, Dictionary<string, Type> variableDefinitions, IEnumerable<Assembly> referencedAssemblies, out ICompiledFunction<T> function, out string sourceCode, out IEnumerable<string> compilerErrors) { if (scripts == null) throw new ArgumentNullException("Scripts"); //Define app domain settings such as the code base var appDomainSetup = new AppDomainSetup(); string appBase = typeof(CompilerService).Assembly.CodeBase; appBase = new Uri(appBase).LocalPath; appDomainSetup.ApplicationBase = Path.GetDirectoryName(appBase); var evidence = AppDomain.CurrentDomain.Evidence; //Create a uniquely named app domain and push it onto a stack for disposing later string sandboxAppDomainName = "Sandbox" + Guid.NewGuid().ToString().Replace("-", ""); var sandboxAppDomain = AppDomain.CreateDomain(sandboxAppDomainName, evidence, appDomainSetup); SandboxAppDomains.Push(sandboxAppDomain); //Clone the original scripts, we don't want to alter the originals scripts = scripts.ToArray(); //Strip out the using clauses from each script so that they can be combined var usingClauses = StripOutUsingClauses(scripts).OrderBy(x => x).ToList(); //Build the complete source code to be compiled sourceCode = BuildSourceCode(usingClauses, scripts, variableDefinitions); CompiledFunction<T> result; try { //Create an instance of CompiledFunction<T> in the new app domain result = (CompiledFunction<T>)sandboxAppDomain.CreateInstanceAndUnwrap( assemblyName: typeof(CompiledFunction<T>).Assembly.GetName().FullName, typeName: typeof(CompiledFunction<T>).FullName); } catch (Exception unexpectedException) { function = null; compilerErrors = new string[] { unexpectedException.Message }; return false; } //Compile the function //Variable definitions and source code are passed so that we can translate line numbers //in the error message back to the line number of the snippet of source the user //has typed in function = (ICompiledFunction<T>)result; return result.Compile( sourceCode: sourceCode, variableDefinitions: variableDefinitions, referencedAssemblies: referencedAssemblies, compilerErrors: out compilerErrors); } public static HashSet<string> StripOutUsingClauses(string[] scripts) { //Make a hashset of using clauses, to ensure we have no duplicates var usingClauses = new HashSet<string>(); //Ensure that the standard using clauses are present usingClauses.Add("System"); usingClauses.Add("System.Linq"); usingClauses.Add("System.Collections.Generic"); //Create a regex which matches a using statement var regex = new Regex(@"^[\s]*using[\s]+(\S*?)[\s]*;[\s]*$", RegexOptions.IgnoreCase); //Loop through each script for (int scriptIndex = 0; scriptIndex < scripts.Length; scriptIndex++) { string script = scripts[scriptIndex]; if (string.IsNullOrEmpty(script)) continue; var scriptReader = new StringReader(script); var scriptBuilder = new StringBuilder(); while (true) { //Read the next line of the current script, null == end of script string line = scriptReader.ReadLine(); if (line == null) break; //Ignore blank lines if (line.Trim() == "") continue; var match = regex.Match(line); if (match.Captures.Count > 0) { //If there is a match then add the name to the using clause hash set string nameSpace = match.Groups[1].Value; usingClauses.Add(nameSpace); } else { //If no match then the using clauses are finished, add the rest //of the text from the reader into the writer scriptBuilder.AppendLine(line); scriptBuilder.Append(scriptReader.ReadToEnd()); break; } }//Loop through each line of script //Replace the script with text without the using clauses scripts[scriptIndex] = scriptBuilder.ToString(); }//foreach script return usingClauses; } //Builds the full source code with wrapping class string BuildSourceCode(List<string> usingClauses, string[] scripts, IEnumerable<KeyValuePair<string, Type>> variableDefinitions) { if (usingClauses == null) throw new ArgumentNullException("usingClauses"); var scriptBuilder = new StringBuilder(); //Add using clauses foreach (string nameSpace in usingClauses.OrderBy(x => x)) scriptBuilder.AppendFormat("using {0};\r\n", nameSpace); if (usingClauses.Any()) scriptBuilder.AppendLine(); //namespace scriptBuilder.AppendLine("namespace Sandbox"); scriptBuilder.AppendLine("{"); { //Wrapper class scriptBuilder.AppendLine("public class ScriptHolder"); scriptBuilder.AppendLine("{"); { //Add variable definitions as properties within the class BuildProperties(scriptBuilder, variableDefinitions); //Combine all scripts as a single string foreach (string script in scripts) { scriptBuilder.AppendLine(script); scriptBuilder.AppendLine(); } } scriptBuilder.AppendLine("}"); //class } scriptBuilder.AppendLine("}"); //namespace return scriptBuilder.ToString(); } //Add variable definitions as properties within the script void BuildProperties(StringBuilder scriptBuilder, IEnumerable<KeyValuePair<string, Type>> variableDefinitions) { if (variableDefinitions == null) return; foreach (var kvp in variableDefinitions.OrderBy(x => x.Key)) { //ToScriptableString is required to expand generic types back into //proper looking source code string scriptableTypeName = kvp.Value.ToScriptableString(); string variableName = kvp.Key; scriptBuilder.AppendFormat("\tpublic {0} {1};\r\n", scriptableTypeName, variableName); } if (variableDefinitions.Any()) scriptBuilder.AppendLine(); } void Dispose(bool isDisposing) { if (Disposed) return; Disposed = true; if (isDisposing) //Unload all sandbox app domains, //in reverse order - just because I have OCD :-) while (SandboxAppDomains.Count > 0) AppDomain.Unload(SandboxAppDomains.Pop()); } public void Dispose() { Dispose(true); } } }

 

One thing to note here is the use of Type.ToScriptableString() – this is implemented via a helper class.  This is because when you use GetType().FullName on a generic type you get a mangled name which would not compile in a script, so it must be unmangled.

 

using System; using System.Text; namespace PeterMorris.Scripting { public static class TypeHelper { public static string ToScriptableString(this Type type) { if (type == null) return "Void"; //If not a generic type then just return the full name if (!type.IsGenericType) return type.FullName; //Convert a generic type back to a compilable representation var builder = new StringBuilder(); Type genericType = type.GetGenericTypeDefinition(); //Unmangle the name builder.Append(genericType.Name.Remove(genericType.Name.IndexOf("`")) + "<"); string separator = ""; //Include the inner types of the generic type foreach (var genericArgument in type.GetGenericArguments()) { //Recursively call ToScriptableString for the typename, as the inner type //itself may also be a generic type builder.AppendFormat("{0}{1}", separator, genericArgument.ToScriptableString()); separator = ","; } builder.Append(">"); return builder.ToString(); } } }

 

And now the implementing code for CompiledFunction<T>

using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using Microsoft.CSharp; namespace PeterMorris.Scripting { public class CompiledFunction<T> : MarshalByRefObject, ICompiledFunction<T> { bool IsCompiled; Dictionary<string, Type> VariableDefinitions; Dictionary<string, FieldInfo> FieldReferences; object Instance; MethodInfo Method; public CompiledFunction() { VariableDefinitions = new Dictionary<string, Type>(); FieldReferences = new Dictionary<string, FieldInfo>(); } public bool Compile( string sourceCode, Dictionary<string, Type> variableDefinitions, IEnumerable<Assembly> referencedAssemblies, out IEnumerable<string> compilerErrors ) { if (IsCompiled) throw new InvalidOperationException("Already compiled"); IsCompiled = true; if (variableDefinitions != null) this.VariableDefinitions = variableDefinitions; //Get the full path to the executing binary, this is used to find assembly //references which are not in the GAC. Use Uri to convert to a local path. string binaryPath = new Uri(AppDomain.CurrentDomain.BaseDirectory).LocalPath; var fullReferencedAssemblyList = new List<Assembly>(referencedAssemblies); //System.dll fullReferencedAssemblyList.Add(typeof(string).Assembly); //System.Core.dll fullReferencedAssemblyList.Add(typeof(IQueryable).Assembly); //Get assembly names string[] referencedAssemblyNames = fullReferencedAssemblyList .Select(x => x.GetName().Name + ".dll") .Select ( //If the file exists in the binary folder then add it by full path, //otherwise add it without a path so that it is assumed to be in the GAC assemblyName => File.Exists(Path.Combine(binaryPath, assemblyName)) ? Path.Combine(binaryPath, assemblyName) : assemblyName ) .Distinct() .ToArray(); //Set the compiler parameters to compile in memory var compilerparameters = new CompilerParameters { GenerateExecutable = false, GenerateInMemory = true, IncludeDebugInformation = true, TreatWarningsAsErrors = true }; //Add the list of referenced assemblies compilerparameters.ReferencedAssemblies.AddRange(referencedAssemblyNames); //Use C# 3.5 so that we have access to nice features such as LINQ var options = new Dictionary<string, string> { { "CompilerVersion", "v3.5" } }; //Create the compiler var compiler = new CSharpCodeProvider(options); //Compile the source code CompilerResults compilerResults; try { compilerResults = compiler.CompileAssemblyFromSource( options: compilerparameters, sources: new string[] { sourceCode }); } catch (Exception unexpectedException) { compilerErrors = new string[] { unexpectedException.Message }; return false; } var errors = new List<string>(); if (compilerResults.Errors.Count == 0) { //Create an instance of the new class Instance = compilerResults.CompiledAssembly.CreateInstance("Sandbox.ScriptHolder"); //Find the Main() method Method = Instance.GetType().GetMethod("Main"); //Report an error if it is not decalred if (Method == null) errors.Add(string.Format("public {0} Main() has not been defined", typeof(T).Name)); else if (!typeof(T).IsAssignableFrom(Method.ReturnType)) //If the Main() method's return type is not assignable to the expected return type //then report an error errors.Add(string.Format("Expected return type {0} but found {1}", typeof(T).FullName, Method.ReturnType.FullName)); } //Add any compiler errors. FormatError uses the source code and variable definitions //in order to work out the line number relative to the script entered by the user //rather than the position in the composite source code if (compilerResults.Errors != null) compilerResults.Errors.Cast<CompilerError>().ToList() .ForEach(x => errors.Add(x.FormatError(sourceCode, variableDefinitions))); //Create field references (i.e. global variables) if (!errors.Any()) CreateFieldReferences(); compilerErrors = errors.ToArray(); return !errors.Any(); } //Execute the compiled function public T Execute(Dictionary<string, object> variables) { if (!IsCompiled) throw new InvalidOperationException("Function has not been compiled"); //If we have any variable definitions, set the field references to their default values if (VariableDefinitions != null) foreach (var kvp in VariableDefinitions) { string fieldName = kvp.Key; Type fieldType = kvp.Value; object defaultValue = fieldType.IsValueType ? Activator.CreateInstance(fieldType) : null; SetVariable(fieldName, defaultValue); } //Set the variables passed by the caller if (variables != null) foreach (var kvp in variables) SetVariable(kvp.Key, kvp.Value); //Invoke the compiled scripted method return (T)Method.Invoke(Instance, null); } //Populate the FieldReferences list so that it can be built into the composite source code void CreateFieldReferences() { if (VariableDefinitions == null) return; foreach (var kvp in VariableDefinitions) { string fieldName = kvp.Key; Type fieldType = kvp.Value; FieldInfo fieldInfo = Instance.GetType().GetField(fieldName); FieldReferences.Add(fieldName, fieldInfo); } } //Set a field (global variable) value void SetVariable(string variableName, object value) { FieldInfo fieldInfo; if (!FieldReferences.TryGetValue(variableName, out fieldInfo)) throw new ArgumentException(string.Format("Variable {0} has not been defined", variableName)); fieldInfo.SetValue(Instance, value); } } }

And finally the helper class to reverse the line number from the composite script back to the individual script.

using System; using System.CodeDom.Compiler; using System.Collections.Generic; namespace PeterMorris.Scripting { public static class CompilerErrorHelper { public static string FormatError(this CompilerError instance, string script, Dictionary<string, Type> variableDefinitions) { if (instance == null) throw new ArgumentNullException("Instance"); int lineAdjustment = 0; if (!string.IsNullOrEmpty(script)) { var clauses = CompilerService.StripOutUsingClauses(new string[] { script }); if (clauses.Count > 0) lineAdjustment = clauses.Count + 1; } if (variableDefinitions != null && variableDefinitions.Count > 0) lineAdjustment += variableDefinitions.Count + 1; return string.Format("(Approx line: {0}, Col: {1}) {2}", instance.Line - lineAdjustment - 4, // NameSpace \r { \r class \r { instance.Column, instance.ErrorText); } } }

 

This is how it might be used - written in notepad so might not compile Smile

using (var compilerService = new CompilerService()) { string script = @" public string Main() { return ""Hello "" + Name; }"; var scripts = new string[] { script }; var variableDefinitions = new Dictionary<string, Type> { { "Name" typeof(string) } }; string fullSourceCode; IFunction<string> function; IEnumerable<string> compilerErrors; if (!compilerService.Compile( scripts: scripts, variableDefinitions: variableDefinitions, referencedAssemblies : null, function: out function, sourceCode: out fullSourceCode, compilerErrors: out compilerErrors)) { compilerErrors.ToList() .ForEach(x => Console.WriteLine(x)); return; } //Now execute it as many times as you like var variableValues = new Dictionary<string, object> { { "Name", "Bob Monkhouse" } }; Console.WriteLine(function(variableValues)); variableValues["Name"] = "Peter Poppov"; Console.WriteLine(function(variableValues)); }//Calls CompilerService.Dispose, which unloads all temporary app domains

posted @ Monday, April 30, 2012 11:50 AM | Feedback (0) | Filed Under [ Code ]

Friday, April 27, 2012

Free Reflector upgrade

Had an email yesterday telling me that my Reflector license had been updated to include a Visual Studio plugin.  Now pressing F12 not only shows the classes for which I have no source, but reflector then takes over and decompiles the binary back into source – it’s hard to tell what you do or don’t have source for these days Smile

posted @ Friday, April 27, 2012 7:46 AM | Feedback (0) | Filed Under [ Code ]

Thursday, April 26, 2012

DLR is crap!

It took me hours last night to work out why I was getting a null result from the Value property in the following code when accessed via IronPython

public interface ISomeInterface { decimal Value { get; } }

 

How could a non nullable type possibly return a null (or “None”)?  It turns out that when I set my variable using the .NET scripting API

ScriptScope.SetVariable(variableName, (ISomeInterface)value);

The scripting engine still works on the implementing object rather than the interface.  This means that if my object has a method “DoSomethingThatScriptingShouldNotHaveAccessTo()” then scripting has access to it!

In my case this bit me because I had a Nullable<decimal> property called “Value”, and the Value property implemented explicitly for the interface looked like this

decimal ISomeInterface.Value { get { if (this.Value == null) CalculateValue(); return this.Value; } }

 

In this case I expected the script to access the variable’s “Value” property via the interface declaration because that is how I typecast the value when setting the variable; but because the DLR accesses the object itself it was going directly to the class’s property itself and fetching a Nullable<Decimal> which of course was null.

The upshot of this is that if I want any kind of control over which methods my users can script I cannot simply have my objects implement specific interfaces, I have to create a facade class for each class I want API access to which has a subset of members and simply passes them through to the real object.  That is going to be a LOT of work!

The DLR should respect the type of the reference passed, not look up GetType() and work on that.  I need a static scripting language for .NET!

posted @ Thursday, April 26, 2012 8:32 AM | Feedback (0) | Filed Under [ Code IronPython ]

Thursday, February 23, 2012

How many times does a day occur within a date range?

Given two dates I need to know how many times each day of the week occurs within that range, so that I can calculate a value where the factor varies by day.  So I wrote the following code which returns an array of integers, position 0 will tell you how many Sundays there are, position 6 will tell you how many Saturdays, and so on.

int[] CountDays(DateTime firstDate, DateTime lastDate) { var totalDays = lastDate.Date.Subtract(firstDate.Date).TotalDays + 1; var weeks = (int)Math.Floor(totalDays / 7); var result = Enumerable.Repeat<int>(weeks, 7).ToArray(); if (totalDays % 7 != 0) { int firstDayOfWeek = (int)firstDate.DayOfWeek; int lastDayOfWeek = (int)lastDate.DayOfWeek; if (lastDayOfWeek < firstDayOfWeek) lastDayOfWeek += 7; for (int dayOfWeek = firstDayOfWeek; dayOfWeek <= lastDayOfWeek; dayOfWeek++) result[dayOfWeek % 7]++; } return result; }

posted @ Thursday, February 23, 2012 6:05 PM | Feedback (54) | Filed Under [ Code ]

Thursday, November 10, 2011

ASP MVC Silverlight control not appearing–404 not found

Just a quick tip in case you have deployed your ASP MVC app with silverlight controls in it which are not appearing.  I experienced this recently when deploying to IIS6.  The solution is

  1. Open the IIS manager app (Start->Admin->Internet Information Services (IIS) Manager)
  2. Expand the local computer node
  3. Then expand the Websites nodes
  4. Right-click your website and select “Properties”
  5. Click the “Http Headers” tab
  6. At the bottom of the page click the “MIME Types” button
  7. Click the “New” button
  8. For the extension type in .xap
  9. For the MIME Type type in application/x-silverlight-app

All done Smile

posted @ Thursday, November 10, 2011 11:17 AM | Feedback (189) |

Tuesday, April 12, 2011

Identified associations (maps)

I thought I’d share this approach to having named associations.  Let’s say you have an Address class with a Name property on it, your Employee can have a property like this

1 var homeAddress = new Address(.....); 2 homeAddress.Line1 = "192 Blah Street"; 3 homeAddress.Line2 = "etc"; 4 employee.Addresses["Home"] = homeAddress; 5 6 var workAddress = new Address(.....); 7 workAddress.Line1 = "123 Meh Road"; 8 workAddress.Line2 = "etc"; 9 employee.Addresses["Work"] = workAddress;

 

Once you have created your Employee and Address classes add a private association from Employee to Address named something like _Addresses.  Then add the following code to the Employee class.

1 IdentifiedAssociation<string, NamedPostalAddress> postalAddresses; 2 public IdentifiedAssociation<string, NamedPostalAddress> PostalAddresses 3 { 4 get 5 { 6 if (postalAddresses == null) 7 postalAddresses = new IdentifiedAssociation<string, NamedPostalAddress>( 8 //Delete existing value on unlink 9 true, 10 //Get association 11 () => this._Addresses, 12 //Object matches identifier 13 (address, identifier) => string.Compare(identifier, address.Name, true) == 0, 14 //Set object identifier 15 (address, identifier) => address.Name = identifier); 16 return postalAddresses; 17 } 18 } 19

 

You might want to make the setter for the indexed property (Address.Name in this case) to internal so that only business classes can alter it.  The generic code to implement this identified association is as follows

1 public class IdentifiedAssociation<TIdentifier, TAssociated> : IEnumerable<TAssociated> 2 where TAssociated : IEcoObject 3 { 4 readonly bool DeleteOnUnlink; 5 readonly Func<IList<TAssociated>> GetAssociation; 6 readonly Func<TAssociated, TIdentifier, bool> ObjectMatchesIdentifier; 7 readonly Action<TAssociated, TIdentifier> SetIdentifier; 8 9 public IdentifiedAssociation( 10 bool deleteOnUnlink, 11 Func<IList<TAssociated>> getAssociation, 12 Func<TAssociated, TIdentifier, bool> objectMatchesIdentifier, 13 Action<TAssociated, TIdentifier> setIdentifier) 14 { 15 Contract.Requires(getAssociation != null); 16 Contract.Requires(objectMatchesIdentifier != null); 17 Contract.Requires(setIdentifier != null); 18 19 DeleteOnUnlink = deleteOnUnlink; 20 21 GetAssociation = getAssociation; 22 ObjectMatchesIdentifier = objectMatchesIdentifier; 23 SetIdentifier = setIdentifier; 24 } 25 26 public TAssociated this[TIdentifier identifier] 27 { 28 get 29 { 30 return GetAssociation().SingleOrDefault(x => ObjectMatchesIdentifier(x, identifier)); 31 } 32 set 33 { 34 var currentValue = this[identifier]; 35 if (currentValue != null && currentValue.Equals(value)) 36 return; 37 if (currentValue != null && DeleteOnUnlink) 38 currentValue.AsIObject().Delete(); 39 if (value != null) 40 { 41 GetAssociation().Add(value); 42 SetIdentifier(value, identifier); 43 } 44 } 45 } 46 47 public IEnumerator<TAssociated> GetEnumerator() 48 { 49 return GetAssociation().GetEnumerator(); 50 } 51 52 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 53 { 54 return GetEnumerator(); 55 } 56 } 57

posted @ Tuesday, April 12, 2011 10:57 AM | Feedback (401) |

Saturday, April 02, 2011

A WebServer for MonoTouch that also parses posted form data (HttpListenerRequest)

I found a few examples of web servers for MonoTouch but none of them parsed the data sent in a POST request.  I looked around the web and was unable to find any examples of how to achieve this.  So now that I’ve written it myself I’ve decided to share my own implementation.  This includes not only the code for processing the form post data but also for registering request handlers etc.

Here is an example of how you would use the web server

1 public BookUploadViewController() 2 : base("BookUploadViewController", null) 3 { 4 RequestHandler = new DefaultRequestHandler(); 5 var defaultActionHandlerFactory = new DefaultActionHandlerFactory(); 6 RegisterActionHandlers(defaultActionHandlerFactory); 7 RequestHandler.AddActionHandlerFactory(defaultActionHandlerFactory); 8 9 WebServer = new EmbeddedWebServer(RequestHandler); 10 } 11 12 void RegisterActionHandlers(DefaultActionHandlerFactory factory) 13 { 14 factory.RegisterHandler( 15 request => request.RawUrl == "/", 16 request => new IndexActionHandler(request) 17 ); 18 factory.RegisterHandler( 19 request => 20 string.Compare(request.RawUrl, "/Upload", true) == 0 && 21 string.Compare(request.HttpMethod, "POST", true) == 0, 22 request => new UploadActionHandler(request) 23 ); 24 } 25 26 public override void ViewDidAppear(bool animated) 27 { 28 base.ViewDidAppear(animated); 29 StatusLabel.Text = string.Format("Server listening on\r\nhttp://{0}:8080", GetIPAddress ()); 30 WebServer.Start(8080); 31 } 32 33 public override void ViewDidDisappear (bool animated) 34 { 35 base.ViewDidDisappear(animated); 36 WebServer.Stop(); 37 } 38

 

And here are two app specific examples of request handlers

1 class IndexActionHandler : DefaultActionHandler 2 { 3 public IndexActionHandler(HttpListenerRequest request) 4 : base(request) 5 { 6 } 7 8 public override ActionResult Execute() 9 { 10 var result = new HtmlResult(); 11 result.AppendLine("<html>"); 12 result.AppendLine("<body>"); 13 result.AppendLine("<h1>Upload an image</h1>"); 14 result.AppendLine("<form action='/Upload' enctype='multipart/form-data' method='post'>"); 15 result.AppendLine ("<input name='Image' type='file'/><br/>"); 16 result.AppendLine("<input name='Upload' type='submit' text='Upload'/>"); 17 result.AppendLine("</form>"); 18 result.AppendLine("</body>"); 19 result.AppendLine("</html>"); 20 return result; 21 } 22 } 23 24 class UploadActionHandler : DefaultActionHandler 25 { 26 public UploadActionHandler(HttpListenerRequest request) 27 : base(request) 28 { 29 } 30 31 public override ActionResult Execute() 32 { 33 string errorMessage = null; 34 var file = FormData.GetFile("Image"); 35 if (file == null 36 || file.FileData == null 37 || file.FileData.Length == 0 38 || string.IsNullOrEmpty(file.FileName)) 39 errorMessage = "No image uploaded"; 40 41 if (errorMessage == null) 42 ProcessFile(file); 43 44 var result = new HtmlResult(); 45 result.AppendLine("<html>"); 46 result.AppendLine("<body>"); 47 if (errorMessage == null) 48 result.AppendLine("<h1>File uploaded successfully</h1>"); 49 else 50 { 51 result.AppendLine("<h1>Error</h1>"); 52 result.AppendLine("<h2>" + errorMessage + "</h2>"); 53 } 54 result.AppendLine("</body>"); 55 result.AppendLine("</html>"); 56 return result; 57 } 58 59 void ProcessFile(MultiPartStreamFileValue postedFile) 60 { 61 string fileName = "Where to save the file"; 62 using (var fileStream = 63 new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) 64 { 65 fileStream.Write(postedFile.FileData, 0, postedFile.FileData.Length); 66 } 67 } 68 69 } 70

 

 

 

You can download the files here.

posted @ Saturday, April 02, 2011 6:58 PM | Feedback (406) | Filed Under [ Code ]

Monday, March 14, 2011

Aurora on the iPad

I fancied indulging my side interest of making music by buying some music making apps on the iPad. One of which is called "Aurora" from 4pockets.com

Because Apple don't allow trial versions of apps in the Apple store people often release a "lite" version of their app free of charge in order to let potential customers try a limited version of their app before purchasing. I was disappointed to see that this app didn't have such a version, but decided that at a price of £24 it must be pretty good so I took the plunge and bought it.

What a mistake! It is absolutely awful! Even the demo tunes (which should show it off at its best) were terrible! I looked at the Apple store to see if there is any way to get a refund, but the T&C say "all purchases are final", great!

I thought that perhaps the company that developed the app might be good with customer relations and, like myself, only want money from people who actually like my work and think it is worth paying for. Unfortunately it seems as though their philosophy is more like "We want every penny we can get our hands on, even if it means selling over priced junk to unhappy customers."

On the other hand, the very reasonably priced £5 app "MorphWhiz" is great fun!

Mr P Morris to sales
Hi

I bought aurora for my iPad. With a lack of a "lite" version I was unable to evaluate it without purchasing it. Having purchased it I have found I really isn't my cup of tea at all. Is there a way to get a refund?

Pete



sales@4pockets.com to me
Dear Sir,

You will need to talk to Apple

Regards
Bill Muir



Peter Morris to sales
Hi Bill

I've read the terms and conditions and it seems that there is no process for issuing refunds after the application has been downloaded, which is why I wrote to you directly.


Regards

Pete



sales@4pockets.com to me
Dear Sir,

Your transaction is with Apple. We get no data as to who has purchased product via Apple. We only get dates, number of sales, value and country details. Sorry we are unable to help.

Regards
Bill Muir



Peter Morris to sales
Yes, I can see that being potential for fraudulent claims :)

What if I were to send you proof of purchase when I receive my invoice, would that be sufficient?


Pete



sales@4pockets.com to me
Dear Sir,

We can not help you. You are a client of Apple not 4pockets.com

Regards
Bill Muir



Peter Morris to sales
Okay, that's a real cop-out answer. I will take this to mean "Dear sir, we intend to grab every penny we can and to hell with customer satisfaction." I shall ensure my rating on your product accurately reflects your sentiments.

Thanks for taking your share of £23 of my hard earned money for an app not worth more than £5.

Shame on you.


Pete

posted @ Monday, March 14, 2011 6:17 PM | Feedback (397) | Filed Under [ Misc ]

Thursday, February 10, 2011

Additional visualisations in ECO

My model needs to take snapshots of certain data and to do this I have various snapshot classes.

Sometimes you might snapshot an aggregate root such as PurchaseOrder and expect all PurchaseOrderLines to be snapshotted along with it. Sometimes though the Parent/Child relationship is not part of an aggregate root and you want the child snapshot to create the parent snapshot, for example when snapshotting an Address you might want to snapshot the Country.

So when I was looking at my snapshot diagrams it wasn't immediately obviously which behaviour you get. Hans showed me this article: http://theblog.capableobjects.com/2009/12/catching-more-information-in-your-model.html

Now I have added symbols + tagged value definitions to my model and as a consequence the diagrams show me visually which way the snapshots work. Brilliant!

posted @ Thursday, February 10, 2011 11:01 AM | Feedback (346) | Filed Under [ Code ]

Powered by: