A TinyIoC Bootstrapper

I really like the TinyIoC Inversion of Control Container. It’s nice and straight forward to use, it’s easily portable, and the auto registration feature does 99% of your work for you. In recent projects it has been an ideal choice, either because of the size of the project, or to introduce the concepts of DI to teams that weren’t already familiar with it.

With a little work though we can help it punch a little above its weight.

The sidekick to an IoC container is the Bootstrapper. A good bootstrapper can make DI a simple painless exercise that just works, so I thought I’d share mine. It started life with the DefaultNancyBootstrapper from the Nancy project and evolved from there.

It’s available and documented with examples on GitHub here, so I won’t go into it in depth, but I will cover the main aspects I wanted to solve.

Auto Scanning

The DefaultNancyBootstrapper auto registration takes all types from AppDomain.CurrentDomain.GetAssemblies. In some scenarios though this isn’t great, as it only picks up those assemblies that are already loaded. As the bootstrapping is usually done very early in the life of the application it may easily be that the bootstrapping omits many of the types. Any missed then need to be hooked up manually. I wanted to enhance this scanning to require less help.

Flexibility

The bootstrapping needs to be flexible enough to allow the developer control over which assemblies/types are not scanned, and to pass in extra assemblies for scanning which aren’t available for pickup automatically.

Extensibility

The bootstrapping should be configurable across various layers in a more complex system (so maybe a solution wide base bootstrapper, and then specific overrides for MVC/WebApi/Service projects).

After a few iterations I ended up with a nice lightweight solution, which had the configurability I was looking for.

You can see the whole project here, or jump straight to the bootstrapper class here.

SignalR: The ConnectionId is in the incorrect format – an alternative cause.

Recently, on an application using SignalR, we found that we were getting many System.InvalidOperationException: The ConnectionId is in the incorrect format exceptions (sometimes 10 per second) from the SignalR hubs in the site.

While there is plenty of discussion on the causes of this error in SignalR documentation and on Q&A sites, they mostly discuss problems with changing levels of authentication (SignalR defines a new connection when the auth level, or authorized user, changes). In this case though this wasn’t relevant. The volume implied that this was unlikely to be the cause, and in anycase, the site was already written to cope with these changes on login.

The cause turned out to be search engine crawler services. Continue reading SignalR: The ConnectionId is in the incorrect format – an alternative cause.

ASP.Net Webforms – Dynamic UpdatePanels and UserControls issue

Have come across an interesting issues in ASP.Net WebForms when migrating a project from 3.5 up to 4.5.

The site in question is extremely dynamic, the page is built up based on configuration in a CMS fashion.

However in 4.5 we have a problem – when more content is added into the page via a button click, not all the markup for the content appears.

Continue reading ASP.Net Webforms – Dynamic UpdatePanels and UserControls issue

AutoMapper and EntityFramework Proxies – a workaround

This is a quick workaround for an issue I came across when working with AutoMapper on EF. Its one of those blog posts that’s as much a reminder for the writer as anything else.

I was trying to repopulate an entity instance from a corresponding model instance using a line similar to the following:

Mapper.Map<MyModel,MyEntity>(model, ent);

At this line I got an AutoMapper.AutoMapperMappingException accompanied by the following message:

Missing type map configuration or unsupported mapping. Mapping types: MyModel-> MyEntity_238F6DF9C0DAD0768B6BF2E9… MyProject.MyModel-> System.Data.Entity.DynamicProxies.MyEntity_238F6DF9C0DAD0768B6BF2E9…

Take as read that I certainly did have a Mapper.Create<MyModel,MyEntity>() type line in place.

Continue reading AutoMapper and EntityFramework Proxies – a workaround

DropDownList Helper For Enums

Its useful to be able to have a helper that converts a Enum typed property from an MVC Model directly into a drop down list without any messing about creating lists of possible values etc.

I found this answer on StackOverflow which has example code for just such a helper: EnumDropDownListFor. However, like a few who have tried it I found some issues around getting it to work to pre-select the value held in the Model’s property.

Continue reading DropDownList Helper For Enums

EditorFor and Interfaces Pt2 – Binding

This is a two part piece on working with Interfaces in MVC via the Html.EditorFor helper.

In the first part – “Display” just covers one of those things I tried on the off chance, and was rather surprised that it worked right out of the box.

In this second part I’ll have a look at the next step of the process – binding these interfaces back to their models.

For reference, I’m working with MVC4, in VS 2012 For Web. I think this will also work in MVC3 at least.

Background

When model data is posted back from the client, an instance of the model class is automatically created for it and passed to the handling action. The process of converting the data into a model instance is called binding, and, normally, this is handled for us automatically.

If, as in Part 1, we’re using an interface as a model, and then allowing EditorFor to automatically render the correct controls this processes stops working, as the automatic binding isn’t able to resolve our interface into a concrete class to instantiate and fill.

Instead we have to implement a custom binding. In the case of interfaces we can make this fairly simple with Generic BaseInterfaceBinder that will work for all interfaces we might want to work with.

Continue reading EditorFor and Interfaces Pt2 – Binding

EditorFor and Interfaces Pt1 – Display

This is a two part piece on working with Interfaces in MVC via the Html.EditorFor helper.

The first part – “Display” just covers one of those things I tried on the off chance, and was rather surprised that it worked right out of the box.

In the second part I’ll have a look at the next step of the process – binding these interfaces back to their models.

For reference, I’m working with MVC4, in VS 2012 For Web. I think this will also work in MVC3 at least.

Background

For a slightly messy user interface I’m working on, I wanted to be able to have a Model that holds a list of classes that implement an interface.

There is a possibility that all the classes might not be the same, and I was concerned that I was going to end up with a messy switch statement in my View that called the right Partial depending on which class I found in the list, and that this switch might need to be maintained on an on-going basis.

Continue reading EditorFor and Interfaces Pt1 – Display

Loading Generic Types with Assembly.LoadFrom

I thought I’d write up a question I posed today on StackOverflow here: Loading a generic type using Assembly.LoadFrom

I had a pre-existing method that I use to get a type based on a full assembly path and a class name. This resides in a Utils namespace.

Continue reading Loading Generic Types with Assembly.LoadFrom

Determining the state of modifier keys when hooking keyboard input

As part of a utility I was writing recently I wanted to hook keyboard input. In particular I was interested in global hooks (ie input to any program, not just mine), and I needed to handle complex key inputs, for example Ctrl-Alt-G, i.e. keys plus modifiers. The usual way to do this is to use SetWindowsHookEx.

Looking around I quickly found a class over on CodeProject here. The globalKeyboardHook class deals with the hooking, allows you to specify which keys you are interested in and disregards the rest. The events are raised using the delegate type KeyEventHandler. KeyEventHandler uses KeyEventArgs and has easily referenced properties for each of the modifier keys. So far so good.

However I found that the instance of KeyEventArgs I was receiving in my event handler never had the modifier properties set to true. Looking at the code for the class I couldn’t see anything there that would set the properties in the call back function for SetWindowsHookEx.

Continue reading Determining the state of modifier keys when hooking keyboard input

Crystal Reports – setting login credentials through code

I just answered a stackoverflow question regarding a common issue when using Crystal Reports as part of a wider application – setting login credentials through code. Its not as straight forward as it really ought to be.

Rather than simply setting the login/server/database details through a single call there are a number of things that are required:

Continue reading Crystal Reports – setting login credentials through code