Thursday, 04 Jun, 2015
Configuring SignalR in ASP.NET MVC, using StructureMap as the IoC container is fairly straightforward, but not without some subtleties that caught me out.
For the purposes of this post, I'm going to assume that you are familiar with both SignalR and StructureMap, and that you know how to configure them in an ASP.NET MVC application. I will also assume that through some google-fu you have seen the Dependency Injection in SignalR guidance, and have worked through it and got to the "Using IoC Containers in SignalR" section.
I would assume, although I've not tested it, that much of this could also be applied to a self-hosted SignalR server.
Library versions used
This post is based on:
- Asp.Net MVC 5.2.3
- SignalR 2.2.0
- StructureMap 188.8.131.52
- StructureMap.MVC5 184.108.40.206
Follow the guidance up to the section on using Ninject, at which point we now want to configure StructureMap.
Replace the SignalR Dependency Resolver
The implementation is nearly identical, with some obvious StructureMap specific differences:
public class StructureMapSignalRDependencyResolver : DefaultDependencyResolver
private readonly IContainer _container;
public StructureMapSignalRDependencyResolver(IContainer container)
_container = container;
public override object GetService(Type serviceType)
return _container.TryGetInstance(serviceType) ?? base.GetService(serviceType);
public override IEnumerable<object> GetServices(Type serviceType)
var objects = _container.GetAllInstances(serviceType).Cast<object>();
The behaviour is fairly similar.
TryGetInstance will attempt to resolve the type, and if it doesn't know about it, will return null, in which case we call the base resolver, which does.
Register this with StructureMap:
Startup, where you configure SignalR, we need to use this new resolver implementation:
var resolver = DependencyResolver.Current.GetService<Microsoft.AspNet.SignalR.IDependencyResolver>();
var hubConfiguration = new HubConfiguration
Resolver = resolver
/* other options as required */
Here, we are using the MVC DependencyResolver, which has already been replaced by StructureMap thanks to StructureMap.MVC5, to resolve an instance of the SignalR dependency resolver we've registered, which we then tell SignalR to use with a hub configuration object.
Now we just need to configure the StructureMap registry, and teach it how to resolve
.Is(ctx => ctx.GetInstance<IDependencyResolver>()
As in the guidance, we want the
StockTicker instance to be a singleton, and we have specify how to resolve the
IHubConnectionContext<dynamic> which the
StockTicker requires. In the
Is, I'm using the context to resolve the default SignalR connection manager we've registered. This isn't in the guidance, but I couldn't get it work without doing this.
If anyone has comments/improvements on this, I'd love to hear them.
Friday, 17 Apr, 2015
It is possible to setup your build server to run code analysis on your solution/projects, without having to install VS on your build server.
The answer is here: http://stackoverflow.com/a/21731245/3181
I'm not going to reproduce the code here, there is no point. This post is just a reminder to myself as to where I found the solution to this.
Wednesday, 15 Apr, 2015
Gitlab is an open source 'clone' of Github, although I don't think it's much of a clone anymore, to be fair. The Enterprise edition has full Active Directory support, and the Community edition does not.
The integration with Active Directory is of great benefit. To get the most out of it, I think that you would need to upgrade to the Enterprise edition, as it adds a number of additonal integrations which look to be really usefull for larger organisations with multiple projects and development teams.
It is possible to configure the Community edition to integrate with Active Directory, to the extent that you can restrict exactly who is allowed to login in. You have to manually configure the users permissions on groups/projects though. In the Enterprise edition, the tighter integration means that you can match up Active Directory groups with groups in Gitlab, and control access both to the server, and groups/projects, all through Active Directory.
Here is the ldap section from my
gitlab.yml (please be aware, I'm no Active Directory expert):
bind_dn: 'CN=Git Lab,OU=AB,OU=example,DC=example,DC=com'
bind_dn is for a user called 'Git Lab', and you must suppoly the password. This user was expressly setup just for this, but you can use any user. The
base is the level of Active Directory that Gitlab searches for users from. Finally, the
user_filter states that users must members of
devs in order to be able to login.
Thursday, 19 Mar, 2015
Having just been stumped on this for longer than I care to admit, and only finding the answer to my problem buried in a comment on StackOverflow, I'm posting it here for my own future reference:
Q: Using ASP.Net MVC with Bootstrap 3, why won't my
<input> stretch to the full width of a
<div class="form-group">, even though I am putting
form-control on the css class?
A: Because Microsoft override
textarea to have
max-width: 280px in the default
Site.css which is added to new projects. Removing this will allow the 100% width from
Tuesday, 17 Mar, 2015
I like using NPoco, it's a really nice library that allows you stop having to write raw ADO.NET. Recently I just switched one of my projects at work to use the Fluent Mapping features, essentially so that my POCO's do not need to have the mapping attributes in them.
I started wth the example on the NPoco wiki of creating a database factory, and adding the mapping class to the fluent configuration. As the number of mapping classes has grown, this has quickly become unwieldy and a bit of a maintainance issue.
As I already utilise Structuremap in the application, I wanted to configure it to automatically pick up my mapping classes and build the configuration object for me. This is easy to in the registry:
For<FluentConfig>().Use(context => FluentMappingConfiguration.Configure(context.GetAllInstances<IMap>().ToArray()));
For<IDatabase>().HttpContextScoped().Use(context => context.GetInstance<DatabaseFactory>().GetDatabase());
The mapping classes must inherit from
Map<T>, which itself inherits from
IMap. Thus we can confidently add all types of
IMap in the scan.
Then an instance of
FluentConfig must be registered, which can be done with an overload of
.Use, from which we get the StructureMap context and pull out all the types of IMap which have been found and pass it to the static
.Configure method of the
FluentMappingConfiguration, which constructs the instance.
DatabaseFactory (see below) is then registered, and it accepts a 'FluentConfig' instance in its constructor. As per the original example on the NPoco wiki, it is marked as a singleton to ensure that only one instance is created, so that the mappings do not get registered more than once. I don't know what would happen if the mappings were to be registered more than once, but I assume that it would be a Bad Thing™.
Then, for the NPoco
IDatabase instance that we register, we want to always create it using the instance of the
DatabaseFactory that has already been registered, which again is accessible using the overload of
.Use to get the Structuremap context. It's marked as
.HttpContextScoped(), but I'm honestly not sure if that's required or not given that I'm using
Structuremap.MVC5 nested containers.
For completeness, here is the
public class DatabaseFactory
private static NPoco.DatabaseFactory _internalFactory;
public DatabaseFactory(FluentConfig fluentConfig)
private static void Configure(FluentConfig fluentConfiguration)
_internalFactory = NPoco.DatabaseFactory.Config(x =>
x.UsingDatabase(() => new Database("ConnectionString"));
public NPoco.Database GetDatabase()