C# – StructureMap Error 202 Setting up IOC container

asp.net-mvccioc-containerstructuremap

I'm getting an error:

StructureMap Exception Code: 202
No Default Instance defined for PluginFamily MVCPoco.Services.IService, MVCPoco.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Line 96:         {
Line 97:             Type controllerType = base.GetControllerType(context, controllerName);
Line 98:             return ObjectFactory.GetInstance(controllerType) as IController;
Line 99:         }
Line 100:    }

the error occurs in line 98

Any ideas?
I'm using asp.net mvc 2 preview 2 that ships with visual studio 2010.

Best Answer

The controller you are trying to instantiate has a constructor dependency on IService. You have to make sure that you register a concrete implementation of IService when you configure StructureMap. The DefaultConventionScanner will only register implementations that have the same name as their interface (without the leading I). So, unless your implementation of IService is named Service, it will not be registered automatically. To register it explicitly, add something like this to your inititalization script:

x.For<IService>().Use<MyService>();

Alternatively, if you are running StructureMap from the latest source code, you can make use of the SingleImplementationScanner in your Scan() expression:

y.With(new SingleImplementationScanner());

and that will automatically register concrete types if they are the only implementation of an interface in the scanned code, regardless of name.

Related Topic