Thursday, 24 October 2013

Fixing broken music downloads in iTunes 11.1.1.11

There are few things more infuriating than eagerly purchasing the latest album by your favourite band from iTunes, transferring to your device of choice and heading off for a trip only to find one or more of the tracks have been truncated due to an incomplete download. This seems to happen to me a lot but maybe I’m just unlucky.

Anyway, how to fix the problem? Well firstly, there doesn’t seem to be an obvious way of doing this in iTunes, at least not in version 11.1.1.11. In the past you could visit the store, locate the track in question and choose to purchase the track again. Having figured out that you had already purchased the item iTunes would respond by asking if you would like to download again. You’d just confirm and all would be well.

This doesn’t seem to be possible in iTunes 11.1.1.11 because if you go to the store all you can do is play the track. No good if the track is broken! It won’t download again and the track will remain broken.

 

itunes003

Figure 1 - No option to download or purchase again in the store.

 

The solution

Now that all your music is in the cloud it seems reasonable that you should be able to get at it again. This is how I do it.

Step 1. Find the affected file on your disk drive and delete it.

Step 2. Start iTunes, navigate to the affected track and try to play it. iTunes will respond by saying it can’t find the track and invites you to locate the file. In the image below the track Blood Drive was truncated so I have deleted it from my disk drive.

 

itunes001

Figure 2 – Prompted to locate the missing file.

 

Step 3. Cancel the dialog and the cloud icon will reappear to the right of the track.

 

itunes002

Figure 3 – The cloud icon should now be available.

 

Step 4. Click the cloud icon to download the file again.

Sunday, 2 June 2013

Andy’s list of JavaScript frameworks

Too many JavaScript frameworks. Too little time. This is a list of frameworks for me to keep track. It’s not meant to be exhaustive but contains the frameworks I’m coming across. For a fuller list why not try www.jsdb.io.

Framework Description URL
H5F A JavaScript library that allows you to use the HTML5 Forms chapters new field input types, attributes and constraint validation API in non-supporting browsers. https://github.com/ryanseddon/H5F
Angular JS From Google. Somewhat similar to Knockout. http://angularjs.org/
Backbone Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface. http://backbonejs.org/
Bootstrap Sleek, intuitive, and powerful front-end framework for faster and easier web development.

Not just JavaScript. Includes HTML and CSS.
http://twitter.github.io/bootstrap
Breeze Breeze is a JavaScript library that helps you manage data in rich client applications. If you store data in a database, query and save those data as complex object graphs, and share these graphs across multiple screens of your JavaScript client, Breeze is for you. http://www.breezejs.com/
Durandal Durandal is a cross-device, cross-platform client framework written in JavaScript and designed to make Single Page Applications (SPAs) easy to create and maintain. http://durandaljs.com/
Font Awesome The iconic font designed for Bootstrap.

Font Awesome gives you scalable vector icons that can instantly be customized — size, color, drop shadow, and anything that can be done with the power of CSS.
http://fortawesome.github.io/Font-Awesome/
jQuery Mobile A unified, HTML5-based user interface system for all popular mobile device platforms, built on the rock-solid jQuery and jQuery UI foundation. Its lightweight code is built with progressive enhancement, and has a flexible, easily themeable design. http://jquerymobile.com/
jQueryUI jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. http://jqueryui.com/
jsRender jsrender - Next-generation jQuery Templates, optimized for high-performance pure string-based rendering, without DOM or jQuery dependency. https://github.com/BorisMoore/jsrender
Knockout Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model. MVVM! http://knockoutjs.com/
Moment A 5.5kb javascript date library for parsing, validating, manipulating, and formatting dates. http://momentjs.com/
RequireJS RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code. http://requirejs.org/
Toastr Simple javascript toast notifications. Contribute to toastr development by creating an account on GitHub. https://github.com/CodeSeven/toastr
Sammy Sammy.js is a tiny JavaScript framework developed to ease the pain and provide a basic structure for developing JavaScript applications. Routing! http://sammyjs.org/
Underscore Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects. It's the tie to go along with jQuery's tux, and Backbone.js's suspenders. http://underscorejs.org/

Wednesday, 29 May 2013

Problem running a Windows service with Topshelf and Spring.Net

Problem

I had written an application using Spring.Net for dependency injection - and some of the other features it provides - and Topshelf. The application could then be written as a console application and then installed and run as a Windows service using Topshelf’s handy ‘install’ command line parameter.
I was using XML files to configure Spring.Net. This turned out to be significant.

