# Thursday, June 13, 2013
The jQuery 2 branch does not support IE 6,7 or 8. Unless the site is exclusively targeted at mobile, or you have a very small and up-to-date audience, everyone should still use the jQuery 1.9+ branch.

Nuget insists that you want to update to jQuery 2.x

Doh.

The package should not have been updated from 1.x to 2.x. There should have been a separate package for jQuery 2, so .net websites continue to update on the 1.x branch.

There is a workaround.

You must manually change the packages.config in the project. Add a range of allowed versions:
 
<package id="jQuery" version="1.10.1" targetFramework="net45" allowedVersions="[1.7.1,2)" />


Square bracket "[" is "greater or equal to". So versions greater than 1.7.1 here...

Closing round bracket ")" is less than (not inclusive). So versions up to but not including 2.


posted on Thursday, June 13, 2013 9:53:11 AM (Romance Daylight Time, UTC+02:00)  #    Comments [0]
# Friday, May 31, 2013
WebAPI got updated 30 May 2013.

if you do a NuGet update you'll probably not be able to build afterwards.

WebAPI depends on WebAPI.WebHost
which depends on WebAPI.Core
which depends on Web.Client
which depends on Microsoft.Net.Http
which now depends on Microsoft.Bcl and Microsoft.Bcl.Build.

Microsoft.Bcl is a portability library which allows .Net 4 etc to use .Net 4.5 types. Apparently it has no effect on 4.5

