BBC identifies a growing concern over inappropriate Internet content in 2013!

Just spotted this article in October 2013 saying that Google’s move to add parental controls to chrome “… comes after growing concerns over inappropriate content children can be exposed to online.”

I believe the concerns have been there for a number of years and that Google is actually very late in adding such controls that other browsers have had for a long time (e.g. Internet Explorer since at least 2007, long before the BBC appears to have had any concerns over Internet content).

The article implies Google are simply reacting to changing times rather than playing catch-up.

http://www.bbc.co.uk/news/technology-24635752

SAML vs OAuth

Excellent article by Zach Dennis on the differences between SAML and OAuth2.

http://www.mutuallyhuman.com/blog/2013/05/09/choosing-an-sso-strategy-saml-vs-oauth2/

In a nutshell:

  • SAML 2.0 older specification not good for mobile and non-web devices but more established
  • OAuth2 new kid on the block with similar flows written in the age of mobile devices and native clients but not so tightly specified leading to some incompatibilities

Must create DependencySource on same Thread as the DependencyObject.

Stumped me for a bit because I wasn’t expecting this as a result of the change I made. I was creating a colour in my ViewModel like this:

private readonly Brush[] colours = new[] { Brushes.Black, Brushes.DarkBlue };

But then wanted to use some more funky colours so changed it to:

private readonly Brush[] colours = new[] { new SolidColorBrush(Color.FromRgb(0x04, 0x81, 0x9E)), new SolidColorBrush(Color.FromRgb(0xEC, 0xFC, 0x00)) };

Because I was no longer referencing the static brushes and instead creating them when this class is instantiated (which happened to be on a non UI thread), this caused the above ArgumentException (which had a stack trace and values that really didn’t point to where the error actually occured).

Fixing it meant doing it properly 🙂 I created the brushes in my View and used Style triggers to choose the appropriate brush based on a value in my ViewModel.

“value does not fall within the expected range” XAML exception using DataTemplateSelector

For the record I made a stupid error in Xaml and got bashed with the usual unhelpful error message (no stack trace, no line numbers just the message you see above).

I’d created a custom DataTemplateSelector but rather than set the ItemTemplateSelector property on the GridView, I’d put it inside the . Nothing complained but the app threw a runtime exception as above.

Instead putting the DataTemplateSelector as a static resource and setting ItemTemplateSelector=”{StaticResource myTemplateSelector}” resolved the problem.

Socket not working after WP7 tombstone resume

I’ve written an app that needs to connect to a WiFi router and broadcasts UDP packets. I was finding that after a tombstone and resume, the packets were no longer seen on the network using Wireshark. No errors were returned, the app was getting Success for the send and connect but it clearly wasn’t working.

It turns out that when the app resumed and the socket was reconnected, instead of defaulting to Wifi, it defaulted to MobileBroadbandGsm so it was sending the packets successfully but on the wrong network interface.

The solution turned out to be calling:

socket.SetNetworkRequirement(NetworkSelectionCharacteristics.NonCellular);

Thanks to Pat Long for his assistance.

Control your lights from Windows Phone 7 using LightwaveRF

Using a Nokia Lumia or one of the other windows phones you can now control your lights and other appliances (UK only I think).

The system is using LightwaveRF no PC required!

Step 1: Buy one of these LightwaveRF Wifi things and plug it into your Wifi router

http://amzn.to/GCkwtR

LightwaveRF Wifi-link for Remote Iphone or Android Mobile Phone Lighting Control

Step 2: Swap your existing light switch for one of these (variety of colours available):

http://amzn.to/HDRQ4h

LightwaveRF Remote 1 Gang Dimmer 250w Black Chrome Light Switch

Step 3: Get the unofficial Home Remote RF app from the market place

http://www.windowsphone.com/en-GB/apps/41666473-fb72-4dd8-a2b0-5d6056acd846

1 of 5

I thought pairing the devices was pretty easy (much easier than X10 if you’ve used that and more reliable). LightwaveRF also has the advantage of remembering dim levels.

Read an in-depth review at: http://www.automatedhome.co.uk/Reviews/LightwaveRF-Home-Automation-System-In-Depth-Review.html

Although the app featured in the review is for the official iPhone app not the unofficial WP7 one.

Unit testing DelegatingHandler

The DelegatingHandler in the MVC 4 beta Web API is not particularly easy to test due to the inheritance dependency on DelegatingHandler and the use of overridden protected methods. All is not lost though, by using the following pattern you can achieve testability of the logic with subtle changes to your existing DelegatingHandler and without breaking the Russian Doll pattern.

Create a delegate as follows:

public delegate Task<HttpResponseMessage> BaseSendAsyncDelegate(HttpRequestMessage request, CancellationToken cancellationToken);

Changing your DelegatingHandler to the following (note you’ll also need to add InternalsVisibleTo attribute for your unit test to see the handler):

public class MyTestableDelegatingHandler : DelegatingHandler
{
    internal Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken, BaseSendAsyncDelegate baseSendAsync)

{

        // Your before logic
        // …
       
return baseSendAsync(request, cancellationToken).ContinueWith(
            task =>
                {
                    HttpResponseMessage response = task.Result;
                       
                    // Your after logic
                   
return response;
            });
    }       
      
    protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        // Don’t put anything else in this method
       
return this.SendAsync(request, cancellationToken, (r, c) => base.SendAsync(r, c));
    }
}

Tests

Now the logic can be called without the overhead of the DelegatingHandler as follows:

[TestMethod]
public void MyTest()
{
    MyTestableDelegatingHandler logic = new MyTestableDelegatingHandler();
    HttpRequestMessage requestMessage = new HttpRequestMessage();

    Task<HttpResponseMessage> result = logic.SendAsync(
        requestMessage,
        new CancellationToken(false),
        (rm, ct) =>
            {

                // Do some Asserts in here for any “Before” logic

                // This simulates what the inner handler (or ultimately the controller) would return
                var task = new Task<HttpResponseMessage>(() => new HttpResponseMessage(HttpStatusCode.OK));
                task.Start();
                return task;
            });

    HttpResponseMessage resultMessage = result.Result;
    var status = resultMessage.StatusCode;
   
    // Do Asserts here for any “After” logic
}

MVC4 Web API–REST Sub-resources

To create sub-resources (e.g. /books/1/authors) in the Web API that shipped with MVC beta 4, you can create a “SubResourceControllerFactory” to mangle the controller name to include a sub controller as follows:

public class SubResourceControllerFactory : DefaultHttpControllerFactory
{
    public SubResourceControllerFactory(HttpConfiguration configuration) : base(configuration)
    {
    }

    public override System.Web.Http.Controllers.IHttpController CreateController(System.Web.Http.Controllers.HttpControllerContext controllerContext, string controllerName)
    {
        if (controllerContext.RouteData.Values.ContainsKey("subcontroller"))
        {
            return base.CreateController(controllerContext, controllerName + controllerContext.RouteData.Values["subcontroller"]);
        }

        return base.CreateController(controllerContext, controllerName);
    }
}

This factory can be instantiated using a custom IDependencyResolver that’s looking for a type of IHttpControllerFactory, for example in Global.asax.cs (Application_Start):

GlobalConfiguration.Configuration.ServiceResolver.SetResolver((type) =>
    {
        if (type == typeof(IHttpControllerFactory))
        {
            return new SubResourceControllerFactory();
        }
    });

Finally you create a route similar to:

routeCollection.MapHttpRoute("SubController", "api/{controller}/{id}/{subcontroller}/{subId}", new { subId = RouteParameter.Optional })