The application worked sweet as a nut as a console application and installed successfully as a Windows service. However, when I tried to run the Windows service using net start all I got was “The service is not responding to the control function”.




Solution

In the app.config file I had a spring configuration section that referenced external files for the spring.context:

<spring>
    <context>
      <resource uri="file://Config/SpringContext.xml" />
      <resource uri="file://Config/SpringDataAccess.xml" />
      <resource uri="file://Config/SpringVelocity.xml" />
    </context>
    <parsers>
      <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
      <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />
      <parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop" />
    </parsers>
</spring>

The XML configuration files were set to “Copy always” and had been copied into the application directory correctly.

Poking around in the Event Viewer I spotted an interesting error log message. Essentially it said “Exception: Error creating context 'spring.root': Could not find file 'C:\Windows\system32\Config\SpringContext.xml'.”

That was weird because the path (C:\Windows\system32) is not where I had put the application but clearly it was where the Windows service was being run from.

A quick solution was to reconfigure the application to use embedded resources for the configuration files:

<spring>
    <context>
      <resource uri="assembly://Assembly.Name.Here/Namespace.Here/Config.SpringContext.xml" />
      <resource uri="assembly://Assembly.Name.Here/Namespace.Here/Config.SpringDataAccess.xml" />
      <resource uri="assembly://Assembly.Name.Here/Namespace.Here/Config.SpringVelocity.xml" />
    </context>
    <parsers>
      <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
      <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />
      <parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop" />
    </parsers>
</spring>

The service now started correctly.

Sunday, 5 May 2013

JavaScript functions

JavaScript functions can be declared in a number of ways.

Basic declaration

function write(message) {
    var div = document.getElementById('message');
    var para = document.createElement("p");
    var node = document.createTextNode(message);
    para.appendChild(node);
    div.appendChild(para);
}

In the example above the function called write is declared and can be called later in JavaScript (e.g. write("My message here")).

Assigned function

Functions can be assigned to variables. There are basically 2 ways to do this: 1) name the function and assign it to a variable, 2) don’t name the function and assign it to a variable.

var writeFunc = function write(message) {
    var div = document.getElementById('message');
    var para = document.createElement("p");
    var node = document.createTextNode(message);
    para.appendChild(node);

    div.appendChild(para);
};

In the example above the function is named (write) and assigned to a variable (writeFunc). The function must now be called via the variable name (e.g. writeFunc("My message here")). Note the semi-colon at after the last curly brace.

var writeFunc = function (message) {
    var div = document.getElementById('message');
    var para = document.createElement("p");
    var node = document.createTextNode(message);
    para.appendChild(node);

    div.appendChild(para);
};

In the example above the function is not named (it’s an anonymous function) but is still assigned to a variable as before.

Anonymous function immediately invoked

(function (message) {
    var div = document.getElementById('message');
    var para = document.createElement("p");
    var node = document.createTextNode(message);
    para.appendChild(node);

    div.appendChild(para);
})("This is a message.");

In the example above the function is anonymous. However, because it is wrapped in brackets and has an argument passed in (see the last line) it will be immediately invoked.

Function overloading

Function overloading doesn’t work the same way in JavaScript as it does in C#. Declaring functions with the same names but different arguments doesn’t result in overloaded functions, rather the last function to be declared overwrites the others. Note that if you call a function and pass in too many arguments any unnecessary arguments are simply ignored.

Don’t forget that object parameters are passed by reference and primitive types by value.

Use an object to hold arbitrary values

One option is to add an object parameter as the last argument to a function. This object can be used as a bag into which you can put whatever parameters you want.

function functionTest(param1, param2, options) {
    write("param1: " + param1);
    write("param2: " + param2);
    write("options.opt1: " + options.opt1);
    write("options.opt2: " + options.opt2);
}

window.onload = function () {
    functionTest("This is param1", "This is param2", {opt1:"This is option1", opt2:"This is option2"});
}

Use the ‘arguments’ object

Another option is to use the arguments object. This is described as:

“An Array-like object corresponding to the arguments passed to a function.” [1]

function functionTest() {
    write("arguments[0]: " + arguments[0]);
    write("arguments[1]: " + arguments[1]);
    write("arguments[2]: " + arguments[2]);
}

window.onload = function () {
    functionTest("This is arguments[0]", "This is arguments[1]", "This is arguments[2]");
}

 

References

[1] https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments

Thursday, 18 April 2013

Fixing WSDL addresses in WCF 3.5 hosted on IIS 7 and using SSL

Problem

I needed to host a WCF 3.5 web service on IIS 7 (running on Windows Server 2008) using SSL. In fact the service was configured to use wsHttpBinding with TransportWithMessageCredential. The service was running on a public facing web server with a registered domain name.

The problem was usual one; having navigated to .svc file in a browser the service description page showed the WSDL URL to be using the machine name, not the domain name. When viewing the generated WSDL the service location was also using the machine name.

Solution

The web site bindings had been set up in IIS for HTTP and HTTPS. It appears that when the HTTPS binding is setup the host name is not set. You cannot set this using the Internet Information Services (IIS) Manager so you have to do it by other means.

 

image

To add the host name to the HTTPS binding do the following:

  1. Open the applicationHost file located in C:\Windows\System32\inetsrv\config.
  2. Locate the section for the website in question (you can see the web site name in the Internet Information Services (IIS) Manager).
  3. Find the https binding and change the bindingInformation by adding the domain name after “:443:”.
  4. Save the applicationHost file and restart IIS.

 

Now when you view the service .svc file in a browser and the generated WSDL the domain name will appear in place of the machine name.

Thursday, 18 April 2013,

Wednesday, 30 January 2013

Single file WSDL generation in WCF

Sometimes it is convenient or even necessary (e.g. some interoperability scenarios) to have WCF generate a single WSDL file without references to external schema. Luckily, there are some 3rd party libraries available to help out: WCFExtras and WCFExtrasPlus. These are both available from NuGet. WCFExtrasPlus is based on WCFExtras and is slightly more up-to-date. Note that there seems to be 2 versions of WCFExtras as well. At the time of writing NuGet gives you WCFExtras 2.0.

Steps

My steps here are based on an IIS hosted WCF service.

Firstly, use NuGet to reference WCFExtras in the WCF host project.

Secondly, edit your Web.config file to include the following behaviour extension:

<system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="wsdlExtensions" type="WCFExtrasPlus.Wsdl.WsdlExtensionsConfig, WCFExtrasPlus, Version=2.3.0.2, Culture=neutral, PublicKeyToken=f8633fc5451b43fc"/>
      </behaviorExtensions>
    </extensions>
    ... snip ...
</system.serviceModel>

If the version is different you can use something like Telerik JustDecompile to get the correct information to use in the type attribute of the add element.

Next create an endpoint behaviour referencing the new extensions:

<system.serviceModel>
    ... snip ...
    <behaviors>
      <endpointBehaviors>
        <behavior name="SingleFileBehaviour">
          <wsdlExtensions singleFile="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    ... snip ...
</system.serviceModel>

You now need to update your endpoint definitions to use the new behaviour, something like this:

<endpoint address="" 
binding="wsHttpBinding"
bindingNamespace="http://schemas.example.com/Example"
bindingConfiguration="wsHttpBindingConfiguration"
behaviorConfiguration="SingleFileBehaviour"
contract="Example.ServiceContracts.IExampleService" />

Fixing problems

Most execution problems seem to stem from namespacing issues. In WCFExtras there is a class called WCFExtras.Wsdl.SingleFileExporter that does the work. The first thing it does is check that the number of generated WSDL documents is not greater that 1 and this will be the case is your namespaces are wrongly defined. Here’s my checklist to avoid problems:

1. If you define service contracts in a separate interface ensure the ServiceContract attribute has a namespace.

[ServiceContract(Name = "ExampleService", "Namespace = http://schemas.example.com/Example")]

2. In the service class add a ServiceBehavior attribute also with a namespace (failure to do this will result in the service being given the http://tempuri.org namespace). The namespace must match that of the service contract.

[ServiceBehavior(Namespace = "http://schemas.example.com/Example")]

3. Check the binding namespace on the endpoint configuration in Web.config (see above).

4. Check the namespaces on any DataContract or MessageContract attributes.

5. If you are building at x86 you might want to set the project output path to be bin\ to avoid having multiple folders with different copies of the dlls. 

Wednesday, 30 January 2013

Saturday, 12 January 2013

ReSharper plugins folder on Windows 7

Here’s a quick reminder that applies to ReSharper 5.1 in Visual Studio 2010 on Windows 7.

ReSharper plugins on Windows 7 live in the following location:

C:\Users\<User Name Here>\AppData\Roaming\JetBrains\ReSharper\v5.1\vs10.0\Plugins\