But it (or at least the Bcl.Build) has an ugly bug when you try to build
Error    12    The "EnsureBindingRedirects" task failed unexpectedly.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Roxel.BuildTasks.EnsureBindingRedirects.MergeBindingRedirectsFromElements(IEnumerable`1 dependentAssemblies)
   at Roxel.BuildTasks.EnsureBindingRedirects.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__20.MoveNext()    Ems.WebApp


The fix is to add culture="neutral" to any binding redirects that are missing them. In the default MVC template they are missing for some, and you almost certainly haven't changed them.
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
Do a Rebuild (rather than a build) to ensure everything's loaded.

Hopefully there will be a update pretty soon.



posted on Friday, May 31, 2013 9:04:42 AM (Romance Daylight Time, UTC+02:00)  #    Comments [0]
# Friday, November 30, 2012

At work I can happily connect to my Azure-hosted Team Foundation Service. But I couldn't do it from home. It says it is looking up identity providers, but the live.com logon screen never shows up. I just see the dreaded TFS31003 error ("Either you have not entered the necessary credentials or your user account does not have permission to connect to Team Foundation Server ").

My home machines are Windows 8 and linked to my personal LiveIDs, not my work logon. Windows 8 likes to connect to lots of Skydrive and lots of other services, storing all those credentials. And Visual Studio picks those rather than allow me to add a new one. Deleting entries in the Windows credentials store didn't work.

How can I force Visual Studio to select the right logon?

In Visual Studio, View>Other Windows>Web Browser

In the browser, go to live.com, and log on.

On mine it automatically logged on with another of my logons, so I signed off, and then signed back in with the correct one.

Now when I tried to connect to the TFS service address, it works.

posted on Friday, November 30, 2012 6:07:20 PM (Romance Standard Time, UTC+01:00)  #    Comments [0]
# Saturday, June 30, 2012

MsTest, Nunit, MbUnit etc

Unit testing is now available in VS 2012 Express. In the paid-for SKUs, you can use other unit test frameworks, not just MSTest. For instance, NUnit, MbUnit and so on.

First, install an adapter via Extensions.

NunitTestAdapter

Then install the framework via Nuget- and start writing tests!

NunitTests

Test windows

TestExplorerThere's now only one, the Test Explorer. Most people only ever used the Test Results before, and Test Lists was unusable. It has a thin red or green bar (at last!), and simple splitting into Failed and Passed tests (now with timings).

There's a search with filters ("FullName: Domain.Tests.MyTest"). For solutions with large numbers of tests this might be awkward- I'd like to see more customization of the result tree (by project/folder or namespace), and more filters.

The results at the bottom of the window are summarized, but you can still click through to the test and stack.

There's a button to make every build (Ctl-Shift-B) also run the tests - "Run Tests After Build". Builds and test are run in the  background so it doesn't stop you coding (well, my underpowered 32bit laptop is less responsive, but it's vastly better than previous versions of VS). Not so good if you include some integration tests, but nice nonetheless.

RunTestsAfterBuild

Features

Unit tests support async tests. And code coverage (in Premium and Ultimate only) is much easier- no .testsettings, no having to select the dlls, just right click the tests (or add /enablecodecoverage on the vstest.console.exe command line).

CodeCoverage

You can't test private methods anymore (didn't use that anyway). You can't Generate Unit Tests from a method either. In VS2010 I used this a fair amount- I got a test project with the correct references, and the correct namespaces as well, even if the stub test it created normally had to be deleted and rewritten straight away. I'll miss that.

Key Mappings

Of course, they broke something. I always use Ctl+R-T to run the current test. Well, I hold Ctl and type R then T. Which just doesn't work in VS2012. You have to hold Ctl and type R, then release Ctl and press T. The combination I used, which turns out to be Ctl R,Ctl T, isn't mapped in VS2012. You can remap it manually. Very annoying.

CtrlRT

Microsoft.Fakes: Stubs and Shims

These are mocking classes, similar to Moq, NMock and RhinoMocks, and derived from the Pex Moles project. It's simpler than Moq: there's no "Setup(" or "Verify". It's also VS Ultimate only (not in Premium or Professional). Personally I much prefer to do simple manual stubs (implement an interface in the test project) than full mocking. Full mocks are powerful but you end up with loads of code setting up the tests (tight coupling), and they make it easy to add too many dependencies (just because you can test it doesn't mean you can forget all about the SRP).

Microsoft.Fakes isn't going to replace mocking frameworks (there's no behaviour verification). The shimming is very powerful (and dangerous), similar to TypeMock Isolator and other expensive tools.

To add Fakes, right click the references.

AddFakesAssembly

You'll get a reference to Microsoft.QualityTools.Testing.Fakes and a project folder called Fakes with an xml file in it. For example, in a web application, you'll probably want to fake System.Web so you handle all the HttpContext/ Request stuff.

Stubs are simple (MSDN). Let's stub our input object and initialize it's value. All properties are automatically prepared to return the defaults (0s or nulls). The stub method (in the .Net framework or a local assembly) has a "Stub" prefix.

        [Test]
        public void Test20()
        {
            //arrange
            var entity = new ClassLibrary1.Tasks.Fakes.StubEntity();
            entity.Value = 20;
            var processor = new Processor();

            //act
            var result = processor.Execute(entity);

            //assert
            Assert.That(result.Value, Is.EqualTo(20));
        }

Shims are more difficult and powerful (MSDN).

Here's a method we want to test:

    public class FileReader
    {
        public string ReadAllText(string path)
        {
            return System.IO.File.ReadAllText(path);
        }

Here's how we can test it. Here we have to fake the System reference (which includes mscorlib). Note for shims, we have to have a ShimsContext. The prefix is "Shim" and methods and properties are prepared with lambda functions.

        [Test]
        public void ReadAllTextTest()
        {
            //arrange
            string result;
            var reader = new FileReader();
            using (Microsoft.QualityTools.Testing.Fakes.
                ShimsContext.Create())
            {
                System.IO.Fakes.ShimFile.ReadAllTextString = (arg) => "x";

                //act
                result = reader.ReadAllText(@"X:\doesnotexist\notThere.txt");
            }

            //assert
            Assert.That(result, Is.EqualTo("x"));
        }
posted on Saturday, June 30, 2012 3:57:04 PM (Romance Daylight Time, UTC+02:00)  #    Comments [0]
# Friday, June 29, 2012

Last time at TechEd 2010 in Berlin was a little disappointing. After taking a year off, Microsoft moved this time to Amsterdam.

20120628_43020120627_417

Ok, I know we're not supposed to be sightseeing, but central Amsterdam is fun and easy to explore on foot. Another incidental but important point: Amsterdam in June is a lot warmer than Berlin in November. The weather was warm and humid under grey skies. The conference air conditioning struggled a little at times, so it was a little uncomfortable.

20120627_389

The venue was a little outside Amsterdam centre, but easily accessible by Metro. Finding rooms was, as always, a bit challenging, but by the middle of the week we had sort of worked it out.

I bet 90% of attendees tried to press the big buttons on the check-in screens. Turns out they weren't touch screens, you had to use the mouse. This at an event promoting Windows 8's touch screen abilities. Even more lame was that Wi-Fi was down for all Tuesday.

The bag was certainly nicer than 2010's. No T-shirt or other swag. This is the week that Google's developer event gave every attendee a new phone, a new tablet, and their new media streamer device. No Surface tablets or Nokia phones here. This is the first place I've seen anyone else with a Windows Phone- there were quite a few around. Three quarters of attendees had iPhones or Androids though. This is about as faithful an audience that Microsoft can get, and Windows Phone is in a minority. That's pretty bad for Microsoft and Nokia.

We'll get TechNet subscriptions, but they don't contain Visual Studio. For developers, who generally feel like second-class citizens at these events, it disappointing. Give us a cheap tablet with Windows 8 RC to play with, and we might be a little less sceptical about Metro. If we can't get enthusiastic about it, no-one will be. Why not DVDs with the RCs of Windows 8 and Visual Studio 2012 just to save us some bandwidth?

There were competitions to win Lumia 900s in the expo, but otherwise swag was disappointing there too.

20120626 dinner1 Dinners were vast - the scale is always impressive. Good food, too.

20120627_412The delegate party was at the Amsterdam arena, and that was a pretty good venue. Plenty of beer, cheese, and other nibbles, and huge screens to show the football. The music was way too loud, though (we're old boring gits, not teenagers).

20120627_0915 AmsterdamArena TechEd2012

20120626_387The keynotes on Tuesday and Wednesday heavily promoted Windows Server 2012 (Tues) and Windows 8/ metro (Wed). The key message from the 2nd keynote seems to be that Windows 8 scales uniformly up from Phone to tablet to desktop. Which is a different story to Apple, who have a clear distinction between iOS and the OS X versions (and MS's own previous CE/ full Windows split). A few glitches when the Metro gestures didn't work properly, which was amusing and disturbing.

My impression is that while it might work well on a small touchscreen, it's not obvious or easily discoverable for a desktop. The demo applications look nice, but most internal and third party business applications look like crap and there's nothing to make the average developer into a decent UX designer. Even properly designed metro apps use lots of whitespace, and have a low information density. Being chromeless actually makes it more difficult to understand what you can do (the Windows Phone IE has tabs, but I still haven't figured out how to switch between them- I think Windows 8 metro IE is the same). The limited choice of full screen or side-by-side docking is just inadequate for a lot of normal PC users.

There were some good sessions. Honestly I'm not sure I learned a lot new, but it most sessions were a good review and clarification of what's current (see my Azure posting). All the information is already on the internet, so a few sessions turned out to be quite boring. But it can be hard to keep up when searches bring up old blog posts that are way out of date, and the sheer range of things that are going on. Best session was Scott Gu's Azure introduction (some of the following Azure sessions were repeating the same information, and were quite dull as a result). Mads Kristensen from the asp.net team was great too, showing current Visual Studio work that only existed on his computer.

The last one was disappointing because there wasn't any new stuff coming out. This year there is, so overall it was worthwhile (thanks to my company for paying for it!). I wonder if these conferences will still be relevant much longer. I'm not arguing that we should get lots of free swag, but they do have to give a compelling reason to physically attend. apart from all the free beer, of course.

posted on Friday, June 29, 2012 10:16:38 PM (Romance Daylight Time, UTC+02:00)  #    Comments [0]
# Thursday, June 28, 2012

azurelogoWebsites are new in Azure From conversations and talks at Teched:

Web Roles Websites
The standard offering New (summer 2012). In preview and discounted at time of writing.
For asp.net/php/html same
Use Azure SQL/Blobs/Table storage. Can use the new Caching. same
- Has a persistent shared disk.
Production and staging. Instant switch for releases. No. You upload to the live site, so a long release will mean the site is broken for that time ( but see below- you can do diff releases)
Can install dependencies (3rd party installations etc, GAC) Bin-deployment of dlls only - or one of a small number of packages like Umbracco.
Release as cspack. Spins up new VMs for each release. Release by web deploy, git, TFS, FTP.
RDP access No. Just FTP.
   

In conclusion:

Web sites simplify the story for simple web sites. But having no staging is a bit limited.

Web roles are better for more complex sites, with dependencies and/or worker roles.

There is no option to upgrade a website to a web role.

All the above will probably be obsolete is 6 months as the features expand.

posted on Thursday, June 28, 2012 11:24:41 PM (Romance Daylight Time, UTC+02:00)  #    Comments [0]
# Wednesday, March 21, 2012
EF Code First has a neat method to merge in the values of a DTO into an mapped entity.

context.Entry(entity).CurrentValues.SetValues(dataTransferObject);

The DTO will generally have a primary key property, and you can use that to determine if it is a new record or a modification. Here's a method that does that:
public static T Merge<T>(this DbContext context, object dataTransferObject)
             where T : class
        {
            if (context == null) throw new ArgumentNullException("context");
            if (dataTransferObject == null) throw new ArgumentNullException("dataTransferObject");
 
            var property = FindPrimaryKeyProperty<T>(context);
            //find the id property of the dto
            var idProperty = dataTransferObject.GetType().GetProperty(property.Name);
            if (idProperty == null)
                throw new InvalidOperationException("Cannot find an id on the dataTransferObject");
            var id = idProperty.GetValue(dataTransferObject, null);
            //has the id been set (existing item) or not (transient)?
            var propertyType = property.PropertyType;
            var transientValue = propertyType.IsValueType ?
                Activator.CreateInstance(propertyType) : null;
            var isTransient = Equals(id, transientValue);
            T entity;
            if (isTransient)
            {
                //it's transient, just create a dummy
                entity = CreateEntity<T>(id, property);
                //if DatabaseGeneratedOption(DatabaseGeneratedOption.None) and no id, this errors
                context.Set<T>().Attach(entity);
            }
            else
            {
                //try to load from identity map or database
                entity = context.Set<T>().Find(id);
                if (entity == null)
                {
                    //could not find entity, assume assigned primary key
                    entity = CreateEntity<T>(id, property);
                    context.Set<T>().Add(entity);
                }
            }
            //copy the values from DTO onto the entry
            context.Entry(entity).CurrentValues.SetValues(dataTransferObject);
            return entity;
        }
 
 
        private static PropertyInfo FindPrimaryKeyProperty<T>(IObjectContextAdapter context)
            where T : class
        {
            //find the primary key
            var objectContext = context.ObjectContext;
            //this will error if it's not a mapped entity
            var objectSet = objectContext.CreateObjectSet<T>();
            var elementType = objectSet.EntitySet.ElementType;
            var pk = elementType.KeyMembers.First();
            //look it up on the entity
            var propertyInfo = typeof(T).GetProperty(pk.Name);
            return propertyInfo;
        }
 
        private static T CreateEntity<T>(object id, PropertyInfo property)
            where T : class
        {
            // consider IoC here
            var entity = (T)Activator.CreateInstance(typeof(T));
            //set the value of the primary key (may error if wrong type)
            property.SetValue(entity, id, null);
            return entity;
        }

posted on Wednesday, March 21, 2012 7:18:25 AM (Romance Standard Time, UTC+01:00)  #    Comments [0]
# Tuesday, March 20, 2012
When the Code First project get a detached entity from the UI, it may need to check if it is a new (transient) entity - which can be added to the DbSet - or an existing entity that has been modified. You can then implement an AddOrUpdate method.

To do that, it needs to know what the primary key of the entity is, and read the value.

The easiest way to do is generically is for all entities to have a standard interface or abstract base.
var id = ((IEntity) entity).Id;
if (id == default(int))
{
    //add
}
else
{
    //update
}
If you use the [Key] attribute you can also use that to discover the primary key of the entity, whatever the type.

Finally,  you can use EF's internal metadata.
public static bool IsTransient<T>(DbContext context, T entity)
    where T : class
{
    //find the primary key
    var objectContext = ((IObjectContextAdapter)context).ObjectContext;
    //this will error if it's not a mapped entity
    var objectSet = objectContext.CreateObjectSet<T>();
    var elementType = objectSet.EntitySet.ElementType;
    var pk = elementType.KeyMembers.First();
    //look it up on the entity
    var propertyInfo = typeof(T).GetProperty(pk.Name);
    var propertyType = propertyInfo.PropertyType;
    //what's the default value for the type?
    var transientValue = propertyType.IsValueType ? Activator.CreateInstance(propertyType) : null;
    //is the pk the same as the default value (int == 0, string == null ...)
    return propertyInfo.GetValue(entity, null) == transientValue;
}

posted on Tuesday, March 20, 2012 5:29:09 AM (Romance Standard Time, UTC+01:00)  #    Comments [0]
# Monday, March 19, 2012
Last time I was adding a new record with a reference to a dummy record. I marked the reference as Unchanged so Code First wouldn't try to validate or save it.

var johnCarter = new Movie() { Title = "John Carter" };
johnCarter.DirectorId = andrewStantonId;
context.Movies.Add(johnCarter);
//after it's added, change the status of the reference
context.Entry(johnCarter.Director).State =
EntityState.Unchanged;
context.SaveChanges();

Can you set all the references on any entity?

var johnCarter = new Movie() { Title = "John Carter" };
johnCarter.DirectorId = andrewStantonId;
context.Movies.Add(johnCarter);
//after it's added, change the status of the reference
MarkNavigationPropertiesUnchanged(johnCarter);
context.SaveChanges();

We have to look into the underlying EF model.

private static void MarkNavigationPropertiesUnchanged<T>(DbContext context, T entity)
    where T : class
{
    var objectContext = ((IObjectContextAdapter)context).ObjectContext;
    var objectSet = objectContext.CreateObjectSet<T>();
    var elementType = objectSet.EntitySet.ElementType;
    var navigationProperties = elementType.NavigationProperties;
    //the references
    var references = from navigationProperty in navigationProperties
                        let end = navigationProperty.ToEndMember
                        where end.RelationshipMultiplicity == RelationshipMultiplicity.ZeroOrOne ||
                        end.RelationshipMultiplicity == RelationshipMultiplicity.One
                        select navigationProperty.Name;
    //NB: We don't check Collections. EF wants to handle the object graph.
 
    var parentEntityState = context.Entry(entity).State;
    foreach (var navigationProperty in references)
    {
        //if it's modified but not loaded, don't need to touch it
        if (parentEntityState == EntityState.Modified &&
            !context.Entry(entity).Reference(navigationProperty).IsLoaded)
            continue;
        var propertyInfo = typeof(T).GetProperty(navigationProperty);
        var value = propertyInfo.GetValue(entity, null);
        context.Entry(value).State = EntityState.Unchanged;
    }
}

This code only fixes the references to single entities (like movie.Director) - not collections (like director.Movies). It's possible to discover and iterate the collections to change their status, but you'll likely get exceptions from EF because its model is broken.

posted on Monday, March 19, 2012 6:08:43 PM (Romance Standard Time, UTC+01:00)  #    Comments [0]
# Sunday, March 18, 2012
You’re adding a new record, which has a reference to an existing object.

var johnCarter = new Movie() { Title = "John Carter" };
johnCarter.Director = new
Director { Id = andrewStantonId };
context.Movies.Add(johnCarter);
context.SaveChanges();

SaveChanges() will save the new Movie- but it also saves a new Director record (which gets a new Id, even though you set it manually). When you add an entity to a DbSet, the entire object graph is marked as “Added”.

You could do a Find to load the Director record from the database, but it is a pointless database access that you don’t need. It just needs to save the Movie record with a known directorId.

In NHibernate you can use session.Load<Director>(andrewStantonId) which will create an empty proxy object without hitting the database. Only if you use one of the proxy properties (like director.Name) will it hit the database to load the record. EF Code First doesn’t have this feature.

One way round it to add a foreign key Id property to the Movie record:

       public virtual Director Director { get; set; }
       public int? DirectorId { get; set; }

You can then set the DirectorId directly. The two properties are not kept in step automatically, so setting the DirectorId doesn’t cause Director to load from the database. Foreign key Id properties are convenient, but your object model is “denormalized”.

The alternative is to mark the dummy record as unchanged. There are two ways.

One is to set the context.Entry state for the dummy reference AFTER the new record has been added.

var johnCarter = new Movie() { Title = "John Carter" };
johnCarter.DirectorId = andrewStantonId;
context.Movies.Add(johnCarter);
//after it's added, change the status of the reference
context.Entry(johnCarter.Director).State =
EntityState.Unchanged;
context.SaveChanges();

The second way is to create the dummy reference by Attaching it.

//attach the dummy director record
var andrewStanton = new
Director { Id = andrewStantonId};
context.Directors.Attach(andrewStanton);
//now we have an "unchanged" director record to attach
var johnCarter = new
Movie() { Title = "John Carter" };
johnCarter.Director = andrewStanton;
context.Movies.Add(johnCarter);
context.SaveChanges();

posted on Sunday, March 18, 2012 8:04:08 AM (Romance Standard Time, UTC+01:00)  #    Comments [